Matplotlib controls which chart is on top

I am wondering if there is a way to control which graph is on top of other graphs if you are making several graphs on the same axis. Example:

enter image description here

As you can see, the green series is on top of the blue series, and both series are on top of the black dots (which I did with the scatter). I would like the black dots to be on top of both lines (lines).

I did the above code first

plt.plot(series1_x, series1_y) plt.plot(series2_x, series2_y) plt.scatter(series2_x, series2_y) 

Then I tried the following

 fig = plt.figure() ax1 = fig.add_subplot(111) ax1.plot(series1_x, series1_y) ax2 = fig.add_subplot(111) ax2.plot(series2_x, series2_y) ax3 = fig.add_subplot(111) ax3.scatter(series2_x, series2_y) 

And some variations of this, but not luck.

Switching around plot functions affects which graph is on top, but no matter where I put the scatter function, the lines are on top of the dots.

Note:

I am using Python 3.5 on Windows 10 (this example), but mostly Python 3.4 on Ubuntu.

NOTE 2:

I know this may seem like a trivial problem, but I have a case where the series on top of the dots is so dense that the color of the dots becomes unclear, and in these cases I need my readers to clearly see which dots are what color , therefore, why I need points that should be on top.

+7
python matplotlib plot
source share
3 answers

Use zorder kwarg , where the lower zone reduces the further back chart, for example

 plt.plot(series1_x, series1_y, zorder=1) plt.plot(series2_x, series2_y, zorder=2) plt.scatter(series2_x, series2_y, zorder=3) 
+12
source share

Another solution, besides using zorder, is worth knowing: you can simply draw a spread of points using the plot command. Something like plot(series2_x, series2_y, ' o') . Note that a ' o' with a space means no lines, but no dots. Thus, the order of their construction along the axes places them on top.

+1
source share

Yes, you can. Just use the zorder parameter. The higher the value, the more on the top of the graph.

 fig = plt.figure() ax1 = fig.add_subplot(111) ax1.plot(series1_x, series1_y, zorder=3) ax2 = fig.add_subplot(111) ax2.plot(series2_x, series2_y, zorder=4) ax3 = fig.add_subplot(111) ax3.scatter(series2_x, series2_y, zorder=5) 

Alternatively, you can simultaneously create a line graph and a marker. You can even set different colors for the line and face of the marker.

 fig = plt.figure() ax1 = fig.add_subplot(111) ax1.plot(series1_x, series1_y) ax2 = fig.add_subplot(111) ax2.plot(series2_x, series2_y, '-o', color='b', mfc='k') 

'-o' sets the build style to line and round markers, color='b' sets the line color to blue, and mfc='k' sets the marker face color to black.

+1
source share

All Articles