Module trase.tools.sei_pcs.plotting
Functions
def dumbbell_compare(df_a, df_b, value, group_by, comparator=<function Compare.absolute_error>, labels=('Base', 'Comparison'), colors=('#4e6356', '#ff6a5f'), title_text='', xaxis_title='', yaxis_title='', legend_title='', label_width=15, shorten_end=False, max_rows=15, other_label='OTHER', include_other=True, row_height_px=40) ‑> plotly.graph_objs.Figure-
Compare two dataframes and create a "dumbbell"-style plot.
Here is an ASCII-art of what that looks like:
BUNGE │ *─────* CARGILL │ *───* ADM │ *─* OTHER │ * └──────────────── 0 10 20 30Args
df_a- base DataFrame.
df_b- comparison DataFrame.
value- the name of the column containing numerical data that will be compared.
Any values duplicated over
group_bywill be summed. group_by- a list of categorical columns to group by.
comparator:optional- a function which takes two series and returns their
comparison. You can also pass in the string name of a method on
Compare. SeeComparefor some examples. Defaults toCompare.absolute_error. labels:optional- name of the two series that will appear in the legend. Default: ("Base", "Comparison").
title_text:optional- title for the chart. Default ""
xaxis_title:optional- title for the X-axis. Default ""
yaxis_title:optional- title for the Y-axis. Default ""
legend_title:optional- title for the legend. Default ""
label_width:optional- the maximum length of a label before it is shortened with "…". Default: 15
shorten_end:optional- if
Truelabels will be shortened from the end ("LONG LA…"); ifFalsethey will be shortened from the start (e.g. "…G LABEL"). Default: False max_rows:optional- how many rows to show, or
Noneif all rows should be shown. Default: 15 other_label:optional- the name of the "other" category; either a string or a
tuple of strings of the same length as
group_by. Default: "OTHER" include_other:optional- whether to include the "OTHER". Default: True
Returns: the plotly Figure
def histogram_compare(df_a, df_b, value, group_by, comparator=<function Compare.absolute_error>, title_text='', scale_by='none', bins=15, significant_digits=2, decimal_points=0, posinf_label='only in b', neginf_label='only in a')def render_label(value: Union[Tuple, str], shorten_end=False, label_width=15) ‑> str-
Plotly cannot render pd.MultiIndex, so we convert the tuples to strings. This function works both on MultiIndex (tuples) or Index (strings).
def sankey(df, value_column, categorical_columns, title_text='', shorten_end=False, label_width=15) ‑> plotly.graph_objs.Figure-
Args
df- the dataframe to be rendered. You do not need to consolidate the dataframe, this will be done for you in the function.
value_column- the column which will be used for the widths of the flows
categorical_columns- the columns which will be used for the columns of the Sankey.
title_text:optional- title for the chart. Default ""
label_width:optional- the maximum length of a label before it is shortened with "…". Default: 15
shorten_end:optional- if
Truelabels will be shortened from the end ("LONG LA…"); ifFalsethey will be shortened from the start (e.g. "…G LABEL"). Default: False
Returns: the plotly Figure
def shorten_label(label: str, shorten_end=False, label_width=15) ‑> str-
Trims labels that are too long and adds '…'