我有以下代码来生成多年来某个月的每日降雨量的条形图。
df=pd.read_excel(("C:/Filepath/RainData.xls"), \
sheetname=sheet,\
header=0,\
parse_cols="B:BD",\
na_values='T')
precip = df.loc[31, :]
precipavg=((sum(precip[1:]))/(len(precip[1:])))
print(precipavg)
df.head(0)
fig,ax= plt.subplots()
precip.plot(kind="bar",ax=ax, color='g',figsize=(15,7))
plt.minorticks_on()
ax.tick_params(axis='x',which='minor',bottom='off')
ax.set_xlabel("Year")
ax.set_ylabel("Precipitation")
ax.set_title((filename+" Precipitation"), fontsize=20)
我想在此条形图的顶部添加一条线,表示该月的海藻降雨量。我已经使用上面的precipavg线计算了这个。感谢。
答案 0 :(得分:3)
您可以按如下方式使用axhline
:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# dummy data
df = pd.DataFrame(np.random.random(size=(20, 1)), columns=["rain"])
# your plot setup
fig,ax= plt.subplots()
df.plot(kind="bar", ax=ax, color='g',figsize=(15,7))
plt.minorticks_on()
ax.tick_params(axis='x',which='minor',bottom='off')
ax.set_xlabel("Year")
ax.set_ylabel("Precipitation")
ax.set_title(("Precipitation"), fontsize=20)
# use axhline
mean = df["rain"].mean()
ax.axhline(mean)