Python Friday #167: Often used Diagrams for Matplotlib

Different types of data need different forms for a graphical representation. In this post we explore the most often used types of diagrams in Matplotlib.

This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.

 

Line plot / Line chart

The line chart is the diagram we used in the last posts and we can create it with this code:

This gives us the familiar looking plot:

A line plot

 

Bar chart

If we have categorical values, we can use this code to create a bar chart:

This gives us a bar chart with vertical bars:

A vertical aligned bar chart

If we switch from ax.bar() to ax.barh() we change the orientation from vertical to horizontal:

A bar chart with horizontal bars

 

Pie chart

We can create a pie chart with this code:

The result is a circle with proportional coloured segments that correspond to our values:

A pie chart for cats and dogs.

 

Scatter plot

The scatter plot draws a point for each data point we have:

Instead of lines or bars we get dots representing our data:

Each data point is represented with a dot.

In the documentation for Matplotlib you find a more interesting example of the possibilities you have with scatter plots. We can set the size and the colour to produce something like this:

A scatter plot with variable colours and sizes for the data points

 

Histograms

A histogram is a helpful way to see how the data is distributed:

This shows us that the numbers 3 and 5 are the most frequent in our data set:

The numbers 3 and 5 have the highest bars in the histogram

 

Box plot

The box plot is a helpful way to see the distribution of data:

This gives us a plot that can abstract a lot of values while it still shows us the variability of our data:

The box plot shows the range in which the values are

What does the plot tell us? The whiskers (the lines on top and at the bottom) represents the maximal and minimal values, while the line in the middle of the box is the median. The distance from the median to the edge of the box and from the box to the whiskers each cover a quarter of the values:

The box plot shows us a quarter of the data between the box and the whiskers and inside the box from the edge of the box to the median.

In our example we can see that we have more smaller values than higher values. For more details to box plots and how to interpret them you should read this article.

If we want to have a horizontal box plot, we need to set the value for vert to false (vert = vertical):

The box plot is now horizontally aligned

 

Next

With these different types of diagrams, we can illustrate most of the data we work with. If it is not enough, there are plenty more visualisations to choose from in the documentation. Next week we explore ways to customise how our plots get drawn.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.