Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

python - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...