xbe.at

Data visualisation

Comprehensive Python Data Visualization Slideshow

4 min read

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

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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()

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')

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

More in Data visualisation

  1. Data Visualization for Machine Learning6 min

    Data visualization is a crucial tool in machine learning, enabling practitioners to gain insights, improve models, and communicate results effectively. It...

  2. Avoiding Misleading Data Visualizations10 min

    Proper data visualization requires careful consideration of scale selection to maintain data integrity. The choice of axis ranges and intervals directly...

  3. Data Visualization with Python Empowering Analysis8 min

    Data visualization is a powerful tool for understanding complex datasets and extracting meaningful insights. Python offers a rich ecosystem of libraries for...

  4. Alternatives to Cluttered Bar Plots4 min

    Bar plots, while popular, can become ineffective when dealing with large datasets or multiple categories. When visualizing data with numerous variables or...

  5. Differential Geometry Visualizations with Python11 min

    Differential geometry is a mathematical discipline that uses techniques of differential calculus, integral calculus, linear algebra, and multilinear algebra...

  6. A Visual Guide to Boosting in Machine Learning24 min

    Boosting is a powerful ensemble learning technique in machine learning. It combines multiple weak learners to create a strong learner, improving overall...