Note
Go to the end to download the full example code.
Frequency Plots¶
Frequency plots/charts refer to stacked horizontal bar charts which show frequency distribution of data across labelled and segmented subgroups, with segment widths representing the proportion or frequency of the subgroup. These can be generated using the fig_frequency_chart() function, which returns a Plotly Go Figure object.
The plot below was generated using a synthetic dataset of patient treatment complications for Dengue, consisting of ten patients and five complications. Click the image to view the full interactive and fully annotated Plotly Go figure.
The synthetic dataset is given below as a table (which can easily be converted to a CSV).
Subgroup Label / Outcome Variable |
Description / Short Label |
Frequency |
|---|---|---|
Dengue Haemorrhagic Fever |
DHF |
0.4 |
Dengue Shock Syndrome |
DSS |
0.2 |
Thrombocytopenia |
Low Platelets |
0.6 |
Hepatomegaly |
Hepatomegaly |
0.3 |
Plasma Leakage |
Plasma Leakage |
0.1 |
The fig_frequency_chart() function expects a dataframe with the following columns (in no particular order):
"label"- the subgroup label / outcome variable column of the table"short_label"- the description column of the table"proportion"- the frequency column of the table
The data source can be in any appropriate form, such as, typically, a CSV. Here are the Python steps you need to generate the plot using the fig_frequency_chart() function:
import io, pandas as pd
from isaricanalytics.visualisation import fig_frequency_chart
# Load the CSV data from a string buffer
data = pd.read_csv(io.StringIO(
"""label,short_label,proportion\n
Dengue Haemorrhagic Fever,DHF,0.4\n
Dengue Shock Syndrome,DSS,0.2\n
Thrombocytopenia,Low Platelets,0.6\n
Hepatomegaly,Hepatomegaly,0.3\n
Plasma Leakage,Plasma Leakage,0.1\n
"""
), skipinitialspace=True)
# Create and display the figure
fig = fig_frequency_chart(
data,
title="Frequency Chart of Synthetic Dengue Patient Complications",
)
fig.update_layout(autosize=True)
fig
You should see the plot appearing as given above.
Note
Any dataframe or CSV column names, or dictionary field labels, in the example above that are not specific to the dataset must be as given, otherwise the function may throw an exception or return an incorrect figure.
The figure height and width parameters can be set using the height
and width parameters, but it may be more convenient to let Plotly handle
this using the figure layout autosize
parameter. Refer to the fig_frequency_chart()
function docstring for more information.
Total running time of the script: (0 minutes 9.757 seconds)