Creating Data Visualizations. (Part-1)
Sculpting Data into Art: Unleash Matplotlib's Visual Power!
A key component of utilizing Matplotlib, a well-known Python data visualization package, is creating data visualizations. To begin utilizing Matplotlib to its fullest, you need to become proficient in the creation, customization, and enhancement of various visualizations. Here's a step-by-step tutorial on using Matplotlib to begin data visualization.
Import Matplotlib
Create Basic Plots
Customize Your Plot
Multiple Subplots
Bar Charts
Histograms
Pie Charts
Box Plots
Customize Aesthetics
Save and Export
Advanced Techniques
Interactive Plots
Step 1: Import Matplotlib
Matplotlib must be imported to be used. You may use the following command to accomplish this.
import matplotlib.pyplot as plt
By convention, Matplotlib is often imported as plt
.
Step 2: Create Basic Plots
Line Plot: Use plt.plot()
to create a basic line plot. You can provide x and y data to visualize a line chart.
x = [1, 2, 3, 4, 5]
y = [10, 15, 13, 18, 22]
plt.plot(x, y)
plt.show()
x = [1, 2, 3, 4, 5]
y = [10, 15, 13, 18, 22]
plt.scatter(x, y)
plt.show()
Step 3: Customize Your Plot
Use plt.xlabel() and plt.ylabel() to add labels to your axes.
Use plt.title() to give your plot a title.
Use the plot functions' optional parameters to change the color, line style, and markers.
Step 4: Multiple Subplots
You can use plt.subplot() or plt.subplots() in Matplotlib to create many subplots in a single figure, but their functions and use patterns differ.
plt.subplot()
:
One by one, subplots can be created inside a single figure using plt.subplot().
plt.subplot(rows, columns, index) requires three arguments: rows, which indicates how many rows there are in a subplot; columns, which indicates how many columns there are in a subplot; and index, which indicates where the current subplot is located.
From top to bottom and left to right, the index rises. Here's an illustration of how to make subplots using plt.subplot().
import matplotlib.pyplot as plt
# Create a 2x2 grid of subplots and select the first one
plt.subplot(2, 2, 1)
plt.plot([1, 2, 3, 4])
# Select the second subplot
plt.subplot(2, 2, 2)
plt.plot([4, 3, 2, 1])
# Select the third subplot
plt.subplot(2, 2, 3)
plt.plot([2, 4, 2, 4])
# Select the fourth subplot
plt.subplot(2, 2, 4)
plt.plot([3, 1, 3, 1])
plt.show()
plt.subplots()
:
To generate a grid of subplots all at once, use plt.subplots().
The various subplots are represented by a figure and a NumPy array of axis objects, which are returned.
Through indexing, each subplot is accessible. Here's an illustration of how to make subplots using plt.subplots().
import matplotlib.pyplot as plt
# Create a 2x2 grid of subplots
fig, axes = plt.subplots(2, 2)
# Now you can access each subplot using indexing
axes[0, 0].plot([1, 2, 3, 4])
axes[0, 1].plot([4, 3, 2, 1])
axes[1, 0].plot([2, 4, 2, 4])
axes[1, 1].plot([3, 1, 3, 1])
plt.show()
When you wish to generate many subplots, using plt.subplots() is usually more straightforward since it lets you deal directly with an array of axes. However, based on your unique needs, you can select either approach.
Step 5: Bar Charts
You can use the plt.bar
()
and plt.barh()
functions from the Matplotlib library in Python to create bar charts with vertical or horizontal bars, respectively. Here, I'll provide you with an example of both a vertical and a horizontal bar chart.
Vertical Bar Chart
Horizontal Bar Chart
Vertical Bar Chart
import matplotlib.pyplot as plt
# Sample data
categories = ['Category A', 'Category B', 'Category C', 'Category D']
values = [25, 50, 30, 45]
# Create a vertical bar chart
plt.bar(categories, values)
# Adding labels and title
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Vertical Bar Chart')
# Display the chart
plt.show()
Horizontal Bar Chart
import matplotlib.pyplot as plt
# Sample data
categories = ['Category A', 'Category B', 'Category C', 'Category D']
values = [25, 50, 30, 45]
# Create a horizontal bar chart
plt.barh(categories, values)
# Adding labels and title
plt.xlabel('Values')
plt.ylabel('Categories')
plt.title('Horizontal Bar Chart')
# Display the chart
plt.show()
In the first example, we make a vertical bar chart using plt.bar(), and in the second, we create a horizontal bar chart using plt.barh(). By including labels and captions and modifying the color, width, and other elements as necessary, you may further alter the chart.
Step 6: The Histogram
To generate histograms in Python using the Matplotlib library, you can use the plt.hist()
function. A histogram is a graphical representation of the distribution of a dataset. Here's how you can use plt.hist()
to create a histogram:
import matplotlib.pyplot as plt
import numpy as np
# Generate some random data (replace this with your dataset)
data = np.random.randn(1000) # Example data
# Create a histogram
plt.hist(data, bins=20, edgecolor='k') # "bins" specifies the number of bins in the histogram
# Adding labels and title
plt.xlabel('Values')
plt.ylabel('Frequency')
plt.title('Histogram')
# Display the histogram
plt.show()
to be continued ...
Summary
Thank you for reading our blog. Our top priority is your success and satisfaction. We are ready to assist with any questions or additional help. If find the blog to your liking, you may consider the gracious gesture of offering the author a token of appreciation in the form of a coffee.
Warm regards,
Content Editor