Skip to content

Commit

Permalink
Merge pull request #691 from nikk-nikaznan/673-axis-labels
Browse files Browse the repository at this point in the history
Issue 673: Add title and just year as x-axis label
  • Loading branch information
vahtras authored Dec 3, 2024
2 parents a0a796f + 2c5f20e commit 18b7d60
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions episodes/14-looping-data-sets.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,21 @@ for filename in glob.glob('data/gapminder_gdp*.csv'):
# convenient abstractions for working with filesystem paths and could solve this as well:
# from pathlib import Path
# region = Path(filename).stem.split('_')[-1]
region = filename.split('_')[-1][:-4]
region = filename.split('_')[-1][:-4]
# extract the years from the columns of the dataframe
headings = dataframe.columns[1:]
years = headings.str.split('_').str.get(1)
# pandas raises errors when it encounters non-numeric columns in a dataframe computation
# but we can tell pandas to ignore them with the `numeric_only` parameter
dataframe.mean(numeric_only=True).plot(ax=ax, label=region)
# NOTE: another way of doing this selects just the columns with gdp in their name using the filter method
# dataframe.filter(like="gdp").mean().plot(ax=ax, label=region)

# set the title and labels
ax.set_title('GDP Per Capita for Regions Over Time')
ax.set_xticks(range(len(years)))
ax.set_xticklabels(years)
ax.set_xlabel('Year')
plt.tight_layout()
plt.legend()
plt.show()
```
Expand Down

0 comments on commit 18b7d60

Please sign in to comment.