avatarEsteban Thilliez

Summarize

Matplotlib Basics — Part. 2 — Scatter/Bars/Histograms/Pie Charts

Photo by Nick Brunner on Unsplash

The previous article was a quick guide to Matplotlib basics. Most of the things you can apply to a line chart can also be applied to other charts, like scatter plots, bar charts, histograms, etc…

So after seeing how you can create basic plots with Matplotlib, we’ll see how you can create other types of charts.

Scatter Plots

A scatter plot is a type of chart that is used to show the relationship between two numerical variables. By looking at a scatter plot, you can quickly see whether there is a positive relationship, a negative relationship, or no relationship at all between the two variables.

It uses dots to represent the values for each variable, and the position of the dots on the x and y axes shows the relationship between the two variables.

To create a scatter plot using Matplotlib in Python, you can use the scatter method and create it as if you were creating a line chart:

import matplotlib.pyplot as plt


x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

plt.scatter(x, y)
plt.show()

Just a reminder of the previous article, to show how you can add some options:

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

plt.scatter(x, y, label='2x', color='red', marker='o', s=100, edgecolor='black')

plt.xlabel('x axis')
plt.ylabel('y axis')
plt.title('Scatter Plot')
plt.legend()
plt.show()

A real use case for a scatter plot could be to visualize the relationship between a company’s stock price and its quarterly earnings. You can use a scatter plot to see whether there is a positive or negative correlation between these two variables, which can help you make decisions about whether to invest in the company.

Bar Charts

A bar chart is a type of chart that is used to show the frequency or relative frequency of different values in a dataset. It uses bars to represent the values, and the length of the bars shows the frequency or relative frequency of each value.

By looking at a bar chart, you can quickly see which values are the most common and how the values compare to each other.

To create a bar chart using Matplotlib in Python, you can use the bar method:

x = ["A", "B", "C", "D"]
y = [1, 2, 3, 4]

plt.bar(x, y)
plt.show()

One use case for a bar chart is to show the results of an election. You can use a bar chart to display the number of votes each candidate received, and the length of the bars will show the relative popularity of each candidate. This can help you quickly see which candidate won the election and by how much.

You can also create multiple bars and prevent them from overlapping. To do this, you have to use the width parameter and modify the x-axis for each additional bar.

x = [1, 2, 3, 4]
xticks = ["Monday", "Tuesday", "Wednesday", "Thursday"]
y1 = [1, 2, 3, 4]
y2 = [2, 4, 1, 3]

width = 0.25

plt.bar(x, y1, width=width, label='y1')
plt.bar([x + width for x in x], y2, width=width, label='y2')

plt.xticks([x + width for x in x], xticks)
plt.show()

Histograms

A histogram is a type of chart that is used to show the distribution of a dataset. It uses bars to represent the values in the dataset, and the height of the bars shows the frequency or relative frequency of each value.

The purpose of a histogram is to identify any patterns or trends within the data. By looking at a histogram, you can quickly see the range of values in the dataset, the shape of the distribution, and any outliers or gaps in the data.

We can create histograms in matplotlib with the histogram method:

data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]

plt.hist(data)
plt.show()

When we look at a histogram, it may look a lot like a bar chart. However, a histogram and a bar chart are completely different.

A histogram is used to show the distribution of a dataset, while a bar chart is used to show the frequency or relative frequency of different values in a dataset.

The bars in a histogram are positioned along a continuous x-axis, and the width of the bars represents the range of values in the dataset. In contrast, a bar chart is typically used to show the frequency or relative frequency of discrete values, and the position of the bars on the x-axis is determined by the values themselves.

Despite their similarities, a histogram and a bar chart are two different types of charts that are used to represent different types of data.

One use case for a histogram is to show the distribution of grades in a class. You can use a histogram to display the number of students who received each grade, and the height of the bars will show the relative frequency of each grade. This can help you quickly see the overall performance of the class and identify any trends or patterns in the grades.

Pie Charts

A pie chart is a type of chart that is used to show the proportion of different values in a dataset. It uses a circular shape, divided into slices, to represent the data. The size of each slice represents the proportion of the value it represents, and the slices are usually labeled with the corresponding value.

By looking at a pie chart, you can quickly see which values make up the largest and smallest proportions of the dataset, and how the values compare to each other.

We can make a pie chart in matplotlib using the pie method:

labels = ["A", "B", "C", "D"]
sizes = [10, 20, 30, 40]

plt.pie(sizes, labels=labels)
plt.show()

There are some options specific to the pie charts, look at the following example:

plt.pie(sizes, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90, explode=(0, 0.1, 0, 0))

One use case for a pie chart is to show the market share of different companies in an industry. You can use a pie chart to display the percentage of the market that each company controls, and the size of each slice will show the relative size of each company’s market share. This can help you quickly see which companies are the largest and smallest in the industry, and how the market is divided among the companies.

Final Note

Now you know the basics of Matplotlib, and can use it to create professional-looking visualizations of your data, and you can use these visualizations to explore and understand your data.

Whether you are analyzing financial data, political data, or any other type of data, Matplotlib can help you make sense of your data and communicate your findings to others.

Matplotlib is a valuable tool for data analysis, and by mastering the basics, you can take your data analysis skills to the next level.

To explore more of my Python stories, click here! You can also access all my content by checking this page.

If you liked the story, don’t forget to clap, comment, and maybe follow me if you want to explore more of my content :)

You can also subscribe to me via email to be notified every time I publish a new story, just click here!

If you’re not subscribed to Medium yet and wish to support me or get access to all my stories, you can use my link:

Python
Matplotlib
Software Development
Data Science
Programming
Recommended from ReadMedium