简体   繁体   English

绘图的纵横比

[英]Aspect ratio of a plot

I try to change my aspect ratio in python.我尝试在 python 中更改我的纵横比。 I know this has been discussed in many other cases (eg with figaspect), but I don't get it like I want to have it.我知道这已经在许多其他情况下讨论过(例如使用 figaspect),但我不明白我想要它。 If I use figaspect, I get the error:如果我使用 figaspect,我会收到错误消息:

'Figure' object is not callable . 'Figure' object is not callable

Without it I have this code:没有它我有这个代码:

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

It works, but I need an other aspect ratio.它有效,但我需要其他宽高比。 How do I get a plot that is three times wider than high?如何获得比高度宽三倍的图?

There are a few options.有几个选项。 As suggested in the comments, you can increase the figure size manually, making sure the width is three time bigger than the height.正如评论中所建议的,您可以手动增加图形大小,确保宽度是高度的三倍。

fig, ax = plt.subplots(figsize=(height, height*3))

If you want to use figaspect you can do the following:如果您想使用figaspect您可以执行以下操作:

from matplotlib.figure import figaspect

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

plt.show()

This creates a figure which is 3 times wider than it is tall and gives:这将创建一个比它高 3 倍的图形,并给出:

在此处输入图片说明

You can also change the axes aspect ratio using ax.set_aspect() .您还可以使用ax.set_aspect()更改纵横比。 This will not change the figure size:不会改变图形大小:

fig, ax = plt.subplots()

ax.set_aspect(1/3)

plt.show()

Which gives:这使:

在此处输入图片说明

I think you're best helped with something like this:我认为你最好在这样的事情上得到帮助:

ax.set_aspect('auto')

You can find the documentation here This, however, changes the axis aspect ratio.您可以在此处找到文档 这会更改轴纵横比。 as @MaxPowers points out, you can also alter the figsize of your subplots.正如@MaxPowers 指出的那样,您还可以更改子图的figsize

you can specify the figure size directly to produce varied sizes as wanted.您可以直接指定图形大小以根据需要生成各种大小。

fig, axs = plt.subplots(2, 3, figsize=(10, 3), sharex=True, sharey=True, constrained_layout=True)

Hope this could help.希望这会有所帮助。

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

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