python - Panda/PyPlot 使 X 轴线出现
问题描述
这是我目前拥有的。如果有意义,我想为 y = 0 的 x 轴添加一条黑线?现在,这些酒吧看起来好像只是漂浮在空中。
我的代码:
df2 = pd.DataFrame(values, columns=sectors)
df2.plot(kind='bar')
plt.axis("tight")
谢谢
编辑:想出了如何删除 x 轴标签
plt.xticks([])
解决方案
解决方案
使用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)
输出:
推荐阅读
- reactjs - 如何处理表单子组件中的按钮
- python - 将 JSON 元素转换为数组
- docker - 为什么 docker-compose 使用 user:group 999:999 创建目录/文件?
- haproxy - HAProxy - 影响开始时间的大量后端服务器
- java - 标准化 UTC 日期
- java - 安卓。从外部文件夹读取数据库
- amazon-web-services - 将非活动用户迁移到 aws cognito
- python-3.x - 写入某个可移动目录的程序 (Windows)
- javascript - amCharts - 通过 HTML 元素或 id 获取图表对象?
- java - 非法字符 - 如果您需要对完整的 ASCII 字符集进行编码,请尝试使用扩展模式