简体   繁体   English

Matplotlib:条上方的网格线

[英]Matplotlib: grid lines above bars

This is a simple code that reproduces the kind of graph i`m trying to plot: 这是一个简单的代码,再现了我试图绘制的图形:

import matplotlib.pyplot as plt
import seaborn as sb
import numpy as np

x  = np.arange(1,11)
y1 = x[::-1]
y2 = 2 ** x

fig  = plt.figure(figsize = (8,6))
ax1 = fig.add_subplot(111)
plt.bar(x, y1, 1, color = 'blue')
ax1.grid(axis = 'y')
ax2 = ax1.twinx()
ax2.plot(x+0.5, y2, color = 'red')

Which produces: 产生:

在此处输入图片说明

The grid lines from the last line plot are appearing above the bars. 最后一条线图中的网格线显示在条形上方。 How can I put them behind the bars? 我怎样才能把它们关在牢里?

import matplotlib.pyplot as plt
import numpy as np

x  = np.arange(1,11)
y1 = x[::-1]
y2 = 2 ** x

fig  = plt.figure(figsize = (8,6))
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()

# from back to front
ax1.grid(axis="both", color="black")
ax1.bar(x, y1, 1, alpha=.9, edgecolor="black", facecolor='blue', zorder=1)

ax2.grid(axis="y", color="green")
#ax2.grid(False)
ax2.plot(x+0.5, y2, color='red')

plt.show()

which produce the below graph which illustrate the zorder of the elements. 下图显示了元素的zorder。

ax1.grid => ax1.bar => ax2.grid => ax2.line ax1.grid => ax1.bar => ax2.grid => ax2.line

图中元素的zorder

the only workaround i can think of is to plot the grid in ax1 and turn off ax2 grid (if you really want to plot the horizontal grid line for ax2, then you might have to plot the line with ax1. 我能想到的唯一解决方法是在ax1中绘制网格并关闭ax2网格(如果您确实要绘制ax2的水平网格线,那么您可能必须使用ax1绘制该线。

ax1.grid(True, axis="both", color="yellow")
ax1.bar(x, y1, 1, alpha=.9, edgecolor="black", facecolor='blue', zorder=1)

ax2.grid(False)
ax2.plot(x+0.5, y2, color='red')

一些解决方法

After some searches and tries I'm sharing the solution I found, so others can use it as a reference in the future. 经过一些搜索并尝试后,我将分享我找到的解决方案,以便其他人将来可以将其用作参考。 This is not exactly what I was looking for, since I would like horizontal grid lines just for the right axis, however, it provided a much better visualization than the original plot. 这并不是我一直在寻找的,因为我希望水平网格线仅用于右轴,但是,它提供了比原始图更好的可视化效果。

From this , i found out how to align the left and right axis ticks (this problem also happens with line plots, but the effect is not as disturbing as in a bar plot). 由此 ,我发现了如何对齐左右轴刻度(折线图也会发生此问题,但效果不像条形图那样令人不安)。

import matplotlib.pyplot as plt
import seaborn as sb
import numpy as np

x  = np.arange(1,11)
y1 = x[::-1]
y2 = 2 ** x

fig  = plt.figure(figsize = (8,6))
ax1 = fig.add_subplot(111)
plt.bar(x, y1, 1, color = 'blue')
ax2 = ax1.twinx()
ax2.plot(x+0.5, y2, color = 'red')
ax2.set_yticks(np.linspace(ax2.get_yticks()[0],ax2.get_yticks()[-1],len(ax1.get_yticks())))

在此处输入图片说明

The grid lines are still above the bars. 网格线仍在条形上方。 However, since the tick marks are aligned, we can now remove the grid of the second line plot by adding the explicit command: ax2.grid(False) at the end of the previous code. 但是,由于刻度线是对齐的,因此我们现在可以通过在上一个代码的末尾添加显式命令ax2.grid(False)来删除第二个折线图的网格。

在此处输入图片说明

IMPORTANT: Note that in this example the labels in both left and right axis are pretty nice. 重要说明:请注意,在此示例中,左右轴上的标签都非常好。 However, in a real situation this approach may to lead to ugly labels (floats or rough integers) on the right axis. 但是,在实际情况下,此方法可能会导致右轴上出现难看的标签(浮点数或粗糙整数)。 Using this approach we are still not able to set the position of the right tick marks, so it is not an exact answer for the question. 使用这种方法,我们仍然无法设置正确的刻度线的位置,因此它不是该问题的确切答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM