如何绘制非正方形的轮廓填充图

2 投票
3 回答
2746 浏览
提问于 2025-04-16 14:19

我在pyplot中使用contourf来绘制一些标量数据,但当我的数据区域不是正方形时,我觉得数据的表现不太准确,因为它总是以正方形的形式绘制(尽管某一边的轴值会增长得更快)。我该如何强制让坐标轴的比例相等,这样如果我的数据区域在x方向上长了两倍,图像就能真实地以一个长方形的形式绘制出来呢?

我正在做类似这样的事情:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
contour = ax.contourf(X,Y,Z)
fig.colorbar(contour)
fig.canvas.draw()

3 个回答

0

你需要调整坐标轴的设置:

axis('equal')

你可以在这里查看所有的坐标轴设置:

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axis

5

使用 ax.set_aspect

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111)
x=np.r_[-10:10:100j]
y=np.r_[-20:20:100j]
z= np.add.outer(x*x, y*y)
contour=ax.contour(x,y,z)
fig.colorbar(contour)
ax.set_aspect('equal')
# ax.axis('equal')
plt.show()

得到的结果是

enter image description here

而将 ax.set_aspect('equal') 改为

ax.axis('equal')

得到的结果是

enter image description here

4

这可能会对你有帮助:

ax = fig.add_subplot(111, aspect="equal")

撰写回答