简体   繁体   English

如何使用图例更改绘图的大小?

[英]How to change the size of a plot with a legend?

My legend gets cut off:我的传奇被切断了:

图例被切断的截图

If I change如果我改变

plt.figure(figsize=(6.29, 3.54))

to

plt.figure(figsize=(6.29, 8))

It looks like this:它看起来像这样:

在此处输入图片说明

But I want that the plot stays the same size, it just should go longer vertically so that the full legend will be shown.但我希望情节保持相同的大小,它应该垂直更长,以便显示完整的图例。

What can I do?我能做什么?

this is what I got for running the "almost" same code which shows all the legend:这就是我运行“几乎”相同的代码所得到的结果,该代码显示了所有图例:

plt.figure(figsize=(6.29, 3.54))
dev_y1= np.array([112.95, 117.62, 43.67, 85.64,51.37])
dev_y2= np.array([21.81 , 42.34, 23.82, 61.67,46.23])
dev4x4real = np.array([30.63 ,61.19 , 35.71, 90.53 , 60.33])
dev_x2= np.array([1,2,3,4,5])

plt.plot(dev_x2,dev_y1, 'ro')
#popt, pcov = curve_fit(func, dev_x2, dev_y1)
xFit= np.arange(0.0, 5, 0.01)

plt.plot(dev_y1, dev_y2,color='r', linestyle='-',label=f'Ideales DOE 125 \u03bcJ <= 0,3 \u03bcm F(x) = {round(1)} * e^({round(1)}*x)') 
plt.plot(dev_y1, dev_y2,color='r', linestyle='-',label=f'Ideales DOE 125 \u03bcJ <= 0,3 \u03bcm F(x) = {round(1)} * e^({round(1)}*x)') 
plt.plot(dev_y1, dev_y2,color='r', linestyle='-',label=f'Ideales DOE 125 \u03bcJ <= 0,3 \u03bcm F(x) = {round(1)} * e^({round(1)}*x)') 
plt.ylabel('Herstellungsrate: Mikrostrukturen pro Sekunde')

plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),
           shadow=True, ncol=1,)

plt.show()

在此处输入图片说明

Essentially, you have to play with bbox_to_anchor keyboard argument in order to place your legend outside of your axes and add a convenient space.本质上,您必须使用bbox_to_anchor键盘参数才能将图例放置在轴之外并添加方便的空间。
See How to put the legend out of the plot请参阅如何将图例排除在情节之外

plt.figure(figsize=(6.29, 3.54))
dev_y1= np.array([112.95, 117.62, 43.67, 85.64,51.37])
dev_y2= np.array([21.81 , 42.34, 23.82, 61.67,46.23])
dev4x4real = np.array([30.63 ,61.19 , 35.71, 90.53 , 60.33])
dev_x2= np.array([1,2,3,4,5])

plt.plot(dev_x2,dev_y1, 'ro')
popt, pcov = curve_fit(func, dev_x2, dev_y1)
xFit= np.arange(0.0, 5, 0.01)

plt.plot(xFit, func(xFit,*popt),color='r', linestyle='-',label=f'Ideales DOE 125 \u03bcJ <= 0,3 \u03bcm F(x) = {round(popt[0])} * e^({round(popt[1])}*x)') 
plt.ylabel('Herstellungsrate: Mikrostrukturen pro Sekunde')

plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),
           shadow=True, ncol=1,)

plt.show()

This is my code这是我的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM