Python Tutorial : Customizing plots with python matplotlib

Learn how to turn a basic population chart into a clear story using Matplotlib. Add axis labels, titles, and custom tick marks like "2b" for 2 billion, then append historical data from 1800 and 1850 to highlight rapid growth over the last 60 years.
Chapters
Creating a plot is one thing. Making the correct plot that makes the message very clear. That's the real challenge. For each visualization, you have many options. First of all, there are the different plot types. And for each plot, you can do an infinite number of customizations. You can change colors, shapes, labels, axes, and so on. The choice depends on one, the data, and two, the story you want to tell with this data. Since there are so many possible customizations, the best way to learn this is by example. Let's start with the code in this script to build a simple line plot. It's similar to the line plot we've created in the first video, but this time the year and pop list contain more data, including projections until the year 2100 forecasted by the United Nations. If we run this script, we already got a pretty nice plot. It shows that the population explosion that's going on will have slowed down by the end of the century. But some things can be improved. First, it should be clearer which data we are displaying, especially to people who are seeing the graph for the first time. And second, the plot really needs to draw the attention to the population explosion. The first thing you always need to do is label your axes. Let's do this by adding the XLABEL and YLABEL functions as inputs. We pass strings that should be placed alongside the axes. Make sure to call these functions before calling the show function. Otherwise your customizations will not be displayed. If we run the script again, this time the axes are annotated. Great. We're also going to add a title to our plot. With the title function, we pass the actual title WorldPopulationProjections as an argument. And there's the title. So using xLabel, yLabel and title, we can give the reader more information about the data on the plot. Now they can at least tell what the plot is about. To put the population growth in perspective, I want to have the Y axis start from zero. We can do this with the yticks function. The first input is a list in this example with the numbers 0 up to 10 with intervals of 2. If we run this, the plot will change. The curve shifts up. Now it's clear that in 1950 there were already about 2.5 billion people on this planet. Next, to make it clear we're talking about billions, we can add a second argument to the white dicks function, which is a list with the display names of the ticks. This list should have the same length as the first list. The tick 0 gets the name 0. The tick 2 gets the name 2b, the tick 4 gets the name 4b, and so on. By the way, B stands for billions here. If we run this version of the script, the labels will change accordingly. Awesome. Finally, let's add some more historical data to accentuate population explosion in the last 60 years. On Wikipedia, I found the world population data for the years 1800, 1850, and 1900. I can write them in list form and append them to the pop and year lists with the plus sign. If I now run the script once more, three data points are added to the graph, giving a more complete picture. Now that's how we turn an average line plot into a visual that has a clear story to tell. Over to you. Now head over to the exercises. Gradually customize your world development bubble chart and become the next Hans Rosling.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







