Skip to content

Commit

Permalink
add: hypergrowth graph story page
Browse files Browse the repository at this point in the history
  • Loading branch information
gmguarino committed Apr 24, 2024
1 parent b20f754 commit 596488c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pinkbombs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .graphs.viz import make_area_chart, make_area_single_chart
from .graphs.viz import make_area_order_chart, make_bar_chart
from .graphs.viz import make_area_order_chart, make_bar_chart, make_area_order_chart_grouped
from .graphs.viz import make_color_bar_chart, make_color_bar_chart2
from .graphs.viz import make_animated_bubble_map, make_treemap_chart
from .graphs.viz import make_simple_bar_chart, make_simple_pie_chart
Expand Down
11 changes: 11 additions & 0 deletions pinkbombs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@
"Hyper-growth in salmon farming",
True,
],
},
"hyper-growth-grouped": {
"filename": "hyper_growth_salmon_farming_1.2.csv",
"function": pb.make_area_order_chart_grouped,
"parser": pd.read_csv,
"arguments": [
"Year",
"Tonnes - live weight",
"Hyper-growth in salmon farming",
"#fd442f",
],
},
"top-10": {
"filename": "top_10_countries_producing_1.3.csv",
Expand Down
14 changes: 14 additions & 0 deletions pinkbombs/graphs/test_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,17 @@
)

g5_1.write_html("pinkbombs/graphs/test_html/" + data5_1_name + ".html")


# Graph xx - Croissance 1ere page
datax_x_name = "hyper_growth_salmon_farming_1.2"
datax_x_file = "data/" + datax_x_name + ".csv"
df_datax_x = pd.read_csv(datax_x_file)

gx_x = pb.make_area_order_chart_grouped(
df_datax_x,
"Year",
"Tonnes - live weight",
title="Hyper-growth in salmon farming",
)
gx_x.write_html("pinkbombs/graphs/test_html/hyper-croissance-story.html")
14 changes: 14 additions & 0 deletions pinkbombs/graphs/test_html/hyper-croissance-story.html

Large diffs are not rendered by default.

46 changes: 45 additions & 1 deletion pinkbombs/graphs/viz.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pandas as pd
import plotly.express as px
from plotly.graph_objects import Figure
from plotly.graph_objects import Figure, Scatter


def make_area_chart(input_df: pd.DataFrame, input_x: str, input_y: str) -> Figure:
Expand Down Expand Up @@ -41,6 +41,50 @@ def make_area_single_chart(
return area


def make_area_order_chart_grouped(
input_df,
input_x,
input_y,
title,
color="#fd442f",
theme="simple_white",
) -> Figure:
"""Returns plotly express object as area chart with multiple lines
Parameters:
input_df (pd.DataFrame): dataframe with data to be visualised
input_x (str): name of the field for the x axis
input_y (str): name of the field for the y axis
input_col (str): name of the field for the colors
title (str): chart title
reorder (boolean): if true, dataframe is reodered by the input_col field, defauls is False
palette (px.object): plotly discrete palette, default is Dark24
theme (str): plotly chart theme, default is 'simple_white'
Returns:
area (plotly object): output chart object
"""
# Data cleaning - TO REMOVE
input_df[input_y] = input_df[input_y].str.replace(",", ".")
input_df[input_y] = input_df[input_y].astype(float)

input_df = input_df.groupby(input_x)[input_y].sum().reset_index()
input_df["color"] = color

area = Figure()
area.add_trace(
Scatter(
x=input_df[input_x],
y=input_df[input_y],
fill="tozeroy",
line=dict(color=color),
mode="lines",
)
)
area.update_layout(template=theme, title=title, xaxis_title=input_x, yaxis_title=input_y)
area.update_yaxes(exponentformat="none", showgrid=True)

return area


def make_area_order_chart(
input_df,
input_x,
Expand Down

0 comments on commit 596488c

Please sign in to comment.