python - 圆形变成椭圆形 - 在 Python 中设置长宽比

标签 python matplotlib plot aspect-ratio

我有一个绘制了一系列圆形的图表,但是,由于轴的原因,它们不是圆形,而是椭圆形,如下图所示。我知道这是一个反复出现的问题,并且有很多这样的问题......但是我找不到任何可以帮助我的东西!我尝试放入 fig = plt.figure(0, Figsize=(14.5, 1.75)) 这确实有一点帮助,但也许 ax.set_aspect() 但是,使用标量对此也没有多大帮助!

enter image description here

对于该图,标记为 *** 的线不存在

我的代码如下:

fig = plt.figure(0)
ax = fig.add_subplot(111)
ax.set_aspect(???)#*** not sure if this should be here or not
plt.axis([-5, 20, -1, 1])

circle1 = plt.Circle((-1,0.25), radius=0.2, color='c')
circle2= plt.Circle((4,-0.5), radius=0.5, color='m')

plt.gcf().gca().add_artist(circle1)
plt.gcf().gca().add_artist(circle2)

最佳答案

您可以将宽高比设置为相等,但您还需要为两个轴选择相似的尺寸,如下所示:

from matplotlib import pyplot as plt

fig = plt.figure(0)
ax = fig.add_subplot(111)
ax.set_aspect('equal')
plt.axis([-5, 5, -5, 5])

circle1 = plt.Circle((-1, 0.25), radius=0.2, color='c')
circle2 = plt.Circle((4, -0.5), radius=0.5, color='m')

ax.add_artist(circle1)
ax.add_artist(circle2)

plt.show()

显示如下: enter image description here

关于python - 圆形变成椭圆形 - 在 Python 中设置长宽比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33898598/

相关文章:

python - 绘制两个 pandas 数据框列之间差异的分布图

python - 使用 pyplot 中的箭袋函数绘制箭头的对数长度

tokenize 模块中的 Python 2 换行符

python - 如何删除 tkinter 中的菜单栏?

python - 预期的字符串或缓冲区(在 re.sub 中)

python - 如何使用点绘制 Pandas 数据框的两列

python - 如何为子图设置公共(public)轴标签

Python 绘制带注释的 3D 点

r - 带有多个回归线的 ggplot 以显示随机效应

python - 使用 Anaconda 在 OS X 上安装 pygraphviz 时出错