首页 > 解决方案 > Panda/PyPlot 使 X 轴线出现

问题描述

在此处输入图像描述

这是我目前拥有的。如果有意义,我想为 y = 0 的 x 轴添加一条黑线?现在,这些酒吧看起来好像只是漂浮在空中。

我的代码:

df2 = pd.DataFrame(values, columns=sectors)

df2.plot(kind='bar')
plt.axis("tight")

谢谢

编辑:想出了如何删除 x 轴标签

plt.xticks([])

标签: pythonpandasnumpymatplotlib

解决方案


解决方案

使用plt.axhline

import matplotlib.pyplot as plt
import pandas as pd

df2 = pd.DataFrame([[19.6, 2.3, -5.8]], columns=['Real Estate', 'Industrials', 'Utilities'])

df2.plot(kind='bar')

# the c='k' kwarg sets the line's color to blac(k)
plt.axhline(0, c='k')

plt.xticks([])
plt.axis("tight")

输出:

在此处输入图像描述

更改水平线的外观

axhline支持各种选项。对于更细的虚线,请执行以下操作:

# ls is short for linestyle, and lw is short for linewidth
plt.axhline(0, c='k', ls='--', lw=.5)

输出:

在此处输入图像描述


推荐阅读