add horizontal limit line to time series plot in python

对着背影说爱祢 提交于 2021-02-05 10:50:13

问题


I want to add horizontal upper and lower limit line for Temparature timeseries plot. Lets say upper limit line at 30 and lower limit line at 10.

df3.plot(x="Date", y=["Temp.PM", "Temp.AM"],figsize=(20,8))


回答1:


I think this solution can help you

import matplotlib.pyplot as plt
%matplotlib inline

df3.plot(x="Date", y=["Temp.PM", "Temp.AM"],figsize=(20,8))
plt.axhline(30)
plt.axhline(10)



回答2:


plt.plot(df3['Date'], df3[["Temp.PM", "Temp.AM"]])
plt.axhline(30, color='r')
plt.axhline(10, color='b')
plt.show()



回答3:


Here is some code to get started and experimenting:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

dates = pd.date_range('20190822', '20191020', freq='D')
df3 = pd.DataFrame({'Date': dates,
                    'Temp.AM': np.random.normal(22.5, 2, len(dates)),
                    'Temp.PM': np.random.normal(27.5, 2, len(dates))})
df3.plot(x='Date', y=['Temp.PM', 'Temp.AM'], color=['dodgerblue','coral'], figsize=(20, 8))

min_temp = df3['Temp.AM'].min()
max_temp = df3['Temp.PM'].max()
plt.axhline(min_temp, c='coral', ls='--')
plt.axhline(max_temp, c='dodgerblue', ls='--')
plt.text(df3['Date'][0], min_temp, f'\nMin: {min_temp:.1f}°C', ha='left', va='center', c='coral')
plt.text(df3['Date'][0], max_temp, f'Max: {max_temp:.1f}°C\n', ha='left', va='center', c='dodgerblue')
plt.show()



来源:https://stackoverflow.com/questions/60205940/add-horizontal-limit-line-to-time-series-plot-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!