使用pandas DataFrame在boxplot上绘制线 [英] plot line over boxplot using pandas DataFrame
问题描述
我有一个熊猫DataFrame,其中有16列对应的年份(2000年至2015年),每个月有12行,每个月都有值.
I have a pandas DataFrame with 16 columns corresponding years (2000 to 2015) and 12 lines with values for each month.
我正在尝试使用以下代码在同一无花果中绘制箱线图和带有2015年值的线,以便进行比较:
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()
在2015栏中,我有从1月到9月的数据,但是我得到了从yline到9月的移位线图:
In 2015 column I have data from January until September, but I get a shift line plot, from yline until september:
实际上,该行应从"Jan"开始,一直到"set",而不是在"ago"中结束:
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
我正在Windows 8.1上使用Python 3.4.3,pandas 1.7.0运行脚本.
I'm running the script using Python 3.4.3, pandas 1.7.0, over Windows 8.1.
我该如何解决?
推荐答案
非常感谢,pbreach.
Thank you so much, pbreach.
对我有用.
我做了:
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)
这篇关于使用pandas DataFrame在boxplot上绘制线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!