Matplotlib contourf复制线

2021-09-16 23:06:29 发布

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

我有一个I(72x72)矩阵,它表示二维空间中的一条矩形线,我想用下面的matplotlib代码将其作为概率分布图的交点覆盖:

# let mass be a logarithmic scale of masses (length = 72)
# let velocities be a linear scale of velocities (length = 72)
# let distribution be a 2D probability distribution (72x72)
# let contour be a 72x72 identity matrix

import matplotlib
from matplotlib import pyplot as plot
from mpl_toolkits.mplot3d import Axes3D

fig = plot.figure()
plot.title(title)
plot.xlabel('Velocities (km/s)')
plot.ylabel('Mass (grams), 10^X')
plot.axis([0, 40, -6.65, -5.00])
levels = np.linspace(0, 0.0018, 25)

cs_dist = plot.contourf(
    mass, velocities, distribution, alpha=0.95, levels=levels
)
fig.colorbar(cs_dist, format="%.5f")

cs_shield = plot.contour(
    mass, velocities, contour, 1,
    levels=[0], colors='w', linestyles='solid'
)
plot.clabel(cs_shield, inline=True)

plot.show()

此代码生成下图:

Contour intersection

我本以为只有一行,但有两行。。。我做错什么了?你知道吗