使用matplotlib重复x轴值

时间:2015-03-17 20:31:27

标签: python matplotlib

我有以下数据,我想用重复的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。 我明白这是因为范围功能。无论如何我可以解决这个问题。 我对这种支持非常有帮助。

此致 帕

1 个答案:

答案 0 :(得分:2)

您需要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

之类的内容