How can I add a border around a figure window (not just the current axis)?
81 views (last 30 days)
Show older comments
I want to add a border window around a figure window and not just the plot area. I want the box to include xlabel, ylabel, and title of the plot plus other text in the figure sides. Below is a copy of what I want boxed:
x = 0:pi/100:10*pi;
y = -1 + 2*cos(x);
f = figure;
f.WindowState = 'maximize';
f.Color = 'white';
plot(x,y);
f.CurrentAxes.Position=[0.1 0.1 0.85 0.85];
grid on;
xlabel('X','FontSize',16)
ylabel('Y','FontSize',16)
title('Trial','FontSize',20)
text([-3.5 31.5],[1.1 -3.3],'Graph of -1 + 2*cos(x)','Color','r','FontSize',18)
saveas(f,'PIC','png');
0 Comments
Answers (1)
Image Analyst
on 2 Dec 2020
Try this:
x = 0:pi/100:10*pi;
y = -1 + 2*cos(x);
f = figure;
f.WindowState = 'maximize';
f.Color = 'white';
plot(x,y);
f.CurrentAxes.Position=[0.1 0.1 0.85 0.85];
grid on;
xlabel('X','FontSize',16)
ylabel('Y','FontSize',16)
title('Trial','FontSize',20)
text([-3.5 31.5],[1.1 -3.3],'Graph of -1 + 2*cos(x)','Color','r','FontSize',18)
% Shrink the axes.
ax = gca;
ax.Units = 'normalized';
ax.Position = [.1, .1, .8, .8];
% Add a red frame to the figure.
f.Units = 'normalized';
lineWidth = 50; % Whatever you want.
annotation('line', [0, 0], [0, 1], 'LineWidth', lineWidth, 'Color', 'r');
annotation('line', [1, 1], [0, 1], 'LineWidth', lineWidth, 'Color', 'r');
annotation('line', [0, 1], [0, 0], 'LineWidth', lineWidth, 'Color', 'r');
annotation('line', [1, 0], [1, 1], 'LineWidth', lineWidth, 'Color', 'r');
msgbox('Done!');
0 Comments
See Also
Categories
Find more on Specifying Target for Graphics Output in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!