Python Tutorial : Customizing plots with python matplotlib




[ad_1]

Learn how to make custom plots in Python with matplotlib: https://datacamp.com/courses/intermediate-python-for-data-science

Creating a plot is one thing. Making the correct plot, that makes the message very clear, is 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 a 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 lists contain more data, including projections until the year 2100, forecasted by the United Nations.

If we run this script, we already get 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() method, otherwise your customizations will not be displayed. If we run the script again, this time the axes are annotated.

We’re also going to add a title to our plot, with the title function. We pass the actual title, ‘World Population Projections’, 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. You can do this with the yticks() function. The first input is a list, in this example with the numbers zero up to ten, with intervals of 2. If we run this, the plot will change: the curve shifts up. Now it’s clear that already 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 yticks 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, great.

Finally, let’s add some more historical data to accentuate the 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 datapoints are added to the graph, giving a more complete picture.

Now that’s how you 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 the world development chart and become the next Hans Rosling!

Source


[ad_2]

Comment List

  • DataCamp
    November 29, 2020

    Not degree that is worth the money… You can dis ditches and make more that a computer science degree.

  • DataCamp
    November 29, 2020

    Are you Russian?

  • DataCamp
    November 29, 2020

    How to write program for multiple smooth curve look like zigZag

  • DataCamp
    November 29, 2020

    i want to know more about adding legend.

  • DataCamp
    November 29, 2020

    thanks

  • DataCamp
    November 29, 2020

    No video has more clear demonstration than this one

Write a comment