如何自定义matlab图例中的图片?
Posted
技术标签:
【中文标题】如何自定义matlab图例中的图片?【英文标题】:How do I customize the picture in matlab legends? 【发布时间】:2018-11-01 17:49:08 【问题描述】:我想用图例绘制平面和曲面的等高线图。在同一个图中绘制两个曲面会创建相同的图例。我想更改图例中生成的省略号。我可以在图例图表上绘制平行线而不是椭圆吗?
这是一个示例源代码:
[X,Y] = meshgrid(-3:.1:3);
Z1 = peaks(X,Y);
Z2 = 2*X+3*Y+4;
contour(X,Y,Z1)
colormap jet
shading interp
axis([-3 3 -3 3])
hold on
contour(X,Y,Z2)
legend('surface','plane')
【问题讨论】:
您希望图例显示六个元素(图表上的每条线一个),还是 surface 一条线和 plane条线> ?而在第二种情况下,如何确定线条的颜色? 表面的实际图例没问题,无需更改。我希望将第二个图例修改为看起来像平面(具有不同颜色的平行线)。我不希望图例上有六行,因为它对于投影到 XY 的平面没有意义。线条的颜色可以是随机的。 【参考方案1】:老实说,没有什么好的方法可以让图像出现在前面。 我可以向您展示一些解决方法,但您必须具体说明。 最简单的事情是,在文本前面放置一个颜色条或一个矩形框(1 种颜色)。这些是数据类型/图例条目的标准选项。
我建议您从给表面/计数手柄开始。
h1=surf(....);
h2=plot(....);
lgd=legend([h1, h2, ....],[entries']);
对于前面有白色的文字,你可以这样做
h_separator1 = plot(NaN,NaN,'.','Color',[1 1 1]);
lgd=legend([h_separator1],['text']);
这是一个你可以运行的例子,让你做我刚才说的事情
clear all; % just for ease of quickly plotting this
close all; %closing all figures
myc=[1 1 1; 0 0 0; 1 1 1; 0 0 0; 1 1 1]; %this is want we will use to draw the paralel lines, can be of any color, just replace the zeros, with the respective values of the colors you want, 0 0 0 is black
x = [0 0 0 0]; %making it have 0 area and thus invisible
y = [0 0 0 0];
c = [0 0.33 0.66 1]; %lets you add a colorbar
figure
colormap(myc); %update the figure to use your colormap
hold on
h3 = plot(NaN,NaN,'Color','none'); %blank entry
h4 = plot(NaN,NaN,':'); % entry with dotted line, color "pseudo random"
h1=patch(x,y,'red','EdgeColor','none'); %For a rectangular color entry in legend
h2=patch(x,y,c,'EdgeColor','none'); %lets you add the colorbar, later use to place inside the legend as paralel lines
[lgd,OBJH,OUTH,OUTM]=legend([h1,h3,h2,h4],'HI your text here','Nothing','paralel lines','line'); %the lgd handle let's you later modify properties of the legend
hcb=colorbar; %the colorbar can still be modified, to have no number or a different scale, color, etc.
hcm=OBJH(5)
xlim([0 1])
ylim([0 1])
lpos=lgd.Position; % get position legend
lnr=numel(OUTH); %legend entries is useful
lhstp=lpos(4)/(lnr+1); %heigth step
hax=gca;
axpos=hax.Position; %to shift position because the colorbar is placed in the figure and not within the axes in comparison to the legend
% placing at by example 3rd entry
wdentry=0.04; %at the moment trial and error depends on width on legend box which is based on amount of characters and text size.
p1=axpos(1)+lpos(1)+0.01;
p2=lpos(2)+3/2*lhstp;
p3=wdentry;
p4=lhstp-0.01;
hcb.TickLabels=[]; %remove tick labels
hcb.Ticks=[]; %remove ticks
hcb.Color=[1 1 1]; %white border around it to make it "semi-invisible"
hcb.Position=[p1 p2 p3 p4];
【讨论】:
你也可以顺便看看这个文件:nl.mathworks.com/matlabcentral/fileexchange/55053-iconed-legend以上是关于如何自定义matlab图例中的图片?的主要内容,如果未能解决你的问题,请参考以下文章