Hi,
I am trying couple of different approaches when working on Line Plot and Bar plot in this assignment. Wonder if someone can just clarify if what I am trying to do is correct and achievable in that manner.
Q.1 When parsing Data column and also changing index to “Date” column can this be achieved different way compared to whats in my code? like
pd.read_csv(‘fcc…csv’)
pd.to_datetime(df[‘date’])
Q. 2. Can I use plt Matplotlib object to set my xlabel/ ylable/ title etc.? Or it has to be using ax object? like ax.set_xlabel and ax.set_title??
Q. 3 I imported datetime namespace can I not use DT object to set my newly created columns as df.dt.year or df.dt.month ? if No; why?
Q.4 For Bar plot; if I want to find out average views by grouping them into month & year can I use value_counts() function?
My bad; vaoue_counts() will only pick up unique values that re-occur. Disregard this ques pls
Thank in much advance; pls let me know if any of my questions make any sense.
import matplotlib.pyplot as plt
import pandas as pd
import datetime as dt
import seaborn as sns
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
# Import data (Make sure to parse dates. Consider setting index column to 'date'.)
df = pd.read_csv('fcc-forum-pageviews.csv', parse_dates = ['date'], index_col ='date')
#df.set_index('Date')
# Clean data
df = df[(df['value'] >= (df['value'].quantile(0.025))) &
(df['value'] <= (df['value'].quantile(0.975)))
]
def draw_line_plot():
# Draw line plot
fig, ax = plt.subplots()
plt.figure(figsize= (9,14))
plt.title("Daily freeCodeCamp Forum Page Views 5/2016-12/2019")
plt.xlabel("Date")
plt.ylabel("Page Views")
plt.plot(df.index, df['value'], linewidth = 2)
# Save image and return fig (don't change this part)
fig.savefig('line_plot.png')
return fig
def draw_bar_plot():
df['year'] = df.dt.year
df['month'] = df.dt.month
# Copy and modify data for monthly bar plot
df_bar = df.groupby(['year'],['month'])['value'].mean().value_counts()