Seaborn and Matplotlib Course notebook
Matplotlibยถ
Inย [ย ]:
Copied!
import numpy as np
import matplotlib.pyplot as plt
# Generate data
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
# Plot data
plt.plot(x, y)
# Show plot
plt.show()
import numpy as np
import matplotlib.pyplot as plt
# Generate data
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
# Plot data
plt.plot(x, y)
# Show plot
plt.show()
Inย [ย ]:
Copied!
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Plot data
plt.plot(x, y)
# Show plot
plt.show()
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Plot data
plt.plot(x, y)
# Show plot
plt.show()
Inย [ย ]:
Copied!
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Plot data
plt.scatter(x, y)
# Show plot
plt.show()
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Plot data
plt.scatter(x, y)
# Show plot
plt.show()
Inย [ย ]:
Copied!
import matplotlib.pyplot as plt
# Data
x = ['A', 'B', 'C', 'D', 'E']
y = [2, 4, 6, 8, 10]
# Plot data
plt.bar(x, y)
# Show plot
plt.show()
import matplotlib.pyplot as plt
# Data
x = ['A', 'B', 'C', 'D', 'E']
y = [2, 4, 6, 8, 10]
# Plot data
plt.bar(x, y)
# Show plot
plt.show()
Inย [ย ]:
Copied!
import matplotlib.pyplot as plt
# Data
x = ['A', 'B', 'C', 'D', 'E']
y = [2, 4, 6, 8, 10]
# Plot data
plt.barh(x, y)
# Show plot
plt.show()
import matplotlib.pyplot as plt
# Data
x = ['A', 'B', 'C', 'D', 'E']
y = [2, 4, 6, 8, 10]
# Plot data
plt.barh(x, y)
# Show plot
plt.show()
Inย [ย ]:
Copied!
import numpy as np
import matplotlib.pyplot as plt
# Data
x = np.random.randn(1000)
# Plot data
plt.hist(x, bins=100)
# Show plot
plt.show()
import numpy as np
import matplotlib.pyplot as plt
# Data
x = np.random.randn(1000)
# Plot data
plt.hist(x, bins=100)
# Show plot
plt.show()
Inย [ย ]:
Copied!
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35, 25, 25, 15])
plt.pie(y)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35, 25, 25, 15])
plt.pie(y)
plt.show()
Inย [ย ]:
Copied!
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Plot data
plt.plot(x, y, color='green')
# Add title and labels
plt.title('Line Plot')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
# Show plot
plt.show()
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Plot data
plt.plot(x, y, color='green')
# Add title and labels
plt.title('Line Plot')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
# Show plot
plt.show()
Inย [ย ]:
Copied!
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Get the weight of each point
z = np.random.rand(5)
# Plot using color map
plt.scatter(x, y, c=z ,cmap='plasma')
plt.colorbar()
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Get the weight of each point
z = np.random.rand(5)
# Plot using color map
plt.scatter(x, y, c=z ,cmap='plasma')
plt.colorbar()
Out[ย ]:
<matplotlib.colorbar.Colorbar at 0x7f4698040f40>
Inย [ย ]:
Copied!
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Plot data
plt.plot(x, y, linestyle='--', marker='o', markersize=8)
# Add title and labels
plt.title('Line Plot', color='red')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
# Show plot
plt.show()
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Plot data
plt.plot(x, y, linestyle='--', marker='o', markersize=8)
# Add title and labels
plt.title('Line Plot', color='red')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
# Show plot
plt.show()
Inย [ย ]:
Copied!
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y1 = [2, 4, 6, 8, 10]
y2 = [3, 5, 7, 9, 11]
y3= [-3, 5, 4, 9, 1]
y4 = [2, -4, 7, 4, 10]
# Plot data
fig, axs = plt.subplots(2, 2, figsize=(8, 4))
# first figure
axs[0][0].plot(x, y1)
# Add title to first figure
axs[0][0].set_title('Plot 1')
# second figure
axs[0][1].plot(x, y2)
# Add title to second figure
axs[0][1].set_title('Plot 2')
# third figure
axs[1][0].plot(x, y3)
# Add title to third figure
axs[1][0].set_title('Plot 3')
# third figure
axs[1][1].plot(x, y4)
# Add title to third figure
axs[1][1].set_title('Plot 4')
# Show plot
plt.show()
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y1 = [2, 4, 6, 8, 10]
y2 = [3, 5, 7, 9, 11]
y3= [-3, 5, 4, 9, 1]
y4 = [2, -4, 7, 4, 10]
# Plot data
fig, axs = plt.subplots(2, 2, figsize=(8, 4))
# first figure
axs[0][0].plot(x, y1)
# Add title to first figure
axs[0][0].set_title('Plot 1')
# second figure
axs[0][1].plot(x, y2)
# Add title to second figure
axs[0][1].set_title('Plot 2')
# third figure
axs[1][0].plot(x, y3)
# Add title to third figure
axs[1][0].set_title('Plot 3')
# third figure
axs[1][1].plot(x, y4)
# Add title to third figure
axs[1][1].set_title('Plot 4')
# Show plot
plt.show()
Inย [ย ]:
Copied!
plt.savefig('figure.png')
plt.savefig('figure.png')
<Figure size 432x288 with 0 Axes>
Seabornยถ
Inย [ย ]:
Copied!
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset('tips')
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset('tips')
Inย [ย ]:
Copied!
tips.head()
tips.head()
Out[ย ]:
total_bill | tip | sex | smoker | day | time | size | |
---|---|---|---|---|---|---|---|
0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
Inย [ย ]:
Copied!
sns.scatterplot(x='total_bill', y='tip', data=tips)
plt.show()
sns.scatterplot(x='total_bill', y='tip', data=tips)
plt.show()
Inย [ย ]:
Copied!
sns.lineplot(x='size', y='total_bill', data=tips)
plt.show()
sns.lineplot(x='size', y='total_bill', data=tips)
plt.show()
Inย [ย ]:
Copied!
sns.barplot(x='day', y='total_bill', data=tips)
plt.show()
sns.barplot(x='day', y='total_bill', data=tips)
plt.show()
Inย [ย ]:
Copied!
sns.histplot(x='total_bill', data=tips)
plt.show()
sns.histplot(x='total_bill', data=tips)
plt.show()
Inย [ย ]:
Copied!
sns.boxplot(x='day', y='total_bill', data=tips[tips['total_bill']<40])
plt.show()
sns.boxplot(x='day', y='total_bill', data=tips[tips['total_bill']<40])
plt.show()
Inย [ย ]:
Copied!
sns.violinplot(x='day', y='total_bill', data=tips)
plt.show()
sns.violinplot(x='day', y='total_bill', data=tips)
plt.show()
Inย [ย ]:
Copied!
import seaborn as sns
import pandas as pd
iris = sns.load_dataset('iris')
iris.head()
import seaborn as sns
import pandas as pd
iris = sns.load_dataset('iris')
iris.head()
Out[ย ]:
sepal_length | sepal_width | petal_length | petal_width | species | |
---|---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
Inย [ย ]:
Copied!
sns.pairplot(iris, hue='species')
sns.pairplot(iris, hue='species')
Out[ย ]:
<seaborn.axisgrid.PairGrid at 0x7f4664d82910>
Inย [ย ]:
Copied!
iris.head()
iris.head()
Out[ย ]:
sepal_length | sepal_width | petal_length | petal_width | species | |
---|---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
Inย [ย ]:
Copied!
# Create a correlation matrix
corr = iris.corr()
corr
# Create a correlation matrix
corr = iris.corr()
corr
Out[ย ]:
sepal_length | sepal_width | petal_length | petal_width | |
---|---|---|---|---|
sepal_length | 1.000000 | -0.117570 | 0.871754 | 0.817941 |
sepal_width | -0.117570 | 1.000000 | -0.428440 | -0.366126 |
petal_length | 0.871754 | -0.428440 | 1.000000 | 0.962865 |
petal_width | 0.817941 | -0.366126 | 0.962865 | 1.000000 |
Inย [ย ]:
Copied!
# Create the heatmap
sns.heatmap(corr, annot=True)
# Create the heatmap
sns.heatmap(corr, annot=True)
Out[ย ]:
<AxesSubplot:>
Inย [ย ]:
Copied!
import seaborn as sns
import pandas as pd
# Load the tips dataset
tips = sns.load_dataset("tips")
# Create a FacetGrid
g = sns.FacetGrid(tips, col="time", row="sex")
# Map the plot to the FacetGrid
g.map(sns.scatterplot, "total_bill", "tip")
import seaborn as sns
import pandas as pd
# Load the tips dataset
tips = sns.load_dataset("tips")
# Create a FacetGrid
g = sns.FacetGrid(tips, col="time", row="sex")
# Map the plot to the FacetGrid
g.map(sns.scatterplot, "total_bill", "tip")
Out[ย ]:
<seaborn.axisgrid.FacetGrid at 0x7f4660479ac0>
Inย [ย ]:
Copied!
import seaborn as sns
import matplotlib.pyplot as plt
# Set the plot background color
sns.set_style("darkgrid")
# Load the tips dataset
tips = sns.load_dataset("tips")
# Set the color palette
sns.set_palette("husl")
# Create a scatter plot
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="sex", size="size")
# Set the title and axis labels
plt.title("Tips by Total Bill")
plt.xlabel("Total Bill")
plt.ylabel("Tip")
# Set the font size and style
sns.set(font_scale=1.5, font="Arial")
import seaborn as sns
import matplotlib.pyplot as plt
# Set the plot background color
sns.set_style("darkgrid")
# Load the tips dataset
tips = sns.load_dataset("tips")
# Set the color palette
sns.set_palette("husl")
# Create a scatter plot
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="sex", size="size")
# Set the title and axis labels
plt.title("Tips by Total Bill")
plt.xlabel("Total Bill")
plt.ylabel("Tip")
# Set the font size and style
sns.set(font_scale=1.5, font="Arial")
Inย [ย ]:
Copied!