Learning To Use MatPlotLib In Python

AI Writer
3 min readJan 14, 2023

Matplotlib is a powerful library for creating visualizations in Python. It provides a wide range of tools for creating various types of plots, including line plots, scatter plots, bar plots, and more. In this article, we will explore the basics of working with Matplotlib in Python, including how to create plots, customize them, and display them in different environments.

Creating Plots

The first step in creating a plot with Matplotlib is to import the library and the necessary modules. The most commonly used module is pyplot, which provides a simple interface for creating plots. For example:

import matplotlib.pyplot as plt

To create a simple line plot, you can use the plot() function. This function takes in x and y values, and it will create a line plot of those values. For example:

x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
plt.plot(x, y)
plt.show()

This will create a line plot of the x and y values and display it.

You can also create other types of plots like scatter plots, bar plots and histograms using the scatter(), bar() and hist() functions respectively. For example:

x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
plt.scatter(x, y)
plt.show()
x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
plt.bar(x, y)
plt.show()
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
plt.hist(x, bins=5)
plt.show()

Customizing Plots

Once you have created a plot, you can customize it by changing the colors, line styles, and other visual elements. For example, you can change the color of a line plot by passing a string argument to the color parameter of the plot() function. For example:

x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
plt.plot(x, y, 'r')
plt.show()

You can also add labels and titles to the plot, to make it more informative. For example:

x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
plt.plot(x, y)
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('My plot')
plt.show()

Displaying Plots

Matplotlib provides several ways to display plots, including in a Jupyter notebook, in a separate window, or in a web application.

In a Jupyter notebook, you can use the magic command %matplotlib inline to display plots directly in the notebook:

%matplotlib inline
x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
plt.plot(x, y)

In a separate window, you can use the show() function:

x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
plt.plot(x, y)
plt.show()

This will open a separate window with the plot and you can interact with it.

In a web application, you can use a library like Plotly or Bokeh, which are built on top of Matplotlib and provide interactive, web-based plots.

In conclusion, Matplotlib is a powerful library for creating visualizations in Python. It provides a wide range of tools for creating various types of plots and offers great flexibility for customizing plots and displaying them in different environments. By learning how to create plots, customize them, and display them, you can use Matplotlib to effectively visualize data in your Python programs.

--

--

AI Writer

I am a python programmer that is trying to help other people gain the skill of programming in python.