snowflake

Black Friday

snowflake

A New Version of Matplotlib Cheat Sheet


There is a famous English adage, “A picture is worth a thousand words”, meaning that a single image sometimes explains complex or multiple ideas way better than words. Namely, data visualization is a great medium to communicate with our audience.

Data visualization is useful for not only presenting the insights gained but also exploring the data at hand. Understanding the structure of the data, detecting outliers, identifying trends and clusters, choosing a model to apply, evaluating the model output, and finally presenting results can be effectively done via data visualization. This process is also known as EDA or Exploratory Data Analysis.


What is Matplotlib Cheat Cheet?

Python provides many visualization packages for EDA, however, the Matplotlib library is the mother of all of them. Matplotlib.org defines Matplotlib as a “comprehensive library for creating static, animated, and interactive visualizations in Python.”

Since Matplotlib is a very comprehensive library, there are many cheat sheets, especially for beginners, to make plotting easier. However, sometimes they can also be a little bit confusing due to using only “x” and “y” in the cheat sheet codes, rather than using real data.

Here we will explain Matplotlib plot types, how to plot with real data, when to use them, and finally provide a brand-new, broadly explained cheat sheet.


Learn Details of Matplotlib Plot Types

Let’s start with importing required libraries and creating a simple data frame.

1*mBdDnsgQUywfBjJ6yo hkQ

1- Line Plot:

Line plots are commonly used to display change over time, helping to determine the relationship between two sets of values.

The Matplotlib library enables us to use either a functional or object-oriented method to create a plot. We will use both methods in creating a line plot.

1*Yyzq7HpkS181bYjV Q1ZGg

a- Functional Method:

Although the code itself is self-explanatory, let’s explain it step by step.

  • · Define the x-axis and corresponding y-axis values.
  • · Plot them on canvas using .plot() function.
  • · Give a name to x-axis and y-axis using .xlabel() and .ylabel() functions.
  • · Give a title using .title() function.
  • · Use .legend() to label the lines in the plot. You can change the loc parameter inside .legend() to change the location of the legend.
1*FGoynCGUTN5Kh60GM4VlaA

In the Matplotlib Functional method, we can also create subplots. To do this, we use plt.subplot() function and assign the number of rows, the number of columns, and index values.

1*PY332RgCBzEMNKET5QzTkQ
1*4a8gCNwjfV g1mwSUlZ1PA

b- Object-Oriented Method:

Basically here we use “ax”, which is the object of the class “fig” to create a plot.

  • · Use fig, ax=plt.subplots() and ax.plot() to create figure and plot(s).
  • · Give a name to x-axis and y-axis using .set_xlabel() and .set_ylabel() functions.
  • · Give a title using .set_title() function.
  • · For vertical line use .axvline() and for horizontal line use .axhline() functions.
1*LwSDLb oRRXZbdlZt1d2NQ

Setting x_lim and y_lim:

If we want to focus on a specific area on the plot we can use .set_xlim() and .set_ylim() with a list of values to focus.

1* I12MdDSTd9X3dk3dZUVew
1*WPhYH3pqnFHKo7RRanGSHg

Creating a Subplot:

In order to create a subplot in the object-oriented method, we can use fig, ax=plt.subplots() and pass the number of rows and columns inside.

1*
1*Ee6uB7XI07PbJmHHMYc7FQ

2- Scatter Plot:

Scatter plots are commonly used for displaying the relationship between two variables.

1*1ySBT9 m4UZwBUomHlCNSA
1*QuqGiUYSlXtdGc9OADvXyA

3- Bar Plot:

Bar plots are the best option for representing comparative data. They are commonly used especially for performing a comparison across different subgroups of our data.

1*1jBrEu zvfWOgsL0HqXWpQ
1*WNTEYCIpmZKoFMQnyR7S4g

Stacked Bar Plot:

In order to create a stacked bar plot, we can use the bottom parameter inside ax.bar().

1*rw1tclCSzoIz5pFk jqChw
1*vjtPXSsca4 yNfEB9LuUpg

4- Pie Chart:

Pie charts are best used when we need to visualize the part-to-whole relationship in our data. They easily convey the proportions of the subcategories comprising the data.

1*UFuzVUxb3gZngT p Pn95A
1*zYC9LcZ4gggyXfZkTUTzNw

5- Box Plot:

Box plots are the best way of summarizing a set of data measured on an interval scale and they provide information from a five-number summary: first quartile, median, third quartile, and maximum score.

1*E9uITGPlkNqJzKT 9pIkPg

6- Histogram:

Histograms display the distribution of our data by showing the number of data points that fall within a specified range of values (bins) and they are especially useful in understanding the general distribution of data and detecting outliers.

1*qYl1SIO9ZU0oQycQ31vuBA
1*SASBQ9FKNCuEYBictBD8PQ

Final Thoughts About Matplotlib

  • Data visualization and storytelling with data are essential skills for every data scientist to convey insights gained from analysis.
  • Python’s Matplotlib library is a very powerful tool to create visualizations.
  • Knowing when to use what kind of plot is crucial for communication with the audience.

You can download the cheat sheet containing all the codes from here.

Search

Table of Contents

Send Us A Message

Which Tech Career is Right for You?

You can have an idea about the most suitable IT path for you by spending
2 minutes on the quiz we have prepared specially for our visitors. Also, complete the quiz and get a special discount coupon for Clarusway IT courses!

Picture of Clarusway
Clarusway
black friday 25 off
Enter code BF2024 to get up to

50% Off

For more details see Terms&Conditions