Reputation: 609
I need to basically create the line plot using the 'Date, 'max', and 'min' columns in the dataframe I created. Basically, I need to make a line graph of historical high/low temperatures over time, with a shading in between both lines. Dataframe I have:
import matplotlib.pyplot as plt
import mplleaflet
import pandas as pd
import numpy as np
df = pd.read_csv('data/C2A2_data/BinnedCsvs_d400/fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89.csv')
df['max'] = df.groupby('Date')['Data_Value'].transform(lambda x: x.max())
df['min'] = df.groupby('Date')['Data_Value'].transform(lambda x: x.min())
df = df.sort_values('max', ascending=False).drop_duplicates('Date').sort_index()
df = df.sort('Date')
DataFrame I get:
ID Date Element Data_Value max min
1906 USC00205822 2005-01-01 TMIN -17 156 -56
129307 USC00202308 2005-01-02 TMIN -17 139 -56
150989 USW00004848 2005-01-03 TMAX 72 133 0
61059 USC00203712 2005-01-04 TMIN -6 39 -39
51221 USC00205563 2005-01-05 TMIN -94 33 -94
122448 USC00205563 2005-01-06 TMIN -72 0 -106
163039 USW00094889 2005-01-07 TMAX -33 6 -111
99499 USC00208202 2005-01-08 TMIN -83 17 -100
134653 USW00094889 2005-01-09 TMIN -28 28 -67
25301 USW00094889 2005-01-10 TMAX 17 44 -56
48510 USC00208080 2005-01-11 TMAX 17 44 -22
4702 USC00205822 2005-01-12 TMAX 17 139 -17
321 USC00208202 2005-01-13 TMAX 139 161 -83
132913 USC00205822 2005-01-14 TMAX 117 150 -128
97273 USC00207312 2005-01-15 TMIN -117 -33 -144
131251 USC00207320 2005-01-16 TMIN -111 -33 -150
109190 USC00207320 2005-01-17 TMAX -78 -50 -189
25774 USW00014833 2005-01-18 TMAX -89 -33 -217
91432 USC00200230 2005-01-19 TMAX 0 11 -300
71246 USW00004848 2005-01-20 TMIN -89 11 -156
157005 USC00205822 2005-01-21 TMIN -144 -39 -178
8233 USW00004848 2005-01-22 TMAX -78 -72 -178
29357 USC00200032 2005-01-23 TMIN -183 -44 -250
32777 USC00208202 2005-01-24 TMAX -67 11 -267
57337 USW00014833 2005-01-25 TMIN -50 28 -228
2111 USC00205563 2005-01-26 TMAX 0 28 -206
145972 USC00202308 2005-01-27 TMAX 0 6 -239
84449 USC00208202 2005-01-28 TMAX -67 -11 -250
72620 USW00014853 2005-01-29 TMIN -144 17 -222
96968 USC00208080 2005-01-30 TMIN -139 28 -217
So basically, I just need to make two lines- one max, and one min where x='Date' for both, and y='max' for max and y='min' for min.
Haven't really found any other posts related to this, so appreciate the help. Bonus: I also need to delete all values after 2015 so I'd appreciate help with that as well (again, having trouble doing this).
Upvotes: 2
Views: 3420
Reputation: 339200
If I understood correctly the aim is to plot the values from the column min for those entries where Element is TMIN, similarly for max.
In this case the following should work:
import pandas as pd
import matplotlib.pyplot as plt
# reading in the dataframe from the question text
df = pd.read_csv("data/Tminmax.csv", delim_whitespace=True)
# make Date a true Datetime
df["Date"] = pd.to_datetime(df["Date"])
# create two new Dataframes, one where Element is TMIN, one TMAX
dfmin = df[df["Element"] == "TMIN"]
dfmax = df[df["Element"] == "TMAX"]
# plot both dataframes
ax = dfmin.plot("Date", "min")
dfmax.plot("Date", "max", ax=ax)
ax.figure.autofmt_xdate()
plt.show()
which produces the plot below
Upvotes: 1
Reputation: 38415
You can use pandas plot
ax = df.plot('Date', 'max')
df.plot('Date', 'min', ax = ax)
Upvotes: 1