You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I went through the source code of timeVariantplot where I think that the code can be generalized and can be optimized to work better with any pollutant.
The above initialization should be done at once in the beginning like: Proposed solution:
df_days=dfdf_hour=dfdf_month=dfdf_weekday=df
Improved Code:
deftimeVariation(df, pollutant=['pm25']):
""" Plots four plots: - The average pollutant level per day by each hour for each day of the week across all of the data - The average pollutant level by each hour, across all data - The average pollutant level by each month of the year for across data - The average pollutant level per day of the week across all data Parameters ---------- df: pandas.DataFrame data frame of hourly data. Must include a date field and at least one variable to plot pollutant: str as list Name of pollutants to plot """importdatetimeasdtimportmatplotlib.pyplotaspltimportmatplotlibasmplimportnumpyasnpimportpandasaspdfromnumpyimportarray#print(pollutants)df["date"] =pd.to_datetime(df.date)
df_days=dfdf_hour=dfdf_month=dfdf_weekday=dfdf_days["day"] =df_days["date"].dt.day_name()
df_days=df_days.set_index(keys=["day"])
df_days=df_days.groupby(["day"])
dayWeek= [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
]
foriinrange(len(dayWeek)):
plt.figure(1, figsize=(40, 5))
plt.subplot(1, 7, i+1)
plt.grid()
df_day=df_days.get_group(dayWeek[i])
df_day["hour"] =df_day["date"].dt.hourdf_day_m=df_day.groupby("hour").mean().reset_index()
df_day_s=df_day.groupby("hour").std().reset_index()
forkinrange(len(pollutant)):
plt.plot(df_day_m["hour"], df_day_m[pollutant[k]], label=pollutant[k])
plt.fill_between(
df_day_s["hour"],
df_day_m[pollutant[k]] -0.5*df_day_s[pollutant[k]],
df_day_m[pollutant[k]] +0.5*df_day_s[pollutant[k]],
alpha=0.2,
)
plt.xlabel(dayWeek[i])
plt.legend()
plt.savefig("TimeVariationPlots1.png", bbox_inches="tight")
plt.figure(2, figsize=(35, 5))
plt.subplot(1, 3, 1)
plt.grid()
df_hour["hour"] =df_hour["date"].dt.hourdf_hour_m=df.groupby("hour").mean().reset_index()
df_hour_s=df.groupby("hour").std().reset_index()
foriinrange(len(pollutant)):
plt.plot(df_hour_m["hour"], df_hour_m[pollutant[i]], label=pollutant[i])
plt.fill_between(
df_hour_s["hour"],
df_hour_m[pollutant[i]] -0.5*df_hour_s[pollutant[i]],
df_hour_m[pollutant[i]] +0.5*df_hour_s[pollutant[i]],
alpha=0.2,
)
plt.xlabel("Hour")
plt.legend()
plt.subplot(1, 3, 2)
plt.grid()
df_month["month"] =df_month["date"].dt.monthdf_month_m=df_month.groupby("month").mean().reset_index()
df_month_s=df_month.groupby("month").std().reset_index()
foriinrange(len(pollutant)):
plt.plot(df_month_m["month"], df_month_m[pollutant[i]], label=pollutant[i])
plt.fill_between(
df_month_s["month"],
df_month_m[pollutant[i]] -0.5*df_month_s[pollutant[i]],
df_month_m[pollutant[i]] +0.5*df_month_s[pollutant[i]],
alpha=0.2,
)
plt.xlabel("Month")
plt.legend()
plt.subplot(1, 3, 3)
plt.grid()
df_weekday["weekday"] =df_weekday["date"].dt.weekdaydf_weekday_m=df_weekday.groupby("weekday").mean().reset_index()
df_weekday_s=df_weekday.groupby("weekday").std().reset_index()
foriinrange(len(pollutant)):
plt.plot(
df_weekday_m["weekday"], df_weekday_m[pollutant[i]], label=pollutant[i]
)
plt.fill_between(
df_weekday_s["weekday"],
df_weekday_m[pollutant[i]] -0.5*df_weekday_s[pollutant[i]],
df_weekday_m[pollutant[i]] +0.5*df_weekday_s[pollutant[i]],
alpha=0.2,
)
plt.xlabel("WeekDay")
plt.legend()
plt.savefig("TimeVariationPlots2.png", bbox_inches="tight")
print("Your plots has also been saved")
plt.show()
timeVariation(df1, pollutant=['pm25','nh3'])
# =============================================================================# df = pd.read_csv("mydata.csv")# timeVariation(df,['pm10'])# =============================================================================
NOTE: I'm also adding plt.savefig("TimeVariationPlots2.png", bbox_inches="tight") to save the figure.
Output:
The text was updated successfully, but these errors were encountered:
@nipunbatra , @patel-zeel
Description:
I went through the source code of timeVariantplot where I think that the code can be generalized and can be optimized to work better with any pollutant.
Source Code:
Code Optimization 1
vayu/vayu/timeVariation.py
Line 45 in ef99aef
The above list make is particular to the specified pollutants only, so it would be better if we take the list of pollutants from the user.
Proposed solution:
Hence the method signature should look like below:
Code Optimization 2
vayu/vayu/timeVariation.py
Lines 55 to 56 in ef99aef
The above line can be optimized like:
Proposed solution:
Code Optimization 3
vayu/vayu/timeVariation.py
Line 75 in ef99aef
vayu/vayu/timeVariation.py
Line 98 in ef99aef
The above initialization should be done at once in the beginning like:
Proposed solution:
Improved Code:
NOTE: I'm also adding
plt.savefig("TimeVariationPlots2.png", bbox_inches="tight")
to save the figure.Output:
The text was updated successfully, but these errors were encountered: