框中的图例不居中

时间:2016-09-18 12:25:32

标签: matlab plot legend

问题

我使用plotyy创建的绘图的图例不在其框中居中,如图所示:

enter image description here

这是第一次发生在我身上,并且在导出到各种形式之后问题仍然存在。我正在使用Matlab R2016a。

代码

我使用以下代码绘制图例:

a=sprintf('Test')
b=sprintf('Test.\nTest Test')
c=sprintf('Test\nTest')
d=sprintf('TestTest\nTest')
e=sprintf('Test\nTest')
f=sprintf('Test\nTest Test')
hLegend=legend([l1,l2,r1,r2,r3,r4], a, b, c, d, e, f);
set([gca,hXLabel,hYLabel,hLegend] , 'FontName'   , 'Helvetica','FontSize', 8) 
set(hLegend,'Fontsize',8,'Location', 'southoutside', 'Orientation','horizontal')

因此问题,是否有人已经有这个问题?有没有办法解决它或者我可以手动中心传奇?

可执行代码示例

h=figure
x=[1:10]
y=[1:10]
hold on
yyaxis left
l1=plot(x,y)
l2=plot(x,2*y)
hYLabel=ylabel('Test') 

yyaxis right
r1=plot(x,y.^2)
r2=plot(x,y.^3)
r3=plot(x,10*y)
r4=plot(x,20*y)


hYLabel=ylabel('Test2')
hXLabel = xlabel('TestTest]');
a=sprintf('Test')
b=sprintf('Test.\nTest Test')
c=sprintf('Test\nTest')
d=sprintf('Test\nTest')
e=sprintf('Test\nTest')
f=sprintf('TestTest\nTest')
hLegend=legend([l1,l2,r1,r2,r3,r4], a, b, c, d, e, f);
set([gca,hXLabel,hYLabel,hLegend] , 'FontName'   , 'Helvetica','FontSize', 8) 
set(hLegend,'Fontsize',8,'Location', 'southoutside', 'Orientation','horizontal')
set(gca,'LineWidth',1.0)
set(l1, 'LineWidth',1.5,'LineStyle','-')
set(l2, 'LineWidth',1.5,'LineStyle','-.')
set(r1, 'LineWidth',1.5,'LineStyle','-')
set(r2, 'LineWidth',1.5,'LineStyle','-.')
set(r3, 'LineWidth',1.5)
set(r4, 'LineWidth',1.5,'LineStyle','- -')

hold off

虽然没有显示使用的原始数据,但使用上面的示例我遇到了同样的问题。 非常感谢!

1 个答案:

答案 0 :(得分:1)

我猜测问题是<html xmlns="http://www.w3.org/1999/xhtml" xmlns:m="http://www.w3.org/1998/Math/MathML"> <head> <script type="text/javascript" async="async" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=MML_CHTML"> </script> </head> <body> <h3>Quadratic Equation</h3> <math> <m:mi>x</m:mi> <m:mo>=</m:mo> <m:mrow> <m:mfrac> <m:mrow> <m:mo>&#x2212;</m:mo> <m:mi>b</m:mi> <m:mo>&#x00B1;</m:mo> <m:msqrt> <m:msup><m:mi>b</m:mi><mn>2</mn></m:msup> <m:mo>&#x2212;</m:mo> <mn>4</mn><m:mi>a</m:mi><m:mi>c</m:mi> </m:msqrt> </m:mrow> <m:mrow> <mn>2</mn><m:mi>a</m:mi> </m:mrow> </m:mfrac> </m:mrow> </math> </body> </html> 是一行,而a=sprintf('Test')之类的其他行是多行。

当所有图例条目都是两个文本行时,文本正确居中。

解决方案:将b=sprintf('Test.\nTest Test')替换为a=sprintf('Test') 当第二行是空白字符时,这会使a=sprintf('Test\n\0.')两个文本行。

enter image description here