Matplotlib: how to plot a line with categorical data on the x-axis?

瘦欲@ 提交于 2019-12-05 16:44:25
user2750362

Your code looks incorrect with regards to some syntax:

line1 = ax1.plot(data1,'ko-',label='line1') #no need for str(x_axis)
line2 = ax1.plot(data2,'ro-',label='line2') 
line3 = ax1.plot(data3,'mo-',label='line3') 

and

plt.setp(ax1.get_xticklabels(), visible=True) #not ax2

When I fixed these the plotting worked fine, your line

plt.xticks(range(len(data3)), x_axis, size='small')

is a correct way to assign a list to the x axis.

Matplotlib version 2.1.0 allows plotting categorical variables directly, just calling plt.plot(x,y) as usual, without the need to use range or get_xticklabels().

line1 = plt.plot(x_axis, data1,'ko-',label='line1')
line2 = plt.plot(x_axis, data2,'ro-',label='line2') 
line3 = plt.plot(x_axis, data3,'mo-',label='line3')

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!