Matplolib Cheat Sheet v2
Matplolib Cheat Sheet v2
Import Libraries and Create a DataFrame Line Plot (Object Oriented Method)
import numpy as np fig, ax= plt.subplots()
import pandas as pd ax.plot(df.men_age, df.men_salary ,"b")
import seaborn as sns ax.set_xlabel("men age")
import matplotlib.pyplot as plt ax.set_ylabel("men salary", color="r")
men_age = [25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45] ax.set_title("salary by age", color="b")
men_salary = [38496, 42000, 46752, 49320, 53200, ax.axvline(x=35, color="red", ls="--") # creating a vertical line for a given "x"
56000, 62316, 64928, 67317, 68748, 73752] value
women_salary = [45372, 48876, 53850, 57287, 63016,
65998, 70003, 70000, 71496, 75370, 83640]
women_age =[22, 26, 28, 30, 33, 37, 38, 39, 42, 43, 44]
group=["G1", "G2","G3","G4","G5","G6","G7","G8","G9","G10","G11"]
df=pd.DataFrame({"men_age":men_age, "men_salary":men_salary,
"women_age":women_age, "women_salary":women_salary, "group":group})
Bar Plot
fig,ax=plt.subplots(figsize=(8,5))
ax.bar(df.group, df.men_salary)
ax.set_xlabel("Age Groups")
ax.set_ylabel("Salary")
ax.set_title("Salary by Age Groups")
Pie Chart
slices = [59000, 55000, 47000, 36000, 35000]
langs = ['JavaScript', 'HTML/CSS', 'SQL', 'Python', 'Java']
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = langs
sizes = slices
explode = (0, 0, 0, 0.2, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
fig, ax = plt.subplots(figsize=(7,7))
ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=False, startangle=110)
ax.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.show()
Histogram
tips=sns.load_dataset("tips")
fig,ax=plt.subplots()
ax.hist(tips.total_bill, bins=15, edgecolor="black", color="green")