[英]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)
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. 在
plot
函数周围进行交换会影响哪个绘图位于顶部,但无论在哪里放置scatter
函数,线都位于点的顶部。
NOTE: 注意:
I am using Python 3.5 on Windows 10 (this example), but mostly Python 3.4 on Ubuntu. 我在Windows 10(本例)中使用Python 3.5,但在Ubuntu上主要使用Python 3.4。
NOTE 2: 笔记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. 我知道这可能看起来像一个微不足道的问题,但我有一个案例,在点上的系列是如此密集,以至于点的颜色变得模糊,在这些情况下我需要我的读者清楚地看到哪些点是什么颜色,因此我需要点在顶部。
Use the zorder kwarg where the lower the zorder the further back the plot, eg 使用zorder kwarg ,其中zorder越低,越靠后的情节,例如
plt.plot(series1_x, series1_y, zorder=1)
plt.plot(series2_x, series2_y, zorder=2)
plt.scatter(series2_x, series2_y, zorder=3)
Yes, you can. 是的你可以。 Just use
zorder
parameter. 只需使用
zorder
参数。 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. '-o'
将绘图样式设置为线和圆标记, color='b'
将线颜色设置为蓝色, mfc='k'
将标记面颜色设置为黑色。
Another solution besides using zorder, and worth knowing: You can simply plot a scatter of points using the plot command. 除了使用zorder之外的另一种解决方案,值得了解:您可以使用plot命令简单地绘制点的散点图。 Something like
plot(series2_x, series2_y, ' o')
. 像
plot(series2_x, series2_y, ' o')
。 Note the ' o'
with a space means no lines but circle points. 注意带有空格的
' o'
表示没有线而是圆点。 This way the order of plotting them on the axes does put them on top. 这样,在轴上绘制它们的顺序确实将它们放在顶部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.