Machine Learning courses with 110+ Real-time projects Start Now!!
The ability to effectively share and comprehend information relies heavily on data visualization’s ability to help us draw insights from complicated datasets. The Matplotlib Python package is widely used because it has robust capabilities for producing eye-catching visualisations.
The pyplot module is an important part of this package since it streamlines the production of plots and charts. This in-depth tutorial will teach you how to use Matplotlib’s pyplot for data visualisation and will equip you with the skills you need to make impressive visual representations of your data.
Learning the Foundations of Pyplot in Matplotlib
Installation
Let’s check whether Matplotlib is available in our Colab environment before we get started. There is no need to explicitly install Matplotlib because it is already included in Colab. However, Matplotlib may be installed with the Python package manager (pip) if you’re using a different Python environment.
!pip install matplotlib
Let’s go into using Pyplot now that Matplotlib is set up.
Matplotlib and Pyplot import
Importing the required libraries into your Python environment is the first step in utilising Matplotlib’s pyplot. The following lines of code will get you there:
import matplotlib.pyplot as plott import numpy as numpyy
Here, the Matplotlib library is brought in and aliased to Plott for simplicity. NumPy (abbreviated numpyy) is also imported since it is often used to produce data for graphing.
Outlining a Simple Storyline
1. Figure and axis initialization
Axes relate to the specific plotting region inside a figure, whereas figures represent the complete window or page in Matplotlib. Use the following code to generate a simple graph:
figuree, axess = plott.subplots()
2. Use Pyplot’s plot() function to visually represent data
The plot() method allows you to see your data once you have prepared the figure and axes. Assume that the a and b positions of your data points are stored in two separate arrays, a and b. The following lines provide a visual representation of this:
figuree, axess = plott.subplots() a = numpyy.linspace(3, 30, 300) b = numpyy.cos(a) axess.plot(a, b)
This piece of code creates an array of 300 values from 3 to 30 and uses the sine function to get their associated y-coordinates. These data points are then used by the plot() method to generate a graph.
3. Adding Title
Visualisations are useless without descriptive titles. Titles may be easily added to charts in Pyplot. Let’s have a look at a basic plot of a sine wave with a name:
import matplotlib.pyplot as plott
import numpy as numpyy
# Generate data
x_axis = numpyy.linspace(10, 5 * numpyy.pi, 50)
y_axis = numpyy.sin(x_axis)
# Create the plot
plott.plot(x_axis, y_axis)
# Add title
plott.title("Sine Wave Example")
# Display the plot
plott.show()
Using plt.title(), we give our plot the title “Sine Wave Example” in this demonstration. You may change the size, colour, and alignment of the titles to suit your needs.
Graphing Varieties of Data
The ability to see and communicate trends and patterns in your data is greatly enhanced by data visualisation. The pyplot module of Matplotlib offers a variety of plotting capabilities. This tutorial will walk you through the steps of using Matplotlib’s pyplot to successfully visualise your data in a variety of formats.
Line Graphs
1. Drawing a straight line graph
The plot() method may be used to generate a simple line plot. Let’s say we’re interested in graphing the outcomes of the function b = a:
a = numpyy.linspace(5, 50, 550) b = a plott.plot(a, b) plott.show()
This code creates an array of 100 integers from 0 to 10 and finds the squares of those numbers. The plot() method then draws a line between each of these points to create a line plot.
2. Altering the appearance of lines and their markers
Changing the line’s look is as simple as adjusting a few sliders. For instance, the following code may be modified to switch the line colour to red and the style to dashed:
plott.plot(a, b, linestyle='-.', color='green') plott.show()
Adding marks to emphasise the data points is also possible through the marker option.
plott.plot(a, b, marker='x') plott.show()
3. Increasing the number of lines in a graph
It is possible to include many lines in a single plot when using Pyplot. Take the following two equations, line1 = a2 and line2 = a3, and graph them side by side:
line1 = a ** 3 line2 = a ** 2 plott.plot(a, line1, label='a^3') plott.plot(a, line2, label='a^2') plott.legend() plott.show()
Here, we use the plott.legend() function to plot a legend above each line, and we provide the legend using the label argument.
Scatter Diagrams
1. Discrete data points as symbols in a graph
The correlation between two variables may be shown clearly in a scatter plot. Use the scatter() procedure to make a scatter diagram. Let’s take a look at a scenario where we have two sets of data coordinates, a and b, represented by separate arrays:
a = [7, 14, 28, 56, 112] b = [14, 28, 56, 112, 224] plott.scatter(a, b) plott.show()
The code generates a graph with discrete markers representing the data points.
2. Size, hue, and opacity controls for markers
Changes to the s, c, and alpha parameters allow you to alter the size, colour, and opacity of the marks. Here’s an illustration:
plott.scatter(a, b, s=130, c='green', alpha=0.5) plott.show()
The size of the marker is changed to 130, the colour to green, and the transparency to 0.5 in this code piece.
3. Including supplementary visuals (such as a grid or a legend)
Add a grid and a legend using plott.grid() and plott.legend(), respectively, to improve the visual representation. Here’s an illustration:
plott.scatter(a, b, label='Data Flair') plott.legend() plott.grid(True) plott.show()
This piece of code renders a grid over the plot with a legend titled “Data Flair.”
Bar Graphs
1. Making bar graphs, both horizontal and vertical
When comparing discrete groups of information, bar graphs shine. Plott.bar() generates vertical bar plots, whereas plott.barh() generates horizontal bar plots. Consider this case:
cats = ['Apple', 'Banana', 'Guava', 'Mango'] vals = [20, 25, 14, 18] plott.bar(cats, vals) plott.show()
With this code, you can generate a vertical bar plot, where the x-axis represents categories and the y-axis represents values.
2. Adjustable label placement, bar width, and coloration
The bar chart’s appearance may be altered in many ways. The colour option, for instance, lets you customise the hues of the bars:
plott.bar(cats, vals, color='maroon') plott.show()
You may also adjust the bar width using the width parameter:
plott.bar(cats, vals, width=0.3) plott.show()
The plott.xlabel() and plott.ylabel() routines may be used to add labels to the bars.
3. Bar graphs with stacking and grouping
Matplotlib lets you create group and stacked bar graphs to display many different variables. Multiple calls to plott.bar() with the same bottom argument will produce a stacked bar plot. Each bar’s x-coordinate may be changed independently in grouped bar graphs. Here’s an illustration:
cats = ['Apple', 'Banana', 'Guava', 'Mango'] vals1 = [20, 25, 14, 18] vals2 = [15, 28, 13, 11] plott.bar(cats, vals1, bottom=vals2, label='1st Group') plott.bar(cats, vals2, label='2nd Group') plott.legend() plott.show()
This code snippet generates a stacked bar plot consisting of two separate bar groups.
Histograms
1. Displaying data in a graphical format
Histograms display the distribution of data points graphically. Using the plott.hist() method, a histogram may be generated. Let’s take a look at a scenario where we have a set of values in an array:
dataflair_data = numpyy.random.randn(2500) plott.hist(dataflair_data) plott.show()
This piece of code creates a histogram with 2500 values chosen at random.
2. Adjusting the bin size and bin width
The default number of histogram bins is set by Matplotlib. However, the bins option allows you to choose the desired number of bins. Case in point:
plott.hist(dataflair_data, bins=30) plott.show()
A histogram with 30 bins is generated by this piece of code.
Bin width may be adjusted using the range parameter:
plott.hist(dataflair_data, bins=30, range=(-5, 5)) plott.show()
3. Histograms with added density curves
Using the density parameter, a density curve may be superimposed on top of the histogram. Here’s an illustration:
plott.hist(dataflair_data, bins=30, density=True) plott.show()
This piece of code renders the density curve beside the histogram.
Pie Charts
1. Making a pie chart using categorised information
Categorical data can be easily visualised using pie charts, and percentages can be shown with ease. The plott.pie() method may be used to make a pie chart. Consider this case:
cats = ['Bluecurrent', 'Mango', 'Guava', 'Apple'] values = [22, 33, 24, 10] plott.pie(values, labels=cats) plott.show()
This code creates a pie chart where each section is labelled and its size corresponds to the percentage of data in that section.
2. Altering the slice’s title, colour, and explosion size
Several components of a pie chart may be altered to improve its visual appeal. The colours argument, for instance, allows us to alter the slice colours:
df_colors = ['blue', 'yellow', 'green', 'red'] plott.pie(values, labels=cats, colors=df_colors) plott.show()
In addition, the explode option allows us to highlight a particular slice by exploding it.
df_explode = [0, 0, 0, 0.1] df_colors = ['blue', 'yellow', 'green', 'red'] plott.pie(values, labels=cats, colors=df_colors, explode=df_explode) plott.show()
The autopct argument allows us to automatically label each slice with the corresponding percentage:
plott.pie(values, labels=cats, autopct='%1.2f%%') plott.show()
3. Bringing out certain slices
A pie chart’s individual segments may be highlighted by removing them from the whole. Here’s an illustration:
df_explode = [0, 0, 0, 0.1] df_colors = ['blue', 'yellow', 'green', 'red'] plott.pie(values, labels=cats, explode=df_explode, colors=df_colors, shadow=True, startangle=90) plott.show()
With this line of code, you can give your pie chart a shadow effect and have the first slice begin at a right angle.
Limits and Checkboxes
If you want to zero in on certain data ranges and make your plots more legible, you need to take command of the axis boundaries and tick labels. The x and y axes in Pyplot can have their limits and tick labels modified to suit your needs. Let’s try this out on a scatter plot and see what happens.
# Generate random data
x_axis = numpyy.random.rand(100)
y_axis = numpyy.random.rand(100)
# Create the plot
plott.scatter(x_axis, y_axis)
# Set axis limits and tick labels
plott.xlim(0, 1)
plott.ylim(0, 1)
plott.xticks([0, 0.4, 1])
plott.yticks([0, 0.4, 1])
# Add labels
plott.xlabel("X-axis")
plott.ylabel("Y-axis")
# Add title
plott.title("Scatter Plot with Custom Axis Limits and Tick Labels")
# Display the plot
plott.show()
Here, we restrict the range of both the x and y axes by employing the plt.xlim() and plt.ylim() functions. In order to specify where the tick marks will appear along each axis, use the plt.xticks() and plt.yticks() routines.
Figure Class
Matplotlib’s Figure class offers a more sophisticated method of handling numerous plots in a single visualisation. You may use it to develop and organise subplots. Let’s make a diagram with two plots running parallel to one another:
# Generate data
x_axis = numpyy.linspace(10, 5 * numpyy.pi, 50)
y_sine = numpyy.sin(x_axis)
y_cosine = numpyy.cos(x_axis)
y_axis = numpyy.sin(x_axis)
# Create a figure with two subplots
figuree, axes = plott.subplots(1, 2, figsize=(12, 6))
# Plot the sine wave
axes[0].plot(x_axis, y_sine)
axes[0].set_title("Sine Wave Figure Data Flair")
# Plot the cosine wave
axes[1].plot(x_axis, y_cosine)
axes[1].set_title("Cosine Wave")
# Display the plots
plott.show()
Here, we use plt.subplots() to generate a chart with two adjacent subplots. The figure’s dimensions can be set with the figsize option.
The Plot Is Saved
It’s common practice to export finished visualisations as images for use in reports and presentations. Plots made using Pyplot can be exported to many different file types, including PNG, JPEG, PDF, and many more. Let’s create a PNG picture of our sine wave plot:
# Generate data
x_axis = numpyy.linspace(10, 5 * numpyy.pi, 50)
y_axis = numpyy.sin(x_axis)
# Create the plot
plott.plot(x_axis, y_axis)
# Add title
plott.title("Sine Wave Figure")
# Save the plot as a PNG image
plott.savefig("sine_wave_plot.png")
# Display the plot
plott.show()
Here, the plot is saved as a PNG file in the current working directory with the name “sine_wave_plot.png” using the plt.savefig() function.
Conclusion
In this in-depth tutorial, we looked at the powerful and flexible data visualisation tool Pyplot in Matplotlib has to offer. Using Matplotlib’s pyplot module, we learned how to generate a variety of graphical representations, including line plots, scatter plots, bar plots, histograms, and pie charts. Plots may be made more aesthetically pleasing and instructive by the use of different colours, markers, labels, and styles.
The capabilities of Matplotlib are much more than those covered in this introduction. I hope you’ll take some time to investigate Matplotlib and all the cool things it can do.
Matplotlib is an indispensable tool for data visualisation in Python, enabling you to express your creativity and effectively convey insights from your data thanks to its vast documentation and active user community. Let the planning begin!
