How to save a figure/plot after annotating?
显示 更早的评论
Hi, i have been trying to google whole day a very simple answer to a basic question but unfortunately i cannot find the answer. My query is very simple. I have a plot and using the tool menu in the figure display window, i drew a rectangle on my plot marking an area of interest. The problem is if i use save as command from file menu, i cannot control the resolution of the saved plot. I tried to use the code generator for the box created and added the line of the code in the plot code and tried saving the figure using print and export_ fig commands but both times, the location was different as compared to the orignal postion in the maxized window.
How can i save the annotated plot in eps format with having the desired resolution.

for annotation i used this code . In the attachment one can see how the blue box moves after saving it using a command.
annotation(f_stacked,'rectangle',[0.317276041666667 0.508639308855292 0.0172291666666667 0.390928725701941],...
'Color',[0 0 1],...
'LineWidth',3);
set (f_stacked, 'Clipping', 'on');% experimented with it, did not worked
4 个评论
dpb
2023-7-10
annotation uses normalized coordinates from the figure by default; I think these screw up when exporting the figure.
I've never really had to try to print/export figures but it seems to have been/continue to be a bane of everybodys' existence that have had to try. TMW just can't seem to solve a lot of similar issues cleanly for some reason...
William Rose
2023-7-10
@arjun luther, when the two previous commenters say stuff, I pay attention because they always give excellent advice.
dpb
2023-7-11
@William Rose Agree, the stackedplot is not a regular axes into which one plots; I wouldn't be surprised if there are still warts with it that may not show on a regular axes as @arjun luther used for illustration.
@Adam Danz is good, although the link he posted uses only axes figures, not any that are a <standalone visualization object>. With only the laptop at the moment and a pressing deadline, I don't have time to try to explore much at this instant, sorry...
采纳的回答
更多回答(1 个)
William Rose
2023-7-10
plot(0:10,0:10,'-rx')
ax=gca;
exportgraphics(ax,'MyFig300.eps','Resolution',300);
After issuing the plot command above, I added a rectangle using Insert -> Rectangle in the figure window. The saved eps file does include the rectangle.
Good luck.
4 个评论
In this example, I use the annotation command, like you did.
plot(0:10,0:10,'-rx'); grid on
annotation('rectangle',[.01 .01 .98 .98],'Color',[0 0 1]);
annotation('rectangle',[.45 .45 .10 .10],'Color',[0 1 0]);
ax=gca;
exportgraphics(ax,'MyFig300.eps','Resolution',300)
The rectangles in the eps file are in the same place as the Matlab plot above. annotate's frame (0 to 1 and 0 to 1) includes areas outside the plot box (axis labels, title area, etc.).
arjun luther
2023-7-10
[edit: correct spelling]
@dpb says rectangle() uses the units of the quantities being plotted. That is good to know, since it is a lot more convenient to use plot units.
Your sample plot has a rectangle that crosses subplots. This is probably not possible with rectangle(), but you can make such a rectange with annotate().
I have not used stackedplot() much. It works somewhat differently than regular plots, as @dpb noted. If the problem persists, and you really are bothered by it, you could investigate using a loop with subplot(9,1,i) to make 9 subplots. But you will get an x-axis under every plot, which is ugly, unnecessary, and a waste of space. I don't know if the rectangle placement in the eps file would work any better with subplots than it does with stackedplot().
% datetime vector: 8:00, 8:01,...,10:59,11:00, similar to your plot
D=datetime(2023,7,10,8,0:180,0);
t=convertTo(D,'excel');
x=cos(48*pi*t')*ones(1,9);
stackedplot(D,x)
annotation('rectangle',[.35 .47 .04 .46],'Color',[0 1 0],'Linewidth',3);
exportgraphics(gca,'MyFig300.eps','Resolution',300)
The code makes a figure similar to yours: nine stacked plots, plus a rectangle that spans 5 plots. It saves the figure as an eps file with specified resolution. The rectangle in the eps file (atached) looks like it is in the same place as in the Matlab figure.
dpb
2023-7-11
@William Rose, the point about annotation being able to cross axes boundaries is a good one; hadn't really thought about that the stackedplot is multiple axes even though don't show..
类别
在 帮助中心 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





