绘图的纵横比

csi*_*csi 1 python matplotlib python-3.x

我尝试在 python 中更改我的纵横比。我知道这已经在许多其他情况下讨论过(例如使用 figaspect),但我并没有像我想要的那样得到它。如果我使用 figaspect,我会收到错误消息:

'Figure' object is not callable.

没有它我有这个代码:

fig, ax = plt.subplots()

ax.plot(y_test1, linewidth=0.5, linestyle="-", label='Test1')
ax.plot(y_test2, linewidth=0.5, linestyle="-", label='Test2') 
legend = ax.legend(loc='right center',prop={'size': 5}, shadow=True, fontsize='medium')
plt.xlabel('images')
plt.ylabel('error')
plt.grid()
plt.axis([0,100, 0, 20])
Run Code Online (Sandbox Code Playgroud)

它有效,但我需要其他宽高比。如何获得比高度宽三倍的图?

Dav*_*idG 5

有几个选项。正如评论中所建议的,您可以手动增加图形大小,确保宽度是高度的三倍。

fig, ax = plt.subplots(figsize=(height, height*3))
Run Code Online (Sandbox Code Playgroud)

如果要使用figaspect,可以执行以下操作:

from matplotlib.figure import figaspect

w, h = figaspect(1/3)
fig, ax = plt.subplots(figsize=(w,h))

plt.show()
Run Code Online (Sandbox Code Playgroud)

这将创建一个比它高 3 倍的图形,并给出:

在此处输入图片说明

您还可以使用 更改轴纵横比ax.set_aspect()。这不会改变图形大小:

fig, ax = plt.subplots()

ax.set_aspect(1/3)

plt.show()
Run Code Online (Sandbox Code Playgroud)

这使:

在此处输入图片说明