python - 如何生成 x 轴乱序的 Matplotlib 图?

标签 python matplotlib

我想用不按数字顺序出现的 x 轴绘制一些 y 数据:

例如:

y = [100, 99, 93, 88, 85, 43]
x = [0, 5, 4, 3, 2, 1]
plot(x, y)

我希望 x 轴按此顺序显示为 0、5、4、3、2、1,并且间隔均匀,并具有上面适当的 y 值。

我该怎么做?

最佳答案

我会使用 x 'values' 作为 xtick 标签。例如:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
y = [100, 99, 93, 88, 85, 43]
xt = [0, 5, 4, 3, 2, 1]
ax.plot(y)
ax.set_xticklabels(xt)
fig.savefig("out.png")

产生

pic with changed xtick labels

更一般地说,您可以将 x 坐标设置为您想要的任何值,然后适本地设置 xticksxticklabels

关于python - 如何生成 x 轴乱序的 Matplotlib 图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15319699/

相关文章:

python - 在Python中执行文件夹内的所有文件

python - 使用 numpy cumsum 计算求和面积表的 3D 变体

python - 在可迭代对象上迭代多次

python - python 中区分大小写的路径比较

python - matplotlib 等高线图与 lognorm - colorbar 级别

python - 立体太阳图 matplotlib 极坐标图 python

python - 修改了颜色条刻度中的科学记数法

python - 嵌入 python

python - Matplotlib 散点图轴自动缩放对于小数据值失败

python - matplotlib 从颜色栏中删除刻度(轴)