简体   繁体   English

Matplotlib Contourf复制线

[英]Matplotlib contourf duplicating lines

I have a following I(72x72) matrix that represents a rect line in a 2D space and I want to overlay it as an intersection with a plot of a probability distribution using the following matplotlib code: 我有一个下面的I(72x72)矩阵,它表示2D空间中的一条矩形线,我想使用以下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()

This code generates the following figure: 此代码生成下图:

轮廓交点

I was expecting a single line, but two of them appear... what am I doing wrong? 我本来期望一行,但其中有两个出现...我在做什么错?

Since I can't execute your example (for the future, you could make it runnable by constructing some fake dataset, it just had to give the same two line behavior), I modified it to be self-contained. 由于我无法执行您的示例(将来,您可以通过构建一些假数据集使其可运行,因此只需给出相同的两行行为即可),因此我将其修改为独立的。 I do not see this bug with matplotlib 1.5.1: 我没有在matplotlib 1.5.1中看到此错误:

import numpy

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

fig = plot.figure()
s = fig.add_subplot(1, 1, 1)
s.set_title('Shielding protection')
s.set_xlabel('Velocities (km/s)')
s.set_ylabel('Mass (grams), 10^X')
levels = numpy.linspace(0, 1, 25)

N = 100
m = numpy.linspace(-6.65, -5, N)
v = numpy.linspace(0, 40, N)
mass, velocities = numpy.meshgrid(m, v)
distribution = numpy.exp(-(mass + 6)**2  - (velocities - 20)**2 / 10)

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

contour = (mass - 6) + (velocities - 20)

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

fig.savefig('t.png')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM