This tutorial aims to introduce you to the basics of data visualization using two powerful Python libraries - Matplotlib and Seaborn. It will teach you how to create various types of plots, customize plot aesthetics, and present complex data in a visually understandable way.
Data visualization is the graphical representation of data. It involves producing images that communicate relationships among the represented data to viewers of the images.
Matplotlib is a plotting library for the Python programming language. It provides an object-oriented API for embedding plots into applications.
Seaborn is a Python data visualization library based on Matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics.
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
# Create a figure and axis
fig, ax = plt.subplots()
# Plotting
ax.plot(x, y)
# Show the plot
plt.show()
In this example, we first import the matplotlib.pyplot
module and create data to plot. We then create a figure and axis using plt.subplots()
and plot the data on the axes. Finally, we display the plot using plt.show()
.
You should see a simple line plot displayed.
import seaborn as sns
# Load iris dataset
iris = sns.load_dataset('iris')
# Create histogram
sns.distplot(iris['sepal_length'])
# Show the plot
plt.show()
In this example, we first load the built-in iris dataset. We then create a histogram of the 'sepal_length' column with sns.distplot()
. Finally, the plot is displayed with plt.show()
.
You should see a histogram displayed.
In this tutorial, we covered the basics of data visualization using Matplotlib and Seaborn. We learned how to create a simple line plot with Matplotlib and a histogram with Seaborn.
Create a scatter plot using Matplotlib for the following data:
x = [5,7,8,7,2,17,2,9,4,11,12,9,6]
y = [99,86,87,88,111,86,103,87,94,78,77,85,86]
Create a boxplot for the 'sepal_width' column in the iris dataset using Seaborn.
Exercise 1:
# Data
x = [5,7,8,7,2,17,2,9,4,11,12,9,6]
y = [99,86,87,88,111,86,103,87,94,78,77,85,86]
# Create a figure and axis
fig, ax = plt.subplots()
# Plotting
ax.scatter(x, y)
# Show the plot
plt.show()
In this solution, we use ax.scatter()
instead of ax.plot()
to create a scatter plot.
Exercise 2:
# Load iris dataset
iris = sns.load_dataset('iris')
# Create boxplot
sns.boxplot(y=iris['sepal_width'])
# Show the plot
plt.show()
In this solution, we use sns.boxplot()
to create a boxplot. The 'y' argument specifies the column for which we want to create the boxplot.