回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我试图绘制几行(不是条形图,如<a href="https://stackoverflow.com/questions/7559242/matplotlib-strings-as-labels-on-x-axis">this case</a>)。我的y值是<code>float</code>,而x值是<code>categorical data</code>。如何在<code>matplotlib</code>中执行此操作?</p>
<p>我的价值观:</p>
<pre><code>data1=[5.65,7.61,8.17,7.60,9.54]
data2=[7.61,16.17,16.18,19.54,19.81]
data3=[29.55,30.24,31.51,36.40,35.47]
</code></pre>
<p>我的类别:</p>
<pre><code>x_axis=['A','B','C','D','E']
</code></pre>
<p>我正在使用的代码,它没有给我想要的:</p>
<pre><code>import matplotlib.pyplot as plt
fig=plt.figure() #Creates a new figure
ax1=fig.add_subplot(111) #Plot with: 1 row, 1 column, first subplot.
line1 = ax1.plot(str(x_axis), data1,'ko-',label='line1') #Plotting data1
line2 = ax1.plot(str(x_axis), data2,'ro-',label='line2') #Plotting data2
line3 = ax1.plot(str(x_axis), data3,'mo-',label='line3') #Plotting data3
plt.xticks(range(len(data3)), x_axis, size='small')
ax1.set_ylim(0,51)
ax1.set_ylabel('y values',fontsize=12)
#Assigning labels
lines = line1+line2+line3
labels = [l.get_label() for l in lines]
ax1.legend(lines,labels,loc='upper center', prop={'size':10}, bbox_to_anchor=(0.5, -0.13), fancybox=True, shadow=True, ncol=5)
ax1.set_xlabel('Categories',fontsize=14)
plt.setp(ax2.get_xticklabels(), visible=True)
title_string=('Plotting categories vs y values in matplotlib')
plt.suptitle(title_string, y=1.0, fontsize=17)
fig.tight_layout()
fig.subplots_adjust(top=0.92,bottom=0.2)
plt.show()
plt.savefig('myplot.jpg',bbox_inches='tight')
plt.close()
</code></pre>