English 中文(简体)
MATLAB 图形图图密图: 在绘图过程中指定图例标签
原标题:MATLAB graph plotting: assigning legend labels during plot

我在用典型的 MATLAB 散射图格式绘制数据。 通常, 在绘制多个数据集时, 我会使用命令抓住; 然后绘制每个数据, 然后绘制我的图例 :

legend( DataSet1 ,  DataSet2 ) % etcetera

然而,我在同一轴上绘制的( 多个) 数据集并不一定每次都是相同的数据集。 我正在同一轴上绘制多达六套不同的数据集, 并且这些数据集可能存在任何组合( 取决于用户选择显示什么 ) 。 显然, 如果我想用传统的方式设置图例的话, 就会有很多其他的数据集 。

我真正想要做的是指定每个 DataSet 的名称 , 因为它被绘制为 , 这样之后我就可以拨打所有显示数据的传说 。

...或者,任何其它 解决这个问题的解决方案, 任何人都可以想到...?

最佳回答

其中一个选项是利用 UseData 属性,例如:

figure;
hold on
plot([0 1], [1 0],  -b ,  userdata ,  blue line )
plot([1 0], [1 0],  --r ,  userdata ,  red dashes )

% legend(get(get(gca,  children ),  userdata ))                      % wrong
legend(get(gca,  children ), get(get(gca,  children ),  userdata ))  % correct

编辑 : 正如提问者指出的, 原始版本可能会出错 。 要解决这个问题, 请指定哪个控点使用哪个标签( 在固定版本中, 它的顺序是正确的 ) 。

问题回答

您应该能够为每个绘图设置“ 显示” 属性 :

figure
hold on
plot(..., DisplayName , DataSet1 )
plot(..., DisplayName , DataSet2 )
legend(gca, show )

http://www.mathworks.com/help/matlab/ref/line_props.html" rel=“noreferrer'>http://www.mathworks.com/help/matlab/ref/line_props.html

备注:我找到了许多这样的小把戏, 通过让数字看得像我想要的一样, 然后选择图表 s “ 文件” 菜单选项“ Generate M-File... ” 并检查生成的输出代码 。

使用 显示Name 作为 plot () 属性, 并称您的传说为

legend( -DynamicLegend );

我的代码看起来是这样的:

x = 0:h:xmax;                                  %// get an array of x-values
y = someFunction;                              %// function
plot(x, y,  DisplayName ,  Function plot 1 );  %// plot with  DisplayName  property

legend( -DynamicLegend ,2);                    %//  -DynamicLegend  legend

资料来源:http://uncracedmatlab.com/blog/legend-semi-有记录-feature/

你可以尝试一些类似以下的尝试

for k = 1:10

   h(k) = plot(...);
   name{k} = [ condition   num2str(k)];

end

legend(h, name);

做一个循环。但是在循环之前,做一个阵列。

%for example 

legendset = {}

for i = 1:10 

%blabla
%Then in the fore loop say: 

legendset = [legendset;namedata(i)]

%It puts all names in a column of legendset. 
%Make sure namedata are characters. 

%foreloop ends
end

%Then after the foreloop say: 

legend(legendset). 




相关问题
MATLAB Solving equations problem

I want to solve these equations using MATLAB and I am sure there is a non zero solution. The equations are: 0.7071*x + 0.7071*z = x -0.5*x + 0.7071*y + 0.5*z = y -0.5*x - 0.7071*y +...

Difference between MATLAB s matrix notations

How do you read the following MATLAB codes? #1 K>> [p,d]=eig(A) // Not sure about the syntax. p = 0.5257 -0.8507 -0.8507 -0.5257 d = ...

preventing data from being freed when vector goes out of scope

Is there a way to transfer ownership of the data contained in a std::vector (pointed to by, say T*data) into another construct, preventing having "data" become a dangling pointer after the vector goes ...

Divide two polynomials using MATLAB

I want to divide p(x) by q(x) given that: p(x)=-5x^4+3x^2-6x q(x)=x^2+1 I tried: p=inline( -5*(x^4)+3*(x^2) , x ) p = Inline function: p(x) = -5*(x^4)+3*(x^2) q=inline( x^2+1 , x ) q = ...

matlab deals with Fibbonacci

The Fibonacci series is given as follows: 1, 2, 3, 5, 8, 13, 21, ... How can I write a script which calculates and prints the n-th Fibonacci term (for n>2), where n is inputed by the user. This ...

How do I plot lines between all points in a vector?

I have a vector containing some points in 2-D space. I want MATLAB to plot these points with lines drawn from every point to every other point. Basically, I want a graph with all vertices connected. ...

How do I create a string using a loop variable in MATLAB?

I have a loop like this: for i=1:no %some calculations fid = fopen( c:\out.txt , wt ); %write something to the file fclose(fid); end I want data to be written to different files like ...

热门标签