python - 如何在matplotlib中先绘制线,最后绘制点

标签 python matplotlib

我有一个简单的情节,其中有几组点​​和连接每组的线。我希望将点绘制在线条的顶部(这样线条不会显示在点内)。不管 plotscatter 调用的顺序如何,这个图的结果都是一样的,而不是我想要的。有简单的方法吗?

import math
import matplotlib.pyplot as plt

def poisson(m):
    def f(k):
        e = math.e**(-m)
        f = math.factorial(k)
        g = m**k
        return g*e/f
    return f

R = range(20)
L = list()
means = (1,4,10)
for m in means:
    f = poisson(m)
    L.append([f(k) for k in R])
colors = ['r','b','purple']

for c,P in zip(colors,L):
    plt.plot(R,P,color='0.2',lw=1.5)
    plt.scatter(R,P,s=150,color=c)

ax = plt.axes()
ax.set_xlim(-0.5,20)
ax.set_ylim(-0.01,0.4)
plt.savefig('example.png')

最佳答案

您需要设置 Z 顺序。

plt.plot(R,P,color='0.2',lw=1.5, zorder=1)
plt.scatter(R,P,s=150,color=c, zorder=2)

看看这个例子。 http://matplotlib.sourceforge.net/examples/pylab_examples/zorder_demo.html

关于python - 如何在matplotlib中先绘制线,最后绘制点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2314379/

相关文章:

Python 为什么 100**0.5 == 4+6 是真的?

python - Google App Engine URL 处理程序运行时出错

python - sklearnpartial_fit() 没有像 fit() 显示准确的结果

python - 如何使用beautifulsoup分别获取节点文本和子标签

python - 在 Matplotlib 中删除楔形极坐标图周围的空间

matplotlib - Matplotlib 和 basemap : cannot import name 'dedent'

python - matplotlib:更改当前轴实例(即 gca())

python - 无法在 Jupyter Notebook 中导入 nltk 模块?

python - 根据频率范围重新限制 pyplot.psd y 轴图

python - 更改 python 2.7 中的 matplotlib.bar 顺序