【问题标题】:How to change the size of a plot with a legend?如何使用图例更改绘图的大小?
【发布时间】:2022-01-19 01:27:57
【问题描述】:

我的传奇被切断了:

如果我改变了

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

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

看起来像这样:

但我希望绘图保持相同的大小,它应该在垂直方向上更长一些,以便显示完整的图例。

我能做什么?

【问题讨论】:

  • 您可以添加将图例移动到该位置的绘图代码吗?
  • plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), shadow=True, ncol=1,)

标签: python matplotlib


【解决方案1】:

基本上,您必须使用 bbox_to_anchor 键盘参数才能将图例放置在轴之外并添加方便的空间。
How to put the legend out of the plot

【讨论】:

  • 已经检查
  • 这甚至不是我的问题......我的问题是我的传奇正在被切断
  • 在这种情况下,像这样保存你的图: fig.savefig('my_image.png',format='png', bbox_inches='tight') 它将阻止裁剪图例。
【解决方案2】:

这是我运行显示所有图例的“几乎”相同的代码所得到的:

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()

【讨论】:

  • 你运行的是什么版本的 matplotlib
  • 3.3.4是版本
  • 我的是 3.4.3 也许这就是为什么?
  • 不,但下面的帖子刚刚解决了我的问题
猜你喜欢
  • 2011-10-30
  • 2017-04-01
  • 2021-11-24
  • 2021-04-17
  • 1970-01-01
  • 2018-07-28
相关资源
最近更新 更多