如何传递一系列线规格或样式进行绘制? [英] How do I pass an array of line specifications or styles to plot?
问题描述
我想一次调用plot()
绘制多条线,每条线具有不同的线型.这是一个示例:
I want to plot multiple lines with one call to plot()
, with different line styles for each line. Here's an example:
两者
plot([1,2,3]', [4,5;6,7;8,9], {'-o', '-x'})
和
hs = plot([1,2,3]', [4,5;6,7;8,9])
set(hs, 'LineStyle', {'--'; '-'})
不起作用.我已经尝试了一大堆带有方括号和花括号的奥术组合,但似乎无济于事.
don't work. I've tried a whole bunch of arcane combinations with square and curly braces, but nothing seems to do the trick.
I know it's possible to loop through the columns in Y and call plot()
for each one (like in this question), but that isn't what I'm after. I would really like to avoid using a loop here if possible.
谢谢.
PS:我找到了这个'prettyPlot'脚本,说它可以做这样的事情,但我想知道是否有任何内置的方法.
PS: I found this 'prettyPlot' script which says it can do something like this, but I want to know if there's any built-in way of doing this.
PPS:对于任何想要快速解决此问题的人,请尝试以下方法:
PPS: For anyone who wants a quick solution to this, try this:
for i = 1:length(hs)
set(hs(i), 'Marker', markers{i});
set(hs(i), 'LineStyle', linestyles{i});
end
例如与markers = {'+','o','*','.','x','s','d','^','v','>','<','p','h'}
推荐答案
引用 http://www.mathworks.com/help/matlab/ref/plot.html ,这是如何使用一个绘图命令绘制多条线的方法:
Referring to http://www.mathworks.com/help/matlab/ref/plot.html, this is how to draw multiple lines with a single plot command:
plot(X1,Y1,LineSpec1,...,Xn,Yn,LineSpecn)
所以您对
plot([1,2,3]', [4,5;6,7;8,9], {'-o', '-x'})
必须写为:
plot([1,2,3]', [4,6,8], '-o', [1,2,3]',[5,7,9],'-x')
结果:
将输入参数重新组织到单元格数组中,并使用cellfun将plot命令应用于每个单元格元素.
Reorganize input parameters into cell arrays and use cellfun to apply plot command to each cell element.
x = [1,2,3]';
xdata = {x;x};
ydata = {[4,6,8];[5,7,9]};
lspec = {'-o';'-x'};
hold all;
cellfun(@plot,xdata,ydata,lspec);
这篇关于如何传递一系列线规格或样式进行绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!