Exporting only the plot legend to a jpg file
84 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Joe Lettieri
el 31 de Mzo. de 2021
Comentada: Xiangyan An
el 19 de Dic. de 2023
I have an odd case where I want to export the legend (only) of a plot to a jpeg file. That is, I don't what the whole figure, or even the axes - just the legend. It seems that exportgrpahics cannot do that. Is there another way, or do I have to resort to a screen shot :(
I'm running MATLAB Version: 9.8.0.1323502 (R2020a)
0 comentarios
Respuesta aceptada
Nikhil Sapre
el 25 de Jun. de 2021
Hi Joe,
You can try something like this to turn of visibility of your plots, and axis object. This will export only the Legend from the figure.
clear all; close all;
figure;
p1 = plot([1:10], [1:10], '+-');
hold on;
p2 = plot([1:10], [1:10]+2, 'o--');
legend('text1', 'text2');
set(p1, 'visible', 'off');
set(p2, 'visible', 'off');
set(gca, 'visible', 'off');
exportgraphics(gcf,'test.jpg');
Thanks,
Nikhil.
3 comentarios
Heather
el 15 de Nov. de 2022
Editada: Heather
el 15 de Nov. de 2022
@Nikhil Sapre, this looks like a great solution. One question: I'm finding that the legend text looks a bit grayed out. (Using Matlab 2020b.) Do you have any tips on how to avoid that? I've tried explicitly noting 'TextColor','k' in the legend options but that doesn't seem to help.
Aboltabol
el 3 de Jul. de 2023
Sapre's wayout is not really a solution. When you follow his example, you are turning off plots, one by one, and the according legends are grayed out in succession. Feature; not bug.
Más respuestas (2)
DGM
el 3 de Jul. de 2023
I don't have exportgraphics(), so I'm not going to bother with that.
Consider the example:
% plot some fake data
yy = rand(20,3);
hp = plot(yy);
% put a legend on it
legstr = {sprintf('\\theta_{min} = %4.2f',rand()) ...
sprintf('\\theta_{min} = %4.2f',rand()) ...
sprintf('\\theta_{min} = %4.2f',rand())};
hl = legend(hp,legstr);
% get screenshot of figure as a raster image
drawnow
outframe = frame2im(getframe(gcf));
% crop the legend out of the image
hl.Units = 'pixels';
legpos = hl.Position;
legpos(2) = size(outframe,1)-legpos(2)-legpos(4)+2;
outframe = imcrop(outframe,legpos);
imshow(outframe,'border','tight') % show it
%% compare the results
imwrite(outframe,'out.jpg') % save the image
testjpg = imread('out.jpg'); % read the image back
S = dir('out.jpg'); S.bytes % get the file size in bytes
imwrite(outframe,'out.png') % save the image
testpng = imread('out.png'); % read the image back
S = dir('out.png'); S.bytes % get the file size in bytes
imshow([testjpg; testpng],'border','tight') % JPG is remarkably worse
So yes, you can save the legend alone as a JPG. You can also slam your hand in a car door. These are things you should actively avoid doing to yourself. Unless you're trying to work around compatibility issues with some other piece of crippled software that supports no image formats beyond JPG, there are often no good reasons to save your results as JPG. That goes especially for synthetic images like these with strong edges and flat regions of uniform color. That's even more important in MATLAB, where all JPG outputs are 4:2:0 chroma subsampled. In the given example, the saved JPG is littered with conspicuous artifacts and the apparent line colors are significantly altered. Not only is the image irreversibly damaged, this garbage-quality JPG file is more than twice the size of a clean, lossless PNG file. In this scenario, JPG offers no advantages to compensate for its faults.
Don't use JPG unless you understand and accept the amount of information you're throwing away and the amount of compression you're actually able to achieve with the given image content.
0 comentarios
Xiangyan An
el 19 de Dic. de 2023
Instead of turing the axis and plots unvisible, we can set the axis sufficiently small or outside the figure or both. Then we can get a figure with the axis and plots unvisible, and the legend text is not grayed.
Following code are modified from that of Sapre acoording to this idea.
clear all; close all;
figure;
p1 = plot([1:10], [1:10], '+-');
hold on;
p2 = plot([1:10], [1:10]+2, 'o--');
lgd = legend('text1', 'text2');
lgd.Position(1:2) = [0.4, 0.6]; % any position at will
ax = gca;
ax.Position = [-0.1, -0.1, 0.01, 0.01];
p1.MarkerSize = 0.001;
p2.MarkerSize = 0.001;
exportgraphics(gcf,'test.jpg');
2 comentarios
DGM
el 19 de Dic. de 2023
Sure, but again, why would you want to use a JPG?
When a PNG is cleaner and smaller?
That said, don't ask me why exportgraphics() can't even export the same thing twice consistently.
Xiangyan An
el 19 de Dic. de 2023
I focus on the way to make the legend sole and this is what I want to show. One can save the figure as any format and through any function at will.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!