在matplotlib中绘制正方形等高线图

2024-04-26 00:42:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我不能让matplotlib生成x和y轴相等的等高线图。请注意,我尝试用equalfigsize(10,10)定义图形,并在axis函数中尝试“equal”。两种工作都没有,就像椭圆形的“圆圈”所示:

import matplotlib.pyplot as plt
from matplotlib.colors import BoundaryNorm
from matplotlib.ticker import MaxNLocator
import numpy as np

# generate 2 2d grids for the x & y bounds
y, x = np.mgrid[slice(0, 1 + 0.1, 0.1),
                slice(0, 1 + 0.1, 0.1)]

z = y + x
z = z[:-1, :-1]
levels = MaxNLocator(nbins=15).tick_values(z.min(), z.max())

# pick the desired colormap, sensible levels, and define a normalization
# instance which takes data values and translates those into levels.
plt.figure(num=None, figsize=(10,10))
cmap = plt.get_cmap('nipy_spectral')
norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)

# contours are *point* based plots, so convert our bound into point centers
plt.contour(x[:-1, :-1] + 0.1 / 2.,
            y[:-1, :-1] + 0.1 / 2., z, levels=levels,
            cmap=cmap, zorder=1)

plt.colorbar()
plt.axis([0, 1, 0, 1],'equal')
plt.tight_layout()

circle=plt.Circle((0.4,0.5),.1,color='k',fill=False)
plt.gca().add_artist(circle)

plt.savefig('not_square')

Tags: thefromimportmatplotlibasnpsliceplt

热门问题