gpt4 book ai didi

python - 使用 matplotlib 重复 x 轴值

转载 作者:行者123 更新时间:2023-12-01 04:47:33 28 4
gpt4 key购买 nike

我有以下数据,我想用重复的 x 轴绘制图表。

| 30 |  | 0.650297475 |
| 40 | | 0.657665519 |
| 50 | | 0.669169975 |
| 60 | | 0.695916956 |
| 50 | | 0.680254894 |
| 40 | | 0.663912144 |
| 30 | | 0.653692469 |
| 20 | | 0.644423894 |
| 10 | | 0.637784575 |
| 0 | | 0.626213656 |
| 10 | | 0.637769063 |
| 20 | | 0.643006988 |
| 30 | | 0.646742206 |
| 50 | | 0.668539731 |
| 60 | | 0.694959931 |
| 50 | | 0.679511669 |
| 40 | | 0.6633013 |
| 30 | | 0.653026313 |
| 20 | | 0.644970369 |
| 10 | | 0.63772925 |
| 0 | | 0.626494163 |
| 20 | | 0.643006456 |
| 30 | | 0.646692806 |
| 40 | | 0.656562469 |
| 50 | | 0.668683988 |
| 60 | | 0.695289806 |
| 50 | | 0.680420325 |
| 40 | | 0.663145675 |
| 30 | | 0.654077156 |

我正在尝试使用 myplotlib 绘制它

x=range(len(x_axis))
fig = plt.figure()
ax = plt.gca()
ax.ticklabel_format(useOffset=False)
ax.set_xticks(x_axis, minor=True)
CH1 = ax.plot(x, y_axis)
ax.legend([parameter])#,bbox_to_anchor=(1.05, 1),loc=2, borderaxespad=0
fig.savefig("plotfile_get_Vendor_version_here"+".png")
plt.close(fig)

但 x 轴值终止于 30。我知道这是因为范围函数。无论如何我可以解决这个问题吗?我会非常乐意提供支持。

问候,帕万

最佳答案

您需要 ax.set_xticks() 来设置刻度位置,并需要 ax.set_xticklabels() 来实际打印刻度。尝试:

import numpy as np
import matplotlib.pyplot as plt

# the data:
x_axis = np.array(
[ 30., 40., 50., 60., 50., 40., 30., 20., 10., 0., 10.,
20., 30., 50., 60., 50., 40., 30., 20., 10., 0., 20.,
30., 40., 50., 60., 50., 40., 30.])
y_axis = np.array(
[ 0.65029748, 0.65766552, 0.66916997, 0.69591696, 0.68025489,
0.66391214, 0.65369247, 0.64442389, 0.63778457, 0.62621366,
0.63776906, 0.64300699, 0.64674221, 0.66853973, 0.69495993,
0.67951167, 0.6633013 , 0.65302631, 0.64497037, 0.63772925,
0.62649416, 0.64300646, 0.64669281, 0.65656247, 0.66868399,
0.69528981, 0.68042033, 0.66314567, 0.65407716])

x=range(len(x_axis))

fig, ax = plt.subplots(1, 1)
ax.set_xticks(x) # set tick positions
# Labels are formated as integers:
ax.set_xticklabels(["{:d}".format(int(v)) for v in x_axis])
ax.plot(x, y_axis)

fig.canvas.draw() # actually draw figure
plt.show() # enter GUI loop (for non-interactive interpreters)

或者你可以尝试像 this

关于python - 使用 matplotlib 重复 x 轴值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29109116/

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