python - 为什么 Python Matplotlib 条形图的 X 轴刻度显示奇怪和错误的负值?

标签 python python-3.x matplotlib data-analysis

这个问题在这里已经有了答案:





Matplotlib yaxis range display using absolute values rather than offset values?

(3 个回答)


3年前关闭。




当 Xaxis 值范围为 43990 - 44003 时,为什么我的 X 轴刻度显示负值。

import matplotlib.pyplot as plt
x=[44000, 44001, 44002, 44003, 43990, 43991, 43992, 43993, 43994, 43995, 43996, 43997, 43998, 43999]
y=[8, 5, 3, 1, 1, 3, 4, 10, 4, 11, 4, 10, 17, 19]
plt.bar(x,y)
plt.show()

我看到以下输出。我期待 x 轴的范围从 43990 - 44003
why negative x-axis ticks ?

我在几台机器上试过这个,在最近版本的 python 和 matplotlib 上都显示出类似的奇怪行为(尝试了几个不同的版本)

Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 18:10:19) [GCC 7.2.0] on linux

Python 3.4.3 (default, Nov 17 2016, 01:08:31)



奇怪的是,许多琐碎的玩具示例 x 和 y 数组给了我预期的数字。

例如,以下代码段显示了带有正确 x 轴刻度标签的预期图形
x=[20,30,90,70, 50, 60, 80, 70]
y=[3,2,5,10, 3, 9, 7, 6]
plt.bar(x,y)
plt.show()

correct result for above example

我在这里错过了什么明显的东西?

最佳答案

您需要禁用偏移量:

plt.ticklabel_format(useOffset=False)

另一种选择是输入自定义刻度标签:
plt.bar(x, y, tick_label=x)

关于python - 为什么 Python Matplotlib 条形图的 X 轴刻度显示奇怪和错误的负值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48434919/

相关文章:

python - 根据条件在多个数据帧的数据帧内的列之间交换行

python - 我无法使用 pip3 安装 onnxruntime。请解决它

python [SSL] PEM 库 (_ssl.c :3309) error when verifying certificate

python - 如何使用 Python 在任意平面内绘制矢量场?

python - 具有重新标准化颜色条的 Cartopy pcolormesh

python - 程序第二次运行时 multiarray.pyd 出现未处理的异常

python - 彩色图上的轮廓 : not continuous (Python)

python - 值错误 : could not convert string to float : bored

python - 如何使用 python ElementTree 获取 XML 中的同级标签值

python - 将其他 Python 脚本作为模块导入