1.pd.to_datetime
import pandas as pd
unrate = pd.read_csv("unrate.csv")
unrate['DATE'] = pd.to_datetime(unrate['DATE'])
print (unrate.head(12))
2.DataViz Intro
plt.plot
The default plot for plt.plot
import matplotlib.pyplot as plt
plt.plot()
plt.show()
Add x_value and y_value
xvalue = unrate['DATE'].iloc[0:12]
yvalue = unrate['VALUE'].iloc[0:12]
plt.xticks(rotation = 90) # rotate the label
plt.xlabel("Month") # add x_label
plt.ylabel("Unemployment Rate") # add y_label
plt.suptitle("Monthly Unemployment Trends, 1948") # add title
plt.plot(xvalue, yvalue)
Create multiple subplots in a figure
import matplotlib.pyplot as plt
fig = plt.figure(figsize = (12,6))
ax1 = fig.add_subplot(2,1,1) #2rows 1column, row 1st
ax2 = fig.add_subplot(2,1,2) #2rows 1column, row 2nd
x1 = unrate["DATE"].iloc[0:12]
y1 = unrate["VALUE"].iloc[0:12]
x2 = unrate["DATE"].iloc[12:24]
y2 = unrate["VALUE"].iloc[12:24]
ax1.plot(x1, y1)
ax2.plot(x2, y2)
plt.show()
Get year from datetime and plot multiple figures
import matplotlib.pyplot as plt
fig = plt.figure(figsize = (12,12))
xvalue = []
yvalue = []
for i in range(1948, 1953):
x = unrate['DATE'].loc[unrate['DATE'].dt.year == i] # get year
y = unrate['VALUE'].loc[unrate['DATE'].dt.year == i] # get year
xvalue.append(x)
yvalue.append(y)
for i in range(0, 5):
ax = fig.add_subplot(5, 1, i+1)
ax.plot(xvalue[i], yvalue[i])
plt.show()
Plot several lines in the same figure
unrate['MONTH'] = unrate['DATE'].dt.month
fig = plt.figure(figsize=(6,3))
plt.plot(unrate[0:12]['MONTH'], unrate[0:12]['VALUE'], c='red')
plt.plot(unrate[12:24]['MONTH'], unrate[12:24]['VALUE'], c='blue')
plt.show()
Set range for different index, set legend
fig = plt.figure(figsize=(10,6))
colors = ['red', 'blue', 'green', 'orange', 'black']
for i in range(5):
start_index = i*12
end_index = (i+1)*12
subset = unrate[start_index:end_index]
label = str(1948 + i)
plt.plot(subset['MONTH'], subset['VALUE'], c=colors[i], label=label)
plt.legend(loc='upper left')
plt.xlabel("Month, Integer")
plt.ylabel("Unemployment Rate, Percent")
plt.title("Monthly Unemployment Trends, 1948-1952")
plt.show()
Select certain columns from a dataframe
import pandas as pd
reviews = pd.read_csv("fandango_scores.csv")
norm_reviews = reviews[['FILM', 'RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']]
print (norm_reviews[:1]) # get the first row
Draw a bar chart
import matplotlib.pyplot as plt
from numpy import arange
fig, ax = plt.subplots()
num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']
bar_heights = norm_reviews[num_cols].iloc[0].values
# arange returns to [0, 1, 2, 3, 4] + 0.75 for each of it
bar_positions = arange(5) + 0.75
ax.bar(bar_positions, bar_heights, 0.5)
plt.show()
set_xtick
import matplotlib.pyplot as plt
from numpy import arange
fig, ax = plt.subplots()
num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']
bar_heights = norm_reviews[num_cols].iloc[0].values
bar_positions = arange(5) + 0.75
ax.bar(bar_positions, bar_heights, 0.5)
tick_positions = range(1,6)
ax.set_xticks(tick_positions)
ax.set_xticklabels(num_cols, rotation = 90)
ax.set_xlabel("Rating Source")
ax.set_ylabel("Average Rating")
ax.set_title("Average User Rating For Avengers: Age of Ultron (2015)")
plt.show()
Set barh chart
import matplotlib.pyplot as plt
from numpy import arange
num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']
# set fig, ax and draw barh
fig, ax = plt.subplots()
bar_widths = norm_reviews[num_cols].iloc[0].values
bar_positions = arange(5) + 0.75
ax.barh(bar_positions, bar_widths, 0.5)
# set tick_positions
tick_positions = range(1,6)
ax.set_yticks(tick_positions)
ax.set_yticklabels(num_cols)
ax.set_xlabel("Average Rating")
ax.set_ylabel("Rating Source")
ax.set_title("Average User Rating For Avengers: Age of Ultron (2015)")
plt.show()
Set scatter plot
fig, ax = plt.subplots()
ax.scatter(norm_reviews['Fandango_Ratingvalue'], norm_reviews['RT_user_norm'])
ax.set_xlabel("Fandango")
ax.set_ylabel("Rotten Tomatoes")
plt.show()
Frequency Distribution
fr_counts = norm_reviews["Fandango_Ratingvalue"].value_counts()
fandango_distribution = fr_counts.sort_index()
Histogram
fig, ax = plt.subplots()
# here range() sets the range for xaxis
ax.hist(norm_reviews['Fandango_Ratingvalue'], range = (0, 5))
plt.show()
Multiple histogram
fig = plt.figure(figsize=(5,20))
cname = ["Fandango_Ratingvalue", "RT_user_norm", "Metacritic_user_nom", "IMDB_norm"]
ctitle = ["Distributino of Fandango Ratings", "Distribution of Rotten Tomatoes Ratings", "Distribution of Metacritic Ratings", "Distribution of IMDB Ratings"]
for i in range(0, 4):
ax = fig.add_subplot(4, 1, i+1)
ax.hist(norm_reviews[cname[0]], bins = 20, range = (0, 5))
ax.set_ylim(0, 50)
ax.set_ylabel("Frequency")
ax.set_title(ctitle[i])
plt.show()
Boxplot
fig, ax = plt.subplots()
ax.boxplot(norm_reviews["RT_user_norm"])
ax.set_ylim(0, 5)
ax.set_xticklabels(["Rotten Tomatoes"]) # it has to be iterable
plt.show()
Several boxplot in one figure
fig, ax = plt.subplots()
num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue']
ax.boxplot(norm_reviews[num_cols].values)
ax.set_xticklabels(num_cols, rotation = 90)
ax.set_ylim(0,5)
plt.show()
网友评论