Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

pandas - Python bar plot vs line plot: why are the x-axis labels different?

I'm using a single dataframe merged from two source dataframes to plot a data series. Both plots work well, but I'm having trouble controlling the x-axis labels displayed in the line plot, even though the code I'm using for both plots is otherwise identical. This code:

merged_data = pd.merge(newdata, newstuff, on='Date')
ax = merged_data.plot(kind='bar', figsize=(20, 7))
ax.set_xticklabels(merged_data.Date, rotation=45)
ax.set_ylabel('CPI vs. Wages and salaries - 12-month change')
ax.set_xlabel('Dates')

Produces this output: bar graph

But this code:

ax = merged_data.plot(kind='line', figsize=(20, 7))
ax.set_xticklabels(merged_data.Date, rotation=45)
ax.set_ylabel('CPI vs. Wages and salaries - 12-month change')
ax.set_xlabel('Dates')

gives me this output: line graph I certainly don't mind having fewer labels so they'll be easier to read, but the labels I'm getting on the line graph don't correspond to the actual data: the data covers 20 years (as you can see from the labels in the first graph), but the line-graph labels only cover the first two years.

From carefully visually scanning the graphs themselves, they both seem to be tracking the full data set. Any idea what I'm missing? Thanks,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...