Introduction to Matplotlib
Introduction to Matplotlib
pyplot as plt
fig, ax = plt.subplots()
plt.show()
seattle_weather["MONTH"]
1 Jan
2 Feb
3 Mar
...
12 Dec
seattle_weather["MLY-TAVG-NORMAL"]
1 42.1
2 43.4
3 46.6
...
12 41.1
ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL"])
plt.show()
ax.plot(austin_weather["MONTH"], austin_weather["MLY-TAVG-NORMAL"])
plt.show()
fig, ax = plt.subplots()
ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL"])
ax.plot(austin_weather["MONTH"], austin_weather["MLY-TAVG-NORMAL"])
plt.show()
ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-PRCP-NORMAL"])
plt.show()
fig, ax = plt.subplots()
ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL"], marker="v", linestyle="--")
plt.show()
fig, ax = plt.subplots()
ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL"], marker="v", linestyle="None")
plt.show()
fig, ax = plt.subplots()
ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL"], marker="v", linestyle="--", color=
plt.show()
ax.set_xlabel("Time (months)")
ax.set_ylabel("Average temperature (Fahrenheit degrees)")
plt.show()
ax.set_title("Weather in Seattle")
plt.show()
fig, ax = plt.subplots(3, 2)
plt.show()
plt
plt
plt.subplots()
Axes.plot()
plt.show()
Axes.plot()
Axes.plot()
plt.show()
Axes.plot()
Axes.plot()
plt.show()
marker
"o"
"v"
linestyle
"--"
"None" linestyle
"r"
set_
set_xlabel
set_ylabel
set_title
subplots
Figure
Axes
ax
plot
sharey
sharey
date,co2,relative_temp
1958-03-06,315.71,0.1
1958-04-06,317.45,0.01
...
2016-12-06,404.45,0.81
DatetimeIndex
climate_change.index
DatetimeIndex(['1958-03-06', '1958-04-06', ..., '2016-12-06'], dtype='datetime64[ns]', name='date', length
fig, ax = plt.subplots()
ax.plot(climate_change.index, climate_change['co2'])
ax.set_xlabel('Time')
ax.set_ylabel('CO2 (ppm)')
plt.show()
sixties = climate_change["1960-01-01":"1969-12-31"]
fig, ax = plt.subplots()
ax.plot(sixties.index, sixties['co2'])
ax.set_xlabel('Time')
ax.set_ylabel('CO2 (ppm)')
plt.show()
sixty_nine = climate_change["1969-01-01":"1969-12-31"]
fig, ax = plt.subplots()
ax.plot(sixty_nine.index, sixty_nine['co2'])
ax.set_xlabel('Time')
ax.set_ylabel('CO2 (ppm)')
plt.show()
fig, ax = plt.subplots()
ax.plot(climate_change.index, climate_change["co2"])
ax.plot(climate_change.index, climate_change["relative_temp"])
ax.set_xlabel('Time')
ax.set_ylabel('CO2 (ppm) / Relative temperature')
plt.show()
fig, ax = plt.subplots()
ax.plot(climate_change.index, climate_change["co2"])
ax.set_xlabel('Time')
ax.set_ylabel('CO2 (ppm)')
ax2 = ax.twinx()
ax2.plot(climate_change.index, climate_change["relative_temp"])
ax2.set_ylabel('Relative temperature (Celsius)')
plt.show()
fig, ax = plt.subplots()
ax.plot(climate_change.index, climate_change["co2"], color='blue')
ax.set_xlabel('Time')
ax.set_ylabel('CO2 (ppm)', color='blue')
ax.tick_params('y', colors='blue')
ax2 = ax.twinx()
ax2.plot(climate_change.index, climate_change["relative_temp"], color='red')
ax2.set_ylabel('Relative temperature (Celsius)', color='red')
ax2.tick_params('y', colors='red')
plt.show()
ax2.annotate(">1 degree",
xy=(pd.Timestamp('2015-10-06'), 1),
xytext=(pd.Timestamp('2008-10-06'), -0.2),
arrowprops={"arrowstyle":"->", "color":"gray"})
index_col
twinx
plot
set_ylabel
tick_params
plot_timeseries def
plot set_xlabel set_ylabel tick_params
annotate
xytext
arrowprops
medals = pd.read_csv('medals_by_country_2016.csv', index_col=0)
fig, ax = plt.subplots()
ax.bar(medals.index, medals["Gold"])
plt.show()
fig, ax = plt.subplots()
ax.bar(medals.index, medals["Gold"])
ax.set_xticklabels(medals.index, rotation=90)
ax.set_ylabel("Number of medals")
plt.show()
fig, ax = plt.subplots()
ax.bar(medals.index, medals["Gold"])
ax.bar(medals.index, medals["Silver"], bottom=medals["Gold"])
ax.set_xticklabels(medals.index, rotation=90)
ax.set_ylabel("Number of medals")
plt.show()
fig, ax = plt.subplots()
ax.bar(medals.index, medals["Gold"])
ax.bar(medals.index, medals["Silver"], bottom=medals["Gold"])
ax.bar(medals.index, medals["Bronze"], bottom=medals["Gold"] + medals["Silver"])
ax.set_xticklabels(medals.index, rotation=90)
ax.set_ylabel("Number of medals")
plt.show()
fig, ax = plt.subplots()
ax.bar(medals.index, medals["Gold"], label="Gold")
ax.bar(medals.index, medals["Silver"], bottom=medals["Gold"], label="Silver")
ax.bar(medals.index, medals["Bronze"], bottom=medals["Gold"] + medals["Silver"], label="Bronze")
ax.set_xticklabels(medals.index, rotation=90)
ax.set_ylabel("Number of medals")
ax.legend()
plt.show()
fig, ax = plt.subplots()
ax.bar("Rowing", mens_rowing["Height"].mean())
ax.bar("Gymnastics", mens_gymnastics["Height"].mean())
ax.set_ylabel("Height (cm)")
plt.show()
fig, ax = plt.subplots()
ax.hist(mens_rowing["Height"])
ax.hist(mens_gymnastics["Height"])
ax.set_xlabel("Height (cm)")
ax.set_ylabel("# of observations")
plt.show()
ax.hist(mens_rowing["Height"], label="Rowing")
ax.hist(mens_gymnastics["Height"], label="Gymnastics")
ax.set_xlabel("Height (cm)")
ax.set_ylabel("# of observations")
ax.legend()
plt.show()
ax.hist(mens_rowing["Height"], label="Rowing", bins=5)
ax.hist(mens_gymnastics["Height"], label="Gymnastics", bins=5)
ax.set_xlabel("Height (cm)")
ax.set_ylabel("# of observations")
ax.legend()
plt.show()
ax.hist(mens_rowing["Height"], label="Rowing",
bins=[150, 160, 170, 180, 190, 200, 210])
ax.hist(mens_gymnastics["Height"], label="Gymnastics",
bins=[150, 160, 170, 180, 190, 200, 210])
ax.set_xlabel("Height (cm)")
ax.set_ylabel("# of observations")
ax.legend()
plt.show()
ax.hist(mens_rowing["Height"], label="Rowing",
bins=[150, 160, 170, 180, 190, 200, 210],
histtype="step")
ax.hist(mens_gymnastics["Height"], label="Gymnastics",
bins=[150, 160, 170, 180, 190, 200, 210],
histtype="step")
ax.set_xlabel("Height (cm)")
ax.set_ylabel("# of observations")
ax.legend()
plt.show()
fig, ax = plt.subplots()
ax.bar("Rowing", mens_rowing["Height"].mean(), yerr=mens_rowing["Height"].std())
ax.bar("Gymnastics", mens_gymnastics["Height"].mean(), yerr=mens_gymnastics["Height"].std())
ax.set_ylabel("Height (cm)")
plt.show()
fig, ax = plt.subplots()
ax.errorbar(seattle_weather["MONTH"],
seattle_weather["MLY-TAVG-NORMAL"],
yerr=seattle_weather["MLY-TAVG-STDDEV"])
ax.errorbar(austin_weather["MONTH"],
austin_weather["MLY-TAVG-NORMAL"],
yerr=austin_weather["MLY-TAVG-STDDEV"])
ax.set_ylabel("Temperature (Fahrenheit)")
plt.show()
fig, ax = plt.subplots()
ax.boxplot([mens_rowing["Height"], mens_gymnastics["Height"]])
ax.set_xticklabels(["Rowing", "Gymnastics"])
ax.set_ylabel("Height (cm)")
plt.show()
fig, ax = plt.subplots()
ax.scatter(climate_change["co2"], climate_change["relative_temp"])
ax.set_xlabel("CO2 (ppm)")
ax.set_ylabel("Relative temperature (Celsius)")
plt.show()
eighties = climate_change["1980-01-01":"1989-12-31"]
nineties = climate_change["1990-01-01":"1999-12-31"]
fig, ax = plt.subplots()
ax.scatter(eighties["co2"], eighties["relative_temp"],
color="red", label="eighties")
ax.scatter(nineties["co2"], nineties["relative_temp"],
color="blue", label="nineties")
ax.legend()
ax.set_xlabel("CO2 (ppm)")
ax.set_ylabel("Relative temperature (Celsius)")
plt.show()
fig, ax = plt.subplots()
ax.scatter(climate_change["co2"], climate_change["relative_temp"],
c=climate_change.index)
ax.set_xlabel("CO2 (ppm)")
ax.set_ylabel("Relative temperature (Celsius)")
plt.show()
> "In this chapter, we will focus on quantitative comparisons between parts of the data."
- The dataset contains information about the number of medals won by several countries in the **2016 Olympic Ga
- Though the dataset is small, making comparisons between countries can be challenging without visualization
- The final chart displays all three medal categories (Gold, Silver, Bronze) stacked for each country.
- The completed figure includes a legend that clarifies the meaning of each bar color.
This encourages students to apply what they've learned by creating their own bar chart based on the concepts di
> "Bar charts show us the values of one variable across different conditions, such as different countries. But
> "We can customize scatter plots in a manner that is similar to the customization that we introduced in other
> "You can see that the relationship between temperatures and carbon dioxide didn't change much during these ye
> "In the exercises, go ahead and practice making your own scatter plots."
> "Bar-charts show us the value of a variable in different conditions. Now, we're going to look at histograms.
### Histograms
> "Because the x-axis label no longer provides information about which color represents which variable, labels
- To label a variable:
- Use the **label** keyword argument in the hist method.
- Call the **legend** method before **plt.show()** to display the legend.
- Allows clearer visibility of data, confirming there are rowers with a height of less than 180 cm.
## Statistical Plotting
Statistical plotting is a set of methods for using visualization to make comparisons. It allows us to formalize
**Error Bars**
Error bars are additional markers on a plot or bar chart that provide information about the distribution of the
- **Purpose:** Summarize the distribution in one number, such as the standard deviation.
- **Example Data:** Heights of medalists in the 2016 Olympic Games.
**Implementation in Bar Chart**
To add error bars to a bar chart, use the `ax.bar` method:
- **Arguments:**
- `x`: Position of the bars
- `y`: Mean of the data (e.g., mean of the "Height" column)
- `yerr`: Standard deviation of the data (e.g., standard deviation of the "Height" column)
Error bars can also be added to line plots using the `Axes.errorbar` method.
This method adds vertical markers to the plot, indicating variability in the data.
**Boxplot**
A boxplot is a statistical visualization technique invented by John Tukey, known as a pioneer in data science
**Implementation**
To create a boxplot:
- Use the boxplot method of the Axes object.
- Pass a list of sequences (e.g., "Height" columns from men's rowing and gymnastics).
- Label the y-axis separately.
### Exercises
Students are encouraged to create their own statistical visualizations to apply the techniques discussed
plt.style.use("ggplot")
plt.style.use("bmh")
plt.style.use("seaborn-colorblind")
fig, ax = plt.subplots()
ax.bar(medals.index, medals["Gold"])
ax.set_xticklabels(medals.index, rotation=90)
ax.set_ylabel("Number of medals")
plt.show()
fig.savefig("gold_medals.png")
ls
fig.savefig("gold_medals.jpg")
fig.savefig("gold_medals.jpg", quality=50)
fig.savefig("gold_medals.svg")
fig.savefig("gold_medals.png", dpi=300)
fig.set_size_inches([5, 3])
fig.set_size_inches([3, 5])
sports = summer_2016_medals["Sport"].unique()
print(sports)
fig, ax = plt.subplots()
ax.set_ylabel("Height (cm)")
ax.set_xticklabels(sports, rotation=90)
plt.show()
> "This chapter will focus on creating visualizations that you can share with others and incorporate into autom
> "To see what that means, let's look at one of the figures we created in a previous lesson."
- The overall style of the figure can be changed to significantly alter its appearance.
- Example: A figure showing average temperatures in Seattle and Austin as a function of the months of the year
> "If instead, we add this line of code before the plotting code, the figure style will look completely differe
- Adding a specific line of code allows for the emulation of styles from other libraries, such as **ggplot
- The chosen style affects multiple elements:
- Different colors
- Different fonts
- An added gray background with a faint white grid marking the x-axis and y-axis tick locations
- The selected style applies to all figures in the current session until changed.
> "For example, to go back to the default style, you would run plt.style.use('default')."
- A range of styles can be explored through a dedicated webpage showcasing visualizations created with each sty
- **Visibility Considerations**:
- Dark backgrounds are typically discouraged unless necessary.
- **Color Considerations**:
- Use colorblind-friendly styles (e.g., **seaborn-colorblind**, **tableau-colorblind10**) to accommodate appr
- **Medium Considerations**:
- Design figures with the intended medium in mind:
- For printed reports, avoid colored backgrounds that consume more ink.
- Consider using the **grayscale** style for black-and-white printers to maintain visual differences
> "In the exercises, you'll practice selecting some of these styles for your own visualizations."
> "After you have created your visualizations, you are ready to share them with your collaborators, colleagues,
## A Figure to Share
- Example figure displays data about the **number of gold medals** won by various countries in the **2016
- The code previously used `plt.show()` to display the figure on screen.
- Use UNIX command `ls` to verify the saved file in the current working directory.
## Different File Formats
## Resolution
- Control image quality using the `dpi` keyword argument (dots per inch).
- Higher `dpi` results in higher quality; for example, setting `dpi = 300` yields high quality.
> "One of the strengths of Matplotlib is that, when programmed correctly, it can flexibly adapt to the inputs t
- **Flexibility**: Write functions and programs that automatically adjust based on input data.
- **Efficiency**: Automation allows for quicker figure creation compared to traditional graphical user interfac
- **Robustness**: Programs can inspect incoming data and change behaviors accordingly.
- **Reproducibility**: Automatic adjustments provide consistent behavior across different runs.
```python
unique_sports = df['Sport'].unique()
sport
sport_df
bar
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
plt.show()
seattle_weather["MONTH"]
1 Jan
2 Feb
3 Mar
4 Apr
5 May
6 Jun
7 Jul
8 Aug
9 Sep
10 Oct
11 Nov
12 Dec
seattle_weather["MLY-TAVG-NORMAL"]
1 42.1
2 43.4
3 46.6
4 50.5
5 56.0
6 61.0
7 65.9
8 66.5
9 61.6
10 53.3
11 46.2
12 41.1
ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL"])
plt.show()
ax.plot(austin_weather["MONTH"], austin_weather["MLY-TAVG-NORMAL"])
plt.show()
fig, ax = plt.subplots()
ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL"])
ax.plot(austin_weather["MONTH"], austin_weather["MLY-TAVG-NORMAL"])
plt.show()
ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-PRCP-NORMAL"])
plt.show()
ax.set_xlabel("Time (months)")
plt.show()
ax.set_title("Weather in Seattle")
plt.show()
fig, ax = plt.subplots(3, 2)
plt.show()
ax[0, 0].plot(seattle_weather["MONTH"], seattle_weather["MLY-PRCP-NORMAL"], color='b')
plt.show()
fig, ax = plt.subplots(2, 1)
ax[0].plot(seattle_weather["MONTH"], seattle_weather["MLY-PRCP-NORMAL"], color='b')
ax[0].plot(seattle_weather["MONTH"], seattle_weather["MLY-PRCP-25PCTL"], linestyle='--', color='b')
ax[0].plot(seattle_weather["MONTH"], seattle_weather["MLY-PRCP-75PCTL"], linestyle='--', color='b')
ax[1].plot(austin_weather["MONTH"], austin_weather["MLY-PRCP-NORMAL"], color='r')
ax[1].plot(austin_weather["MONTH"], austin_weather["MLY-PRCP-25PCTL"], linestyle='--', color='r')
ax[1].plot(austin_weather["MONTH"], austin_weather["MLY-PRCP-75PCTL"], linestyle='--', color='r')
ax[0].set_ylabel("Precipitation (inches)")
ax[1].set_ylabel("Precipitation (inches)")
ax[1].set_xlabel("Time (months)")
plt.show()
climate_change.index
DatetimeIndex(['1958-03-06', '1958-04-06', '1958-05-06', ...],
dtype='datetime64[ns]', name='date', length=706, freq=None)
climate_change['relative_temp']
climate_change['co2']
fig, ax = plt.subplots()
ax.plot(climate_change.index, climate_change['co2'])
ax.set_xlabel('Time')
ax.set_ylabel('CO2 (ppm)')
plt.show()
sixties = climate_change["1960-01-01":"1969-12-31"]
fig, ax = plt.subplots()
ax.plot(sixties.index, sixties['co2'])
ax.set_xlabel('Time')
ax.set_ylabel('CO2 (ppm)')
plt.show()
sixty_nine = climate_change["1969-01-01":"1969-12-31"]
fig, ax = plt.subplots()
ax.plot(sixty_nine.index, sixty_nine['co2'])
ax.set_xlabel('Time')
ax.set_ylabel('CO2 (ppm)')
plt.show()
import pandas as pd
climate_change = pd.read_csv('climate_change.csv',
parse_dates=["date"],
index_col="date")
fig, ax = plt.subplots()
ax.plot(climate_change.index, climate_change["co2"])
ax.plot(climate_change.index, climate_change["relative_temp"])
ax.set_xlabel('Time')
ax.set_ylabel('CO2 (ppm) / Relative temperature')
plt.show()
fig, ax = plt.subplots()
ax.plot(climate_change.index, climate_change["co2"])
ax.set_xlabel('Time')
ax.set_ylabel('CO2 (ppm)')
ax2 = ax.twinx()
ax2.plot(climate_change.index, climate_change["relative_temp"])
ax2.set_ylabel('Relative temperature (Celsius)')
plt.show()
fig, ax = plt.subplots()
ax.plot(climate_change.index, climate_change["co2"], color='blue')
ax.set_xlabel('Time')
ax.set_ylabel('CO2 (ppm)', color='blue')
ax2 = ax.twinx()
ax2.plot(climate_change.index, climate_change["relative_temp"], color='red')
ax2.set_ylabel('Relative temperature (Celsius)', color='red')
plt.show()
fig, ax = plt.subplots()
ax.plot(climate_change.index, climate_change["co2"], color='blue')
ax.set_xlabel('Time')
ax.set_ylabel('CO2 (ppm)', color='blue')
ax.tick_params('y', colors='blue')
ax2 = ax.twinx()
ax2.plot(climate_change.index, climate_change["relative_temp"], color='red')
ax2.set_ylabel('Relative temperature (Celsius)', color='red')
ax2.tick_params('y', colors='red')
plt.show()
fig, ax = plt.subplots()
plot_timeseries(ax, climate_change.index, climate_change['co2'],
'blue', 'Time', 'CO2 (ppm)')
ax2 = ax.twinx()
plot_timeseries(ax2, climate_change.index, climate_change['relative_temp'],
'red', 'Time', 'Relative temperature (Celsius)')
ax2.annotate(">1 degree", xy=(pd.Timestamp("2015-10-06"), 1))
plt.show()
ax2.annotate(">1 degree",
xy=(pd.Timestamp('2015-10-06'), 1),
xytext=(pd.Timestamp('2008-10-06'), -0.2),
arrowprops={"arrowstyle":"->", "color":"gray"})
fig, ax = plt.subplots()
plot_timeseries(ax, climate_change.index, climate_change['co2'],
'blue', 'Time', 'CO2 (ppm)')
ax2 = ax.twinx()
plot_timeseries(ax2, climate_change.index,
climate_change['relative_temp'],
'red', 'Time', 'Relative temperature (Celsius)')
plt.show()
fig, ax = plt.subplots()
ax.bar(medals.index, medals["Gold"])
ax.bar(medals.index, medals["Silver"], bottom=medals["Gold"])
ax.bar(medals.index, medals["Bronze"], bottom=medals["Gold"] + medals["Silver"])
ax.set_xticklabels(medals.index, rotation=90)
ax.set_ylabel("Number of medals")
plt.show()
fig, ax = plt.subplots()
ax.bar(medals.index, medals["Gold"], label="Gold")
ax.bar(medals.index, medals["Silver"], bottom=medals["Gold"], label="Silver")
ax.bar(medals.index, medals["Bronze"], bottom=medals["Gold"] + medals["Silver"], label="Bronze")
ax.set_xticklabels(medals.index, rotation=90)
ax.set_ylabel("Number of medals")
ax.legend()
plt.show()
fig, ax = plt.subplots()
ax.hist(mens_rowing["Height"])
ax.hist(mens_gymnastics["Height"])
ax.set_xlabel("Height (cm)")
ax.set_ylabel("# of observations")
plt.show()
fig, ax = plt.subplots()
ax.scatter(climate_change["co2"], climate_change["relative_temp"])
ax.set_xlabel("CO2 (ppm)")
ax.set_ylabel("Relative temperature (Celsius)")
plt.show()
eighties = climate_change["1980-01-01":"1989-12-31"]
nineties = climate_change["1990-01-01":"1999-12-31"]
fig, ax = plt.subplots()
ax.scatter(eighties["co2"], eighties["relative_temp"], color="red", label="eighties")
ax.scatter(nineties["co2"], nineties["relative_temp"], color="blue", label="nineties")
ax.legend()
ax.set_xlabel("CO2 (ppm)")
ax.set_ylabel("Relative temperature (Celsius)")
plt.show()
fig, ax = plt.subplots()
ax.bar("Rowing", mens_rowing["Height"].mean(), yerr=mens_rowing["Height"].std())
ax.bar("Gymnastics", mens_gymnastics["Height"].mean(), yerr=mens_gymnastics["Height"].std())
ax.set_ylabel("Height (cm)")
plt.show()
fig, ax = plt.subplots()
ax.errorbar(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL"],
yerr=seattle_weather["MLY-TAVG-STDDEV"])
ax.errorbar(austin_weather["MONTH"], austin_weather["MLY-TAVG-NORMAL"],
yerr=austin_weather["MLY-TAVG-STDDEV"])
ax.set_ylabel("Temperature (Fahrenheit)")
plt.show()
fig, ax = plt.subplots()
ax.boxplot([mens_rowing["Height"], mens_gymnastics["Height"]])
ax.set_xticklabels(["Rowing", "Gymnastics"])
ax.set_ylabel("Height (cm)")
plt.show()
plt.style.use()
plt.style.use("ggplot")
fig, ax = plt.subplots()
ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL"])
ax.plot(austin_weather["MONTH"], austin_weather["MLY-TAVG-NORMAL"])
ax.set_xlabel("Time (months)")
ax.set_ylabel("Average temperature (Fahrenheit degrees)")
plt.show()
plt.style.use("default")
plt.style.use("bmh")
plt.style.use("seaborn-colorblind")
fig, ax = plt.subplots()
ax.bar(medals.index, medals["Gold"])
ax.set_xticklabels(medals.index, rotation=90)
ax.set_ylabel("Number of medals")
plt.show()
fig.savefig("gold_medals.png")
ls
fig.savefig("gold_medals.jpg")
fig.savefig("gold_medals.jpg", quality=50)
fig.savefig("gold_medals.svg")
fig.savefig("gold_medals.png", dpi=300)
fig.set_size_inches([5, 3])
fig.set_size_inches([3, 5])
sports = summer_2016_medals["Sport"].unique()
print(sports)
fig, ax = plt.subplots()
for sport in sports:
sport_df = summer_2016_medals[summer_2016_medals["Sport"] == sport]
ax.bar(sport, sport_df["Height"].mean(), yerr=sport_df["Height"].std())
ax.set_ylabel("Height (cm)")
ax.set_xticklabels(sports, rotation=90)
plt.show()
sport
sport_d_f
bar
unique
unique_sports = df['Sport'].unique()
sport
sport_df sport
bar
sport
sport_df['Height'].mean()
sport_df['Height'].std()