Adding error-bars to a plot
Adding error-bars to a plot is done by using the errorbar
method of the Axes
object.
Here, you have two DataFrames loaded: seattle_weather
has data about the weather in Seattle and austin_weather
has data about the weather in Austin. Each DataFrame has a column "MONTH"
that has the names of the months, a column "MLY-TAVG-NORMAL"
that has the average temperature in each month and a column "MLY-TAVG-STDDEV"
that has the standard deviation of the temperatures across years.
In the exercise, you will plot the mean temperature across months and add the standard deviation at each point as y errorbars.
This exercise is part of the course
Introduction to Data Visualization with Matplotlib
Exercise instructions
- Use the
ax.errorbar
method to add the Seattle data: the"MONTH"
column as x values, the"MLY-TAVG-NORMAL"
as y values and"MLY-TAVG-STDDEV"
asyerr
values. - Add the Austin data: the
"MONTH"
column as x values, the"MLY-TAVG-NORMAL"
as y values and"MLY-TAVG-STDDEV"
asyerr
values. - Set the y-axis label as
"Temperature (Fahrenheit)"
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
fig, ax = plt.subplots()
# Add Seattle temperature data in each month with error bars
ax.errorbar(____, ____, ____)
# Add Austin temperature data in each month with error bars
____
# Set the y-axis label
____
plt.show()