oldoc63 / learningDS

Learning DS with Codecademy and Books
0 stars 0 forks source link

Fill Between #487

Open oldoc63 opened 1 year ago

oldoc63 commented 1 year ago

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:

  1. x-values - this works just like the x-values of plt.plot()
  2. lower-bound for y-values - sets the bottom of the shaded area
  3. upper-bound for y-values - sets the top of the shaded 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.

oldoc63 commented 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:

oldoc63 commented 1 year ago

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.

oldoc63 commented 1 year ago
  1. 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.
oldoc63 commented 1 year ago
  1. Make an axis object, store it in a 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 months_names.
oldoc63 commented 1 year ago
  1. 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 or 0.9 * i. You can use either of these in your list comprehension.
oldoc63 commented 1 year ago
  1. Create a list containing the upper bound of the expected revenue for each month, and call it y_upper.
oldoc63 commented 1 year ago
  1. Use .fill_between() to shade the error above and below the line we've plotted, with an alpha of 0.2.