how to manually add a legend?

42 次查看(过去 30 天)
Hana Ahmed
Hana Ahmed 2021-11-24
编辑: DGM 2021-11-24
I have generated a Matlab plot, but unfortunately I did not save it in Matlab. I saved only a screenshot from the figure. I need to add a legend.
How can I add it manually?
  2 个评论
DGM
DGM 2021-11-24
编辑:DGM 2021-11-24
I see three obvious options:
  1. Recreate the plot from the data and incorporate a legend as needed.
  2. Create a dummy plot using the same linestyle and color; add a legend to the dummy plot and save as an image. Crop the legend from the dummy plot and paste it into the old image using any convenient image editing software.
  3. Create a dummy plot, display the image in the same axes, then call legend so that it's stacked on top of the image. Wrestle with position properties in order to make it not look ridiculous.
Hana Ahmed
Hana Ahmed 2021-11-24
@DGMThank you for your help. Could you please clarify how I can do option 3 in Matlab?

请先登录,再进行评论。

采纳的回答

DGM
DGM 2021-11-24
编辑:DGM 2021-11-24
The suggestions I gave were in order of decreasing quality. Option #3 was intended to appear impractical enough that option #1 would seem more appealing. It's not impossible or terribly difficult; it's just going to require more work and the results are almost guaranteed to be worse.
Say I have the saved plot
% read image
A = imread('badplot.png');
s = size(A);
% plot something
% doesn't matter so long as linestyle matches
% i'm only plotting one point here so that it's hidden
plot(s(2)/2,s(1)/2,':o');
% overlay image and legend
hold on;
imshow(A)
axis image
hl = legend('banana');
% adjust legend position
hl.Position(1:2) = [0.65 0.80];
% recapture image
print(gcf,'fixedplot3.png','-dpng');
Image displays are for visualization purposes only. They are presented using nearest-neighbor interpolation. Trying to take screenshots of images as an image processing workflow is unnecessarily destructive unless an unwarranted amount of care is taken. Don't be surprised if your image looks worse after creating what amounts to a screenshot of a screenshot.
Options 1 and 2 should be self-explanatory. Even if option 1 isn't acceptable (e.g. due to loss of data, etc), then option 2 should only take a few minutes. It only took me 47 seconds to use GIMP to copy and paste the legend into the original plot image:

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by