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:
As you can see, the g
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.
Another solution besides using zorder, and worth knowing: You can simply plot a scatter of points using the plot command. Something like plot(series2_x, series2_y, ' o')
. Note the ' o'
with a space means no lines but circle points. This way the order of plotting them on the axes does put them on top.
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)