【问题标题】:Pandas bar plot with secondary y-axis: hide grid line below plot带有辅助 y 轴的 Pandas 条形图:隐藏图下方的网格线
【发布时间】:2020-04-30 05:51:16
【问题描述】:

我正在绘制一些数据,如 pandas documentation 中所述。

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()

产生

现在我的问题是我想将网格线隐藏在条形后面。我已经尝试过 zorderset_axisbelow 的不同组合,但这仅适用于“第一”条。

如何将网格(可能还有图例)隐藏在栏后面?

提前致谢!

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    仅启用较低轴的网格。

    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()
    

    【讨论】:

    • 完美!非常感谢。实际上,这个明显的解决方案可能会在我不成功的尝试中出现.. ;)
    猜你喜欢
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多