python - xticks 的 matplotlib 自定义顺序

标签 python matplotlib

我有一天中 19:00 到 06:00 之间的一些数据 并喜欢在 x 轴上按自然顺序绘制它。 这意味着 x 轴的值将按以下顺序排序:19, 20, 21, 23, 0, 1, 2, .. 6

我怎样才能实现?

这是我的示例代码:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(19,24+6)%24
y = np.random.rand(len(x))*10

plt.bar(x, y)
plt.show()

enter image description here

最佳答案

最简单的方法是确保您的 x 数据点是连续的,然后像这样可靠的 xticks

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(19,24+6)
y = np.random.rand(len(x))*10

plt.bar(x, y)
plt.xticks(x, labels=x % 24)
plt.show()

关于python - xticks 的 matplotlib 自定义顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57669005/

相关文章:

python - 我可以将表从 SQL Server (=MS SQL) 导入到 Python/Pandas 数据框中吗?

python - matplotlib 仅绘制具有所需分辨率和 dpi 的图像

python - 使用或不使用颜色条对齐 matplotlib 子图轴(对于 2 个以上的轴)

python - 使用不同的 y 轴绘制 pandas 数据框中的条形图和线条

python - 从 Pandas Groupby Dataframe 创建等高线图

python - 来自两个给定列表的二维数组

python - tkinter.mainloop 函数的 n 参数是什么?

python - 如何控制Python散点图中每个点的颜色和不透明度?

python - 如何将列表划分为相似的平均子集

python - matplotlib 怪异,它没有绘制我的图表