MATLAB: legend for plotyy with multiple data sets

落爺英雄遲暮 提交于 2020-01-05 09:03:15

问题


I'd like to automatically create graphs of Hardness H and Young's modulus E of samples as function of load L of indenter.

My goal is to create legend containing black circle marker labeled Sample one, black square labeled Sample two etc. Right now I get legend of default(blue) colour.

Preceeding line specifications are discussed there.

Here's my actual MWE code:

[m,n]=size(data1); %data1 - m x 3 matrix with data for first sample.
[ax,h1,h2]=plotyy([data1(1:m,1)],[data1(1:m,2)],[data1(1:m,1)],[data1(1:m,3)]);
  %plots 1st sample data

set(h1,'linestyle','o')
set(h2,'linestyle','o')
c1=get(h1,'color);c2=get(h2,'color'); %store colors

line('parent',ax(1),'xdata','[data2(1:m,1)],'ydata',[data2(1:m,2)],...
     'color',c1,'linestyle','s') %plots 2nd sample hardness
line('parent',ax(2),'xdata','[data2(1:m,1)],'ydata',[data2(1:m,3)],...
     'color',c2,'linestyle','s') %plots 2nd sample young's modulus

Thanks for any approach and/or correction.


回答1:


If you have the handle for one of each different elements, you can use legend with a vector of handle and a cell of legend strings.

Example:

figure
hold all
for i=1:3
    h(i) = plot([i i])
end
ylim([0 4])

legend([h([1 3])], {'aa', 'cc'})


来源:https://stackoverflow.com/questions/9328857/matlab-legend-for-plotyy-with-multiple-data-sets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!