-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import pandas as pd | ||
import plotnine as gg | ||
|
||
# Read CSV data | ||
url = 'https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-01-29/clean_cheese.csv' | ||
cheese = pd.read_csv(url) | ||
|
||
# Select columns and compute differences | ||
plot_data = cheese[['Year', 'Cheddar']].copy() | ||
plot_data['diff'] = plot_data['Cheddar'].diff() | ||
plot_data.loc[plot_data.index[0], 'diff'] = plot_data['Cheddar'].iloc[0] | ||
plot_data['start'] = plot_data['Cheddar'] - plot_data['diff'] | ||
plot_data['fill'] = plot_data['diff'] > 0 | ||
|
||
# Define text for annotation | ||
annot_text = 'Data from the United States\nDepartment of Agriculture shows\nthat pounds of cheese consumed\nper person has been steadily rising\nsince the 1970s.\n\nGraphic: Nicola Rennie' | ||
|
||
# Create the plot | ||
p = ( | ||
gg.ggplot(plot_data) + | ||
gg.geom_rect(mapping=gg.aes(xmin='Year-0.45', xmax='Year+0.45', ymin='start', ymax='Cheddar', fill='fill')) + | ||
gg.annotate('text', x=2002, y = 4, label=annot_text, size=8) + | ||
gg.labs(title='Who ate all the cheese?', | ||
x='', | ||
y='Pounds of cheese per person') + | ||
gg.scale_fill_manual(values=['#CC3F0C', '#09814A']) + | ||
gg.theme_xkcd() + | ||
gg.theme(legend_position='none', | ||
plot_title=gg.element_text(face='bold')) | ||
) | ||
|
||
# Save the plot | ||
gg.ggsave(p, filename="2024/viz/05_diverging.png", height=4, width=6, dpi=300) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<h1 align="center"> Dairy Production and Consumption </h1> | ||
|
||
<p align="center"> | ||
<img src="/2019/2019-03-05/20190129.png" width="60%"> | ||
</p> |