Log in from a computer to take this course

You'll need to log in from a computer to start Learn Data Visualization with Python. But you can practice or keep up your coding streak with the Codecademy Go app. Download the app to get started.

apple storegoogle store

We’ve learned how to display errors on bar charts using error bars. Let’s take a look at how we might do this in an aesthetically pleasing way on line graphs. In Matplotlib, we can use plt.fill_between to shade error. This function takes three arguments:

  1. x-values — this works just like the x-values of plt.plot
  2. lower-bound for y-values — sets the bottom of the shared area
  3. upper-bound for y-values — sets the top of the shared area

Generally, we use fill_between to create a shaded error region, and then plot the actual line over it. We can set the alpha keyword to a value between 0 and 1 in the fill_between call for transparency so that we can see the line underneath. Here is an example of how we would display data with an error of 2:

x_values = range(10) y_values = [10, 12, 13, 13, 15, 19, 20, 22, 23, 29] y_lower = [8, 10, 11, 11, 13, 17, 18, 20, 21, 27] y_upper = [12, 14, 15, 15, 17, 21, 22, 24, 25, 31] plt.fill_between(x_values, y_lower, y_upper, alpha=0.2) #this is the shaded error plt.plot(x_values, y_values) #this is the line itself plt.show()

This would give us a plot that looks like: fill_between

Having to calculate y_lower and y_upper by hand is time-consuming. If we try to just subtract 2 from y_values, we will get an error.

TypeError: unsupported operand type(s) for -: 'list' and 'int'

In order to correctly add or subtract from a list, we need to use list comprehension:

y_lower = [i - 2 for i in y_values]

This command looks at each element in y_values and calls the element its currently looking at i. For each new i, it subtracts 2. These opperations create a new list called y_lower.

If we wanted to add 2 to each element in y_values, we use this code:

y_upper = [i + 2 for i in y_values]



We have provided a set of data representing MatplotSip’s projected revenue per month for the next year in the variable revenue. Let’s plot these revenues against months as a line in script.py.


Make an axis object, store it in the variable ax, and then use it to set the x-ticks to months and the x-axis tick labels to be the months of the year, given to you in the variable month_names.


This data is a projection of future revenue. We don’t know that this will be the revenue, but it’s an estimate based on the patterns of past years. We can say that the real revenue will probably be plus or minus 10% of each value. Create a list containing the lower bound of the expected revenue for each month, and call it y_lower.

Remember that 10% less than a number would be either:

i - 0.1 * i


0.9 * i

You can use either of these in your list comprehension.


Create a list containing the upper bound of the expected revenue for each month, and call it y_upper.

Remember that 10% more than a number would be either:

i + 0.1 * i


1.1 * i

You can use either of these in your list comprehension.


Use fill_between to shade the error above and below the line we’ve plotted, with an alpha of 0.2.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?