Open oldoc63 opened 1 year ago
Having to calculate y_lower and y_upper by hand is time consuming. In we just try to subtract 2 from y_values, we will get and error. In order to correctly add or subtract from a list, we need to use list comprehension:
This command looks at each element in y_values and calls the element its currently looking at i
. For each new i , it subtract/add 2. This operations create a new lists.
revenue
. Let's plot these revenues against months as a line.i - 0.1 * i
or 0.9 * i
. You can use either of these in your list comprehension..fill_between()
to shade the error above and below the line we've plotted, with an alpha of 0.2.
We've learned how to display errors on bar charts using error bars. Let's take a look at how we might to do this in a aesthetically pleasing way on line graphs. In Matplotlib, we can use plt.fill_between() to shade error. This function takes three arguments:
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.