• 8 heures
  • Moyenne

Ce cours est visible gratuitement en ligne.

course.header.alt.is_certifying

J'ai tout compris !

Mis à jour le 22/01/2024

Customize Your Charts Using Matplotlib

 Chapter banner

So far, we’ve created a number of functional charts. However, they don’t all conform to the best practices we mentioned previously.

In almost all cases, there are elements missing that would make them easier to read, such as the title, axis titles, or the legend. Now we’re going to see how we can add these elements using Matplotlib.

Update External Elements

Firstly, let’s focus on the “external” elements of a chart. If we go back to the chart showing revenue by branch:

Bar chart showing total revenue on the y-axis and branches on the x-axis
Chart showing revenue by branch

So, we need to add a main title and at least an axis title for one of the axes. To do this, we’re going to do the same as we did before: apply additional functions to our chart window.

I suggest you watch this video, which brings it all together:

Update Internal Elements

So, now we have a complete chart with clear explanatory notes. There are other options we can use to clarify our visuals or make them more appealing to the eye.

There are a number of ways of playing around with various internal chart components. You’ve already encountered one of them, when we displayed values as text on pie plots and bar plots. You can also play around with ticks, grid style and background colors.

Let’s take the following chart:

Bar graph showing total revenue on the y-axis and branches on the x-axis
Chart showing revenue by branch

Let’s make some changes so it looks like this:

Bar chart showing total revenue on the y-axis and branches on the x-axis with updated external elements (scale, grid, background color)
Chart showing revenue by branch with updated external elements

Ready? Let’s take a look at it together:

Over to You!

Over to you exercise banner

Background

Monthly profits by loan type for the year 2021 have just been released at the branch where you work. As part of the monthly reporting cycle, you’ve been asked to create a specific chart to show profits changing over time by loan type.

Guidelines

Here’s the chart that was produced last year:

3-line curve graph to update with dates on the x-axis and net profit on the y-axis
Chart to update for this exercise

We’d like to reproduce this visual using data for the year 2021.

You’ll find the exercise via this link.

Check Your Work

Now you can compare your result with the solution.

Let’s Recap

  • Add external elements to a chart using the various functions provided in Matplotlib:

    • A chart title, using the  title  function

    • A legend, using the  legend  function

    • Axis labels, using  xlabel  and  ylabel

  • Update various internal elements on your visual:

    • Ticks, using the  xticks  or  yticks  function

    • Add text to a specific location using the text function

    • Update the grid style using  grid

    • Update specific chart elements using the  rc  function

Now let’s take a look at the second key library used for dataviz in Python: Seaborn.

Exemple de certificat de réussite
Exemple de certificat de réussite