简体   繁体   English

在matplotlib中设置绘图的纵横比

[英]Set the aspect ratio of a plot in matplotlib

Using Matplotlib, I want to draw six plots side-by-side. 我想使用Matplotlib并排绘制六个图。 However, I want each plot to have an aspect ratio of 1. 但是,我希望每个图的宽高比均为1。

If I run the following: 如果我运行以下命令:

import matplotlib.pyplot as plt
fig = plt.figure()

for n in range(1, 6):
    fig.add_subplot(1, 6, n)
    plt.axis([0, 4, 0, 4])

plt.show()

Then it shows the six plots "squashed" along the x-axis. 然后显示沿x轴“压缩”的六个图。 This occurs even though I have set the x-axis and the y-axis to be the same length. 即使我将x轴和y轴设置为相同的长度,也会发生这种情况。

How can I make all the plots have an aspect ratio of 1? 如何使所有图的纵横比为1?

With 5 plots side by side, you must set the figure size to allow for enough space for your plots, and add a bit of padding between plots so the text labels of the axis of one subplot do not overlap the next plot. 对于并排的5个图,必须设置图形大小以为图留出足够的空间,并在图之间添加一些填充,以使一个子图的轴的文本标签不会与下一个图重叠。

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10,2))

for n in range(1, 6):
    ax = fig.add_subplot(1, 5, n)
    ax.set_aspect(1)
    plt.axis([0, 4, 0, 4])

plt.tight_layout(pad=1)

plt.show()

在此处输入图片说明

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

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