Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot the lines first and points last in matplotlib

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') 
like image 226
telliott99 Avatar asked Feb 22 '10 21:02

telliott99


People also ask

How do I plot a linear line in Matplotlib?

Matplotlib plot a linear function You can use the slope-intercept form of the line that is y = m * x + c; Here, x and y are the X-axis and Y-axis variables respectively, m is the slope of the line, and c is the x-intercept of the line.


1 Answers

You need to set the Z-order.

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

Check out this example. http://matplotlib.sourceforge.net/examples/pylab_examples/zorder_demo.html

like image 157
pdemarest Avatar answered Sep 18 '22 11:09

pdemarest