使用matplotlib.patches

时间:2018-09-14 23:28:55

标签: python matplotlib patch

我正在使用matplotlib.patches生成一个圆。但是,生成的圆在视觉上不是圆形的。它看起来像一个椭圆。这可能是由于某些纵横比设置所致,我无法使用补丁弄清楚。如何使用matplotlib.patches模块获得视觉上圆形的圆圈?

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.path as mpath
import matplotlib.lines as mlines
import matplotlib.patches as mpatches
from matplotlib.collections import PatchCollection

fig, ax = plt.subplots()
grid = np.mgrid[0.2:0.8:3j, 0.2:0.8:3j].reshape(2,-1).T

patches = []

circle1 = mpatches.Circle(grid[4], 0.25, linestyle='-.', fill=False)

ax.add_patch(circle1)

ax.grid(color='r', linestyle='-.', linewidth=1)

# plt.subplots_adjust(left=0.1, bottom=0.1, right=0.5, top=0.5,
#                 wspace=0.1, hspace=0.1)
# plt.tight_layout()
plt.show()

1 个答案:

答案 0 :(得分:1)

如果希望数据坐标中的圆在屏幕坐标中显示为圆形,则需要将轴的纵横比设置为相等。

您可以选择

ax.set_aspect("equal")

让轴缩放。

enter image description here

或者您可以选择

ax.set_aspect("equal", adjustable="datalim")

让数据扩展

enter image description here

如果您想直接在屏幕坐标中创建一个圆,则可以使用散点图。

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.scatter([.5], [.5], s=10000, edgecolor="k", facecolor="none")
ax.grid(color='r', linestyle='-.', linewidth=1)
ax.axis([0,1,0,1])

plt.show()

enter image description here

相关问题