要点是,虽然有2个相同类型的图形(即2个折线图)是可管理的,但是当我想将其中一个更改为另一种图形时,似乎无法完成。
下面是成功运行的两个折线图的代码:
year1 = df['Year'].tolist()
s1 = df['Average Yearly Income ($)'].tolist()
fig, ax1 = plt.subplots()
t = year1
s1 = df['Average Yearly Income ($)'].tolist()
ax1.plot(t, s1, 'b-', color = 'skyblue', label="Yearly Income ($)")
ax1.set_ylabel('Yearly Household Income ($)', color='black')
ax1.tick_params('y', colors='skyblue')
plt.legend(loc='upper left')
z = df2['Year'].tolist()
s2 = df2['Participation (%)'].tolist()
ax2 = ax1.twinx()
ax2.plot(z, s2, '-0', color = 'pink', label="Sports Participation (%)")
ax2.set_ylabel('Percentage of population that plays sports at least once a week', color='black')
ax2.tick_params('y', colors='maroon')
plt.title('Relationship between engagement in sports, and Household Income')
plt.legend(loc=4)
plt.show()
我想将参加运动的信息转换为条形图,该条形图对应于右侧的y轴值,因为只有4年(这就是表示年份的线形图看起来难看的原因)。
更改以下行:
ax2.plot(z, s2, '-0', color = 'pink', label="Sports Participation (%)")
收件人:
ax2.plot(z, s2, '-0', color = 'pink', label="Sports Participation (%)", kind = 'bar')
返回此错误:AttributeError: Unknown property kind