Legend marker appearance changes exporting to vector graphic
조회 수: 14 (최근 30일)
이전 댓글 표시
I'm experiencing an issue exporting figures to a pdf vector graphic format that doesn't occur for the equivalent png bitmap.
I wish to have a marker edge color distinct from the marker face color of a line plot, and also of a different linewidth from the actual line. This can be done through the MarkerHandle property, however, the legend doesn't replicate the same behaviour. To match the line marker style in the legend I am using a complicated chain of indexing through the legend hierachy which works and produces the correct result on screen. The problem arises when exporting the figure to a vector graphics format where the legend reverts to the default behaviour. This only occurs for vector formats, I can export as a bitmap and the resulting image is exactly as I want.
Interestingly, errorbars don't appear to have the same limitations - the legend requires no extra edits to have the different marker edge linewidth. Thus, I am currently creating all my plots as errorbars, removing the caps, and using Inkscape to get rid of the vertical protrusions. This is fine, and I can certainly live with it, but a solution that doesn't involve an external application, and in particular one which uses the plot or line function, would be ideal.
So, is there a way to either, a) stop the legend reverting when exporting as a vector, or b) achieve the same result through some other means (e.g. remove vertical protrusions from errorbar in legend)?
Here is a MWE showing what I mean:
clear
%% Create figure
x = 0:0.5:10;
y = cos(x);
err = zeros(1, length(x));
fig = figure();
hold on
h1 = plot(x, y, 'DisplayName', 'Line');
h1.LineWidth = 2;
h1.Marker = 'o';
h1.MarkerFaceColor = h1.Color;
h1.MarkerEdgeColor = 'k';
h1.MarkerHandle.LineWidth = 0.5;
h2 = errorbar(x, y-0.3, err, 'DisplayName', 'Errorbar');
h2.LineWidth = 2;
h2.CapSize = 0;
h2.Marker = 'o';
h2.MarkerFaceColor = h2.Color;
h2.MarkerEdgeColor = 'k';
h2.MarkerHandle.LineWidth = 0.5;
%% Add legend
% The marker that appears in the legend has a consistent edge linewidth for the
% errorbar but not for the line
leg = legend();
%% Edit legend marker
% Set the marker edge linewidth in the legend to match
leg.EntryContainer.Children(2).Icon.Transform.Children.Children(1).LineWidth = 0.5;
%% Export figure
% Bitmap images are OK
exportgraphics(fig, 'correct.png', 'Resolution', 300);
% Vector images revert the legend marker to the default state
exportgraphics(fig, 'incorrect.pdf', 'ContentType', 'vector');
You'll see the marker edge linewidth differs between the line plot in the two figures, but not the errorbar plot.
댓글 수: 0
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Errorbars에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!