首页 > 解决方案 > 当我在 python 中绘制导入的 excel 文件时,xticks 重叠并聚集在左侧。我该如何解决?

问题描述

当我在 python 中绘制导入的 excel 文件时,在此处输入图像描述,xticks 重叠并聚集在左侧。我该如何解决?

这些是代码片段:

import matplotlib.pyplot as plt
import matplotlib.lines as mlines

plt.style.use('ggplot')
fig = plt.figure()
ax = plt.axes()
data = pd.read_excel('Sample_data_xls.xls')

states_list1 = ["AK", "AR", "CO", "DE", "GA", "IA", "IN", "LA", "MD", "MI", "MS", "ND", "NH", "NM", 
                "OH", "PA", "SC", "TN", "VA", "WI", "WY"]


plt.figure()
plt.plot(data['gdp_growth_2009'], '--', color='red')
plt.plot(data['gdp_growth_2010'], 'b')
plt.plot(data['gdp_growth_2012'], 'g')
plt.ylabel('Growth Rates')
plt.xlabel('States')
plt.title('Growth Rates in 2009 and 2010')

ax.xaxis.set_major_locator(plt.MaxNLocator(25))
ax.axhline(y=0, color="black", alpha= 0.3)
plt.xticks(range(len(states_list1)), list(states_list1))

red_line = mlines.Line2D([], [], color='red', markersize=15, label='2009', linestyle='--')
blue_line = mlines.Line2D([], [], color='blue', markersize=15, label='2010')
green_line = mlines.Line2D([],[], color='green', markersize=15, label='2012')
plt.legend(handles=[red_line, blue_line, green_line])

标签: pythondataframematplotlibspyder

解决方案


推荐阅读