gpt4 book ai didi

python - 如何在使用 matplotlib 的 pyplot 绘制的图形上设置 x 轴标签?

转载 作者:行者123 更新时间:2023-11-28 21:58:38 25 4
gpt4 key购买 nike

我正在尝试绘制多年来(在 x 轴上)的销售额和费用值(在 y 轴上),如下所示。我期望以下代码将 2004、2005、2006 和 2007 设置为 x 轴值。但是,它没有按预期显示。请参阅下面的图片。让我知道如何在 x 轴上正确设置年份值。

import matplotlib.pyplot as plt
years = [2004, 2005, 2006, 2007]
sales = [1000, 1170, 660, 1030]
expenses = [400, 460, 1120, 540]
plt.plot(years, sales)
plt.plot(years, expenses)
plt.show()

enter image description here

最佳答案

这也可以以不同的方式完成工作:

fig=plt.figure()
ax = fig.add_subplot(111)

years = [2004, 2005, 2006, 2007]

sales = [1000, 1170, 660, 1030]


ax.plot(years, sales)
ax.xaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter('%d'))

plt.show()

关于python - 如何在使用 matplotlib 的 pyplot 绘制的图形上设置 x 轴标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17919360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com