Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib control which plot is on top

I am wondering if there is a way to control which plot lies on top of other plots if one makes multiple plots on one axis. An 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 made with a scatter plot). I would like the black dots to be on top of both series (lines).

I first did the above with the following code

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 on that, but no luck.

Swapping around the plot functions has an effect on which plot 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 issue, but I have a case where the series on top of the dots are so dense that the colour of the dots get obscured, and in those cases I need my readers to clearly see which dots are what colour, hence why I need the dots to be on top.

like image 335
Konrad Avatar asked Mar 03 '16 19:03

Konrad


People also ask

How do I change the order of a plot in matplotlib?

You can change the order for individual artists by setting the zorder. Any individual plot() call can set a value for the zorder of that particular item. In the fist subplot below, the lines are drawn above the patch collection from the scatter, which is the default. In the subplot below, the order is reversed.

How do I change the position of a title in matplotlib?

Matplotlib can display plot titles centered, flush with the left side of a set of axes, and flush with the right side of a set of axes. Automatic positioning can be turned off by manually specifying the y keyword argument for the title or setting rcParams["axes. titley"] (default: None ) in the rcParams.

What does PLT show () do?

plt. show() starts an event loop, looks for all currently active figure objects, and opens one or more interactive windows that display your figure or figures. The plt. show() command does a lot under the hood, as it must interact with your system's interactive graphical backend.


2 Answers

Use the zorder kwarg where the lower the zorder the further back the plot, e.g.

plt.plot(series1_x, series1_y, zorder=1) plt.plot(series2_x, series2_y, zorder=2) plt.scatter(series2_x, series2_y, zorder=3) 
like image 141
mechanical_meat Avatar answered Sep 23 '22 21:09

mechanical_meat


Yes, you can. Just use zorder parameter. The higher the value, more on top the plot shall be.

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 do line and marker plot at the same time. You can even set different colors for line and marker face.

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') 

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

like image 25
jure Avatar answered Sep 24 '22 21:09

jure