Tutorial: Creating graphics in Python using matplotlib

Python is a programming language that has multiple libraries or modules to perform different processes.

Whether in the workplace or academic, sometimes it is necessary to show information or data in a way that is more understandable to other people., with this we make use of the graphs.

In Python we mainly have the matplotlib library, with which we can make multiple types of graphs in a simple way and with few lines of code.

If you don't have it installed, you can do it with the following command ‘pip install matplotlib’ o ‘pip3 install matplotlib’

To start graphing, we need to import the necessary libraries:

We will use these libraries in all the examples, so I recommend to always keep them in mind.

Simple Bar Graph

In bar graphs, we always seek to represent quantities as a function of values, for example the number of men or women on a certain day, sales quantity, etc…

To create bar graphs in matplotlib, we make use of the 'bar' function, with which we indicate that the final graph must be a bar graph.

The first thing to do is define lists or sets of values ​​to represent in the graph, in this case two lists are defined, one to represent ‘countries’ and another to represent 'sales’

Attention: In these cases, both the list ‘countries’ and ‘sales’ must contain the same number of values, otherwise an error will occur

We retrieve a couple of values ​​from the ‘subplots’ function, this will help us to add various characteristics to the graph.

We can add 'tags’ to the graph, this helps to better understand the included values, We do this based on the ‘set_xlabel functions’ o ‘set_ylabel’ to add labels on the X or Y axes, y ‘set_title’ to add a title to the chart.

Finally, we build the graph using the values ​​of ‘countries’ and ‘sales’ as the X and Y axis, this is done with the 'bar method’ which receives the aforementioned values ​​as parameters. We show the graph with the 'show' method. The complete code is below and the resulting graph:

Horizontal Bar Graph

A horizontal bar graph, it is similar to that of bars, except that the bars are oriented horizontally and the X and Y values ​​are interspersed. For this example we will try to show in a graph the popularity of various programming languages ​​based on the number of users they have..

First we define the data set: 'idioms’ containing various programming languages, and ‘quantity_use’ What are the users of each language?. Always remember that both data sets must have the same size or number of values. Besides we also get a ‘y_pos’ containing the position of each language.

Having the basics, we go on to create the graph, in this case we use the 'barh method’ which allows to create the bar graph in horizontal position. We pass the parameters where Y is equal to the positions from lowest to highest of each language, and X is the number of users, the 'align parameter’ allows you to align the values ​​to the center

Finally we can add labels to represent the values. And after this show the graph.

The complete code and the resulting graph is as follows:

Double Bar Graph

The double bar graph allows us, in addition to graphically displaying a set of data, perform a comparison between two pairs of values.

For this example, we will seek to show the attendance of men and women from Monday to Friday in two different sets of bars.

We generate the data sets:

We create the 'bars’ for men and women:

In addition to adding the usual tags, we will add a 'legend’ this is a small box that allows to understand the elements in the graph in a simpler way.

We add a function that allows us to add labels individually to each bar:

We graffiti:

The final code and the resulting graph is as follows:

Linear Graph

The line graph is in a way, easier to graph, In addition to being able to represent values ​​in time series or based on another value. In this example, we can represent mathematical values ​​as functions of X.

We generate the intermediate values ​​between 0 Y 2, the more values ​​there are, the smoother’ you will see the straight.

We create the graph by passing the values ​​to the ‘plot method’ with which we make the linear graphs. In this case the values ​​of Y, are modified to represent distinct values. Finally we label and graph:

Linear Graph with Mathematical Functions

We can also graphically represent other mathematical functions, as in this case a sen(x).

Pie Chart

Something more ‘colorful’ and representative to display data based on a segmentation, are the pie charts, For this example we seek to represent which means of transport is the most used by a set of 100 persons.

As in every example, we define a data set ‘media_transporte’ y ‘sizes’. What's more, we add a new set called explode, with which we can 'point’ which dataset can be highlighted.

We add the corresponding labels and legend. We use the ‘pie method’ with which we generate the pie-type graphs, we pass the parameters of the data sets and plot.

The complete code and the resulting graph is as follows:

Scatter Plot

Another of the multiple graphs that we can make with matplotlib are the scatter graphs, that help to understand the display of various data sets and the relationship between them.

We can make multiple graphs to represent data in a simple way and with few lines of code. We can also make 3D graphics but this will be in another tutorial.

Tell us what you think, leave your comment and share if you think someone else could be of help.

4 thoughts on “Tutorial: Creating graphics in Python using matplotlib”

1. Luis says:

Excellent your work, Thanks a lot.

2. orlando says:

This tutorial helped me to make my first graphs, very well explained. Thanks

3. Osvaldo Ponce S. says:

Very good tutorial, very well explained!

Students of the Data Science with Python course appreciate it.

Thanks a lot.!
Greetings.

4. Walquer Huacani Calsin says:

Very interesting friend, Thank you, you could upload applied to ArcGis, MineSigth