Matplotlib控制哪个图在顶部 [英] 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:
如您所见,绿色系列位于蓝色系列的顶部,两个系列均位于黑点的顶部(这是我用散点图绘制的).我希望黑点位于两个系列(线条)的顶部.
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)
然后我尝试了以下
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.
在plot
函数上交换会影响在哪个图的顶部,但是无论我将scatter
函数放在何处,线条都在点的顶部.
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.
注意:
我在Windows 10上使用Python 3.5(此示例),但在Ubuntu上主要使用Python 3.4.
I am using Python 3.5 on Windows 10 (this example), but mostly Python 3.4 on Ubuntu.
注意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.
推荐答案
使用 zorder kwarg zorder越低,则图越靠后,例如
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)
这篇关于Matplotlib控制哪个图在顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!