How to customize a legend in matlab plot window

泪湿孤枕 提交于 2019-12-24 02:56:31

问题


As shown in the image below, the legend is too long for the plot window. what I want to do is:

1- To know how to split the legend over a second line?

2- To know how to shorten the red line in indicated in the legend. As you see, the legend contains a red line then (x1=......), I want to know whether there is any possibility to shorten that red line or to control its length.


回答1:


There are two possibilities to control the content of the legend:

  1. Use of TeX commands: MATLAB parses the legend entries (and titles, labels etc.) with TeX. You can use most TeX features and symbols. You find some details in the MATLAB help pages, in the Interpreter section.

  2. sprintf: The sprintf function works like all printf functions and outputs a string. It allows you to add control characters like \n, insert variables etc. Have a look at the documentation for details.

In your case, you can use either of the following code snippets:

legend('x1 = x = ... \newline ...');
legend(sprintf('x1 = x = ... \n, ...'));

On your second question: I'm not aware of any possibility to change that line. Sorry.




回答2:


As for you second question - you can call legend with 2 output arguments:

[h,att] = legend('show')

And then use att.XData property to manipulate this line.

plot(sin(-pi:0.1:pi),'r')
[h,att] = legend('show')
att(2).XData(2) = 0.2;

The result:



来源:https://stackoverflow.com/questions/27686180/how-to-customize-a-legend-in-matlab-plot-window

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