为什么 matplotlib 将我的圆圈绘制为椭圆? [英] Why is matplotlib plotting my circles as ovals?

查看:48
本文介绍了为什么 matplotlib 将我的圆圈绘制为椭圆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让matplotlib绘制一个完美的圆?它们看起来更像椭圆形.

Is there a way to get matplotlib to plot a perfect circle? They look more like ovals.

推荐答案

只是为了扩展 DSM 的正确答案.默认情况下,图沿一个轴的像素要多于另一个轴.添加圆时,通常会以数据单位添加圆.如果您的轴具有对称范围,则意味着沿着x轴的一个步将包含与沿着y轴的一个步不同的像素数量.因此,数据单位的对称圆在您的像素单位(您实际看到的)中是不对称的.

Just to expand on DSM's correct answer. By default, plots have more pixels along one axis over the other. When you add a circle, it's traditionally added in data units. If your axes have a symmetric range, that means one step along the x axis will involve a different number of pixels than one step along your y axis. So a symmetric circle in data units is asymmetric in your Pixel units (what you actually see).

正如DSM正确指出的,您可以强制x和y轴每个数据单元具有相等数量的像素.这是通过 plt.axis("equal") ax.axis("equal")方法(其中 ax 是一个实例)完成的).

As DSM correctly pointed out, you can force the x and y axes to have equal number of pixels per data unit. This is done using the plt.axis("equal") or ax.axis("equal") methods (where ax is an instance of an Axes).

您还可以绘制一个 Ellipse 使其适当缩放以在您的绘图中看起来像一个圆圈.这是这种情况的示例:

You can also draw an Ellipse such that it is appropriately scaled to look like a circle on your plot. Here's an example of such a case:

import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse, Circle


fig = plt.figure()
ax1 = fig.add_subplot(211)
# calculate asymmetry of x and y axes:
x0, y0 = ax1.transAxes.transform((0, 0)) # lower left in pixels
x1, y1 = ax1.transAxes.transform((1, 1)) # upper right in pixes
dx = x1 - x0
dy = y1 - y0
maxd = max(dx, dy)
width = .15 * maxd / dx
height = .15 * maxd / dy

# a circle you expect to be a circle, but it is not
ax1.add_artist(Circle((.5, .5), .15))
# an ellipse you expect to be an ellipse, but it's a circle
ax1.add_artist(Ellipse((.75, .75), width, height))
ax2 = fig.add_subplot(212)

ax2.axis('equal')
# a circle you expect to be a circle, and it is
ax2.add_artist(Circle((.5, .5), .15))
# an ellipse you expect to be an ellipse, and it is
ax2.add_artist(Ellipse((.75, .75), width, height))

fig.savefig('perfectCircle1.png')

导致这个数字:

或者,您可以调整图形,使 Axes 为方形:

Alternatively, you can adjust your figure so that the Axes are square:

# calculate dimensions of axes 1 in figure units
x0, y0, dx, dy = ax1.get_position().bounds
maxd = max(dx, dy)
width = 6 * maxd / dx
height = 6 * maxd / dy

fig.set_size_inches((width, height))

fig.savefig('perfectCircle2.png')

导致:

请注意具有 axis("equal") 选项的第二个轴现在具有相同的 x 轴和 y 轴范围.该图已被缩放,以便每个的日期单位由相同数量的像素表示.

Notice how the second axes, which has the axis("equal") option, now has the same range for the x and y axes. The figure has been scaled so that the date units of each are represented by the same number of pixels.

您也可以将轴调整为方形,即使图形不是.或者,您可以将圆"的默认变换更改为 None ,这表示使用的单位是像素.我目前很难成功地做到这一点(圆圈是一个圆圈,但不是我想要的地方).

You can also adjust your axes to be square, even if the figure is not. Or you can change the default transform for the Circle to None, which means the units used are pixels. I'm having difficulty successfully doing this at the moment (the circle is a circle, but not where I want it to be).

这篇关于为什么 matplotlib 将我的圆圈绘制为椭圆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆