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
381 views
in Technique[技术] by (71.8m points)

python - plot line over boxplot using pandas DataFrame

I have a pandas DataFrame with 16 columns corresponding years (2000 to 2015) and 12 lines with values for each month.

I'm trying plot a boxplot and a line with 2015 values in same fig in order to compare, using this code:

import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_excel('hidro_ne.xlsx')
fig, ax = plt.subplots()
ax1 = df[2015].plot(ax=ax, linewidth=2, legend='2015',color='red')
df.T.plot.box(yticks=range(0, 100, 5), ax=ax1)
plt.show()

In 2015 column I have data from January until September, but I get a shift line plot, from yline until september:

enter image description here

In fact the line should start at "Jan" and goes until "set", and not finish in "ago":

>>> df[2015]
Jan    16.41
Fev    18.34
Mar    23.52
Abr    27.40
Mai    26.96
Jun    25.34
Jul    22.49
Ago    18.38
Set    13.87
Out      NaN
Nov      NaN
Dez      NaN

I'm running the script using Python 3.4.3, pandas 1.7.0, over Windows 8.1.

How can I fix it?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Thank you so much, pbreach.

It works to me.

I made:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_excel('hidro_ne.xlsx')

fig, ax = plt.subplots()
ax.plot(list(range(1,13)), df[2015].values, 'r', linewidth=2)
ax.legend(['2015'])
df.T.plot.box(yticks=range(0, 105, 5), ax=ax)

enter image description here


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

...