Skip to content

Latest commit

 

History

History
276 lines (179 loc) · 6.42 KB

File metadata and controls

276 lines (179 loc) · 6.42 KB

Comprehensive Python Data Visualization Slideshow

Slide 1:

Introduction to Matplotlib and Seaborn

Matplotlib is a low-level plotting library in Python, which produces publication-quality figures in a variety of formats. Seaborn is a higher-level data visualization library built on top of Matplotlib, providing a more attractive and informative statistical graphics.

Code:

import matplotlib.pyplot as plt
import seaborn as sns

Slide 2:

Line Plots with Matplotlib

Line plots are useful for visualizing the trend of a continuous variable over another variable, such as time. Matplotlib provides a straightforward way to create line plots.

Code:

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Line Plot Example')
plt.show()

Slide 3:

Scatter Plots with Matplotlib

Scatter plots are useful for visualizing the relationship between two continuous variables. Matplotlib's scatter function allows you to create scatter plots with customizable marker styles, colors, and sizes.

Code:

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

plt.scatter(x, y, marker='o', color='r', s=100)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Scatter Plot Example')
plt.show()

Slide 4:

Bar Plots with Matplotlib

Bar plots are useful for visualizing and comparing categorical data. Matplotlib provides the bar function to create bar plots, and you can customize the colors, widths, and alignments of the bars.

Code:

labels = ['A', 'B', 'C', 'D', 'E']
values = [10, 8, 6, 4, 2]

plt.bar(labels, values)
plt.xlabel('Category')
plt.ylabel('Value')
plt.title('Bar Plot Example')
plt.show()

Slide 5:

Histograms with Matplotlib

Histograms are used to visualize the distribution of a continuous variable. Matplotlib's hist function allows you to create histograms and customize the number of bins, bin ranges, and colors.

Code:

import numpy as np

data = np.random.normal(0, 1, 1000)

plt.hist(data, bins=20, edgecolor='black')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram Example')
plt.show()

Slide 6:

Pie Charts with Matplotlib

Pie charts are useful for visualizing the proportions of different categories in a dataset. Matplotlib's pie function allows you to create pie charts and customize the colors, labels, and exploded slices.

Code:

labels = ['A', 'B', 'C', 'D', 'E']
sizes = [25, 20, 15, 10, 30]

plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
plt.axis('equal')
plt.title('Pie Chart Example')
plt.show()

Slide 7:

Seaborn Regression Plots

Seaborn provides a convenient way to visualize the relationship between two continuous variables using regression plots. These plots can show linear regression lines, confidence intervals, and other statistical information.

Code:

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.regplot(x="total_bill", y="tip", data=tips)
plt.show()

Slide 8:

Seaborn Categorical Plots

Seaborn offers several plot types for visualizing the relationship between categorical variables and a continuous variable, such as bar plots, box plots, and violin plots.

Code:

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.catplot(x="day", y="total_bill", data=tips, kind="bar")
plt.show()

Slide 9:

Seaborn Distribution Plots

Seaborn provides several plot types for visualizing the distribution of a continuous variable, such as histograms, kernel density plots, and rugplots.

Code:

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.distplot(tips["total_bill"], kde=False, bins=30)
plt.show()

Slide 10:

Seaborn Pairwise Plots

Seaborn's pairplot function allows you to visualize the pairwise relationships between multiple continuous variables in a dataset. It creates a grid of scatter plots for each pair of variables.

Code:

import seaborn as sns
import matplotlib.pyplot as plt

iris = sns.load_dataset("iris")

sns.pairplot(iris, hue="species")
plt.show()

Slide 11:

Matplotlib Subplots

Matplotlib allows you to create subplots within a single figure, enabling you to display multiple plots simultaneously. This is useful for comparing different visualizations or visualizing different aspects of the same data.

Code:

import numpy as np
import matplotlib.pyplot as plt

# Generate some random data
data1 = np.random.normal(0, 1, 1000)
data2 = np.random.normal(2, 1, 1000)

# Create a figure with two subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))

# Plot the data on each subplot
ax1.hist(data1, bins=20, edgecolor='black')
ax1.set_title('Histogram 1')
ax2.hist(data2, bins=20, edgecolor='black')
ax2.set_title('Histogram 2')

# Adjust spacing between subplots
plt.subplots_adjust(wspace=0.5)

# Display the figure
plt.show()

Slide 12:

Seaborn Styling and Color Palettes

Seaborn provides various styling options and color palettes to enhance the aesthetics of your visualizations. You can set different color palettes, adjust the style of the plot elements, and more.

Code:

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

# Set the color palette
sns.set_palette("dark")

# Create a plot
sns.scatterplot(x="total_bill", y="tip", data=tips, hue="size")

# Remove the top and right spines
sns.despine()

# Display the plot
plt.show()

Slide 13:

Saving Plots to Files

Both Matplotlib and Seaborn allow you to save your plots as image files for later use or sharing. You can save plots in various formats, such as PNG, JPEG, SVG, and PDF.

Code:

import matplotlib.pyplot as plt

# Create a plot
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)

# Save the plot as a PNG file
plt.savefig('my_plot.png')

# Save the plot as a PDF file
plt.savefig('my_plot.pdf')

Slide 14:

Additional Resources

For further learning and exploration, here are some recommended resources:

  • "Python Data Visualization Cookbook" by Igor Milovanović and Dimitri Adreit (Book)
  • "Matplotlib 3.0 Cookbook" by Cyril Sheng (Book)
  • "Hands-On Data Visualization with Matplotlib" by William L. Winston (Book)
  • "Seaborn Data Visualization: A Complete Guide" by James Ranalli (ArXiv: https://arxiv.org/abs/2011.08744)

These resources cover more advanced topics, techniques, and best practices for data visualization with