Bar Chart Basics with Python’s Matplotlib
By: Thiago Carvalho
Tags: Python, Bar Chart, Simple, Matplotlib, Data Visualization

Now we can use Matplotlib to create our Figure and axes.

I declared two variables, one to hold the Figure and the other to hold the Axes. Then I used .subplots to create one subplot with a figure size of 12x6.

Cool… Cool, cool, cool… But where’s the data?

Once we defined how we want our Figure to be, we can start plotting our data. Matplotlib makes this easy to do. We only need to choose which type of chart we want to use (.plot, .scatter, .bar, .pie, and others), pass the required parameters which usually is x and y, and let the default setting do the rest.

And voila! Now we have an ugly chart!

But seriously, it’s not so bad. It’s showing our data as we intended to. The X-axis displays the names as it is in df_filter.Name and the bars have the length as specified in df_filter.Global_Sales.

Visually our chart doesn’t look right. The names are all overlapping each other. There’s no way a viewer will know what these numbers represent or what the purpose of our graph is. So let’s fix it!

Let’s see what we can do about those names. Maybe if we increase our figure size, they’ll have more space.

Ok… We fixed the names, but now the labels are so small that it looks like a bar chart made for ants.

There are many other things we could try like, increase the font-size of the ticks, and find a balance between font-size and figure-size, we can try to edit the names of the games and maybe add some “

” to break them in more than one line, and we can try to rotate the names.

The first two options, adjusting sizes and renaming labels, maybe very practical since we don’t have much data to display, but if we were displaying 20, or maybe 100 columns, this would require too much time and effort.

So let’s try to rotate them.

We can use plt.xticks() to define the rotation parameter, which is the number of degrees we want to rotate. The problem now is that the text is centred by default, and we don’t want that. We want it to end on the tick marking.

That’s better, I’ve used “ha” which is the horizontal alignment parameter to align the texts to the right side of the tick marks.

But why are we rotating the text, since we can turn the bars? Even though we got to fix the spacing between the labels, the chart is still not aesthetically pleasant. It’s inconvenient to read the texts in an angle, so let’s see how it looks like with horizontal bars.

We’re getting closer, and this looks way better than the previous chart. We can read the texts and compare the sizes of the bars.

The only difference between this chart and the first one is their type, in the first example, I used .bar(labels, length), and now I’m using .barh(labels, width). We can also sort the values in our data frame to descending to have higher sales at the top of the chart.

All right, our chart is almost ready. Now we can start to make it look more visually apeling. To do so, we can try to follow some principles of data visualization. In my opinion, the most important here is removing distractions and not letting our viewers take guesses on the values.

In our x-axis, for example, we can estimate the size of a bar by the scale on the bottom, but that is as far as we can go.

The difference between “Tetris” and “New Super Mario Bros.” is 250 million copies, it’s a relatively small difference since we’re talking about billions, but still quite relevant.

We can remove the spines with their set_visible() function, and remove the x-ticks by passing an empty array to it.

Now we can use a “for” loop to go through the Global_Sales values and print them in our chart.

The function we’re using to plot the texts is “Text”. It requires values for x, y, and a string to be displayed.

Now it’s looking almost perfect, but I still need to add a meaningful title, increase the fonts, and vertically align the numbers to the bars.

Great! Now we have a bar chart that suits our data, we can visually observe de differences in sales by the sizes of our bars, and we have the exact number in billions of copies right by them, so we don’t need to make assumptions. The names of the games are readable, and the title allows us to understand the data.

What else can we do with bar charts?

There’s one more very common encoding used in bar charts that we haven’t used yet, color.