1. Histograms
import matplotlib.pyplot as plt
dog_pack[“height_cm”].hist(bins=5)
plt.show()
import matplotlib.pyplot as plt
avocados[avocados["type"] == "conventional"]["avg_price"].hist(alpha=0.5, bins=20)
avocados[avocados["type"] == "organic"]["avg_price"].hist(alpha=0.5, bins=20)
plt.legend(["conventional", "organic"]) # Add a legend
plt.show()
import matplotlib.pyplot as plt
print(df['Existing Zoning Sqft'].describe()) # Describe the column
df['Existing Zoning Sqft'].plot(kind='hist', rot=70, logx=True, logy=True) # Plot the histogram
plt.show()
2. Bar plots
import matplotlib.pyplot as plt
avg_weight_by_breed = dog_pack.groupby(“breed”)[“weight_kg”].mean()
avg_weight_by_breed.plot(kind = “bar”, title = “Mean Weight by Dog Breed”)
plt.show()
import matplotlib.pyplot as plt # Import matplotlib.pyplot with alias plt
print(avocados.head())
nb_sold_by_size = avocados.groupby("size")["nb_sold"].sum() # Get the total number of avocados sold of each size
nb_sold_by_size.plot(kind="bar") # Create a bar plot of the number of avocados sold by size
plt.show()
3. Line plots
import matplotlib.pyplot as plt
sully.plot(x = “date”, y = “weight_kg”, kind = “line”)
plt.show()
import matplotlib.pyplot as plt # Import matplotlib.pyplot with alias plt
nb_sold_by_date = avocados.groupby("date")["nb_sold"].sum() # Get the total number of avocados sold on each date
nb_sold_by_date.plot(kind="line") # Create a line plot of the number of avocados sold by date
plt.show()
import matplotlib.pyplot as plt
sully.plot(x = “date”, y = “weight_kg”, kind = “line”, rot = 45) # By passing an angle in degrees with the “rot” argument
plt.show()
4. Scatter plots
import matplotlib.pyplot as plt
dog_pack.plot(x = “height_cm”, y = “weight_kg”, kind = “scatter”)
plt.show()
avocados.plot(x="nb_sold", y="avg_price", kind="scatter", title="Number of avocados sold vs. average price") # nb_sold vs avg_price
plt.show()
5. Layering plots
import matplotlib.pyplot as plt
dog_pack[dog_pack[“sex”]==“F”][“height_cm”].hist(alpha=0.7) # Transparency - making the histogram translucent by calling alpha
dog_pack[dog_pack[“sex”]==“M”][“height_cm”].hist(alpha=0.7)
plt.legend([“F”, “M”]) # Add a legend
plt.show()
6. Scatter plots
import pandas as pd
import matplotlib.pyplot as plt
df.plot(kind='scatter', x='initial_cost', y='total_est_fee', rot=70) # Create and display the first scatter plot
plt.show()
7. Box plots
import pandas as pd
import matplotlib.pyplot as plt
df.boxplot(column='initial_cost', by='Borough', rot=90) # Create the boxplot
plt.show()
plt.subplot(2, 1, 1) # Add first subplot
gapminder['life_expectancy'].plot(kind="hist") # Create a histogram of life_expectancy
gapminder_agg = gapminder.groupby('year')['life_expectancy'].mean() # Group gapminder: gapminder_agg
print(gapminder_agg.head())
print(gapminder_agg.tail())
plt.subplot(2, 1, 2) # Add second subplot
gapminder_agg.plot(kind="line") # Create a line plot of life expectancy per year
plt.title('Life expectancy over the years') # Add title and specify axis labels
plt.ylabel('Life expectancy')
plt.xlabel('Year')
plt.tight_layout()
plt.show() # Display the plots
gapminder.to_csv('gapminder.csv') # Save DataFrames to csv files
gapminder_agg.to_csv('gapminder_agg.csv') # Save DataFrames to csv files
Creating a scatter plot in matplotlib to display the data
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
FemaleAdults= ([56,60,55,52,40,60,80,39,44,48], [168,155,150,154,140,153,155,156,153,160])
MaleAdults = ([60,70,73,72,81,52,76,58,66,69], [157,158,168,164,170,164,180,166,169,166])
female_weight = FemaleAdults[0]
female_height = FemaleAdults[1]
male_weight = MaleAdults[0]
male_height = MaleAdults[1]
fig = plt.figure()
fig.suptitle("Weight-Height Distribution")
plt.subplot(1,2,1)
plt.scatter(x=female_weight, y=female_height)
plt.title("Female")
plt.xlabel("Weight")
plt.ylabel("Height")
plt.subplot(1,2,2)
plt.scatter(x=female_weight, y=female_height)
plt.title("Male")
plt.xlabel("Weight")
plt.show()