如何在matplotlib中首先绘制线条并最后绘制点 [英] How to plot the lines first and points last in matplotlib

查看:152
本文介绍了如何在matplotlib中首先绘制线条并最后绘制点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的绘图,其中包含多组点和线,每组点和线连接在一起.我希望将点绘制在线条的顶部(这样线条就不会显示在该点的内部).不管plotscatter调用的顺序如何,此图都一样,而不是我想要的.有简单的方法吗?

I have a simple plot with several sets of points and lines connecting each set. I want the points to be plotted on top of the lines (so that the line doesn't show inside the point). Regardless of order of the plot and scatter calls, this plot comes out the same, and not as I'd like. Is there a simple way to do it?

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

这篇关于如何在matplotlib中首先绘制线条并最后绘制点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆