带有辅助 y 轴的 Pandas 条形图:在图下方隐藏网格线

Cor*_*yer 2 python matplotlib pandas

我正在绘制熊猫文档中描述的一些数据。

import pandas as pd
import matplotlib.pyplot as plt


d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)


# plot
ax = df.plot(kind='bar', secondary_y=['col1'])
ax.set_ylabel('Foo')
ax.right_ax.set_ylabel('Bar')

# does not show any effect
ax.grid(True, zorder=0)
ax.right_ax.grid(True, zorder=0)

# does not show any effect
ax.set_axisbelow(True)
# works
ax.right_ax.set_axisbelow(True)

plt.show()
Run Code Online (Sandbox Code Playgroud)

这产生

在此处输入图片说明

现在我的问题是我想隐藏酒吧后面的网格线。我已经尝试了zorder和 的不同组合,set_axisbelow但这仅适用于“第一”条。

我怎样才能隐藏栅栏后面的网格(可能还有图例)?

提前致谢!

Imp*_*est 7

仅启用下方轴的网格。

import pandas as pd
import matplotlib.pyplot as plt

d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)

# plot
ax = df.plot(kind='bar', secondary_y=['col1'])
ax.set_ylabel('Foo')
ax.right_ax.set_ylabel('Bar')

ax.grid(True)
ax.set_axisbelow(True)

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明