Note
Go to the end to download the full example code
Plotly - Treemap (sari)
Warning
Please be patient, it might take some time to load.
For more information, check https://plotly.com/python/treemaps/
Index(['specimen_code', 'microorganism_code', 'antimicrobial_code', 'highly resistant', 'intermediate', 'not done', 'resistant', 'sensitive', 'freq', 'sari', 'sari_medium', 'sari_soft', 'name', 'category', 'acronym_x', 'exists_in_registry_x', 'domain', 'phylum', 'class', 'order', 'family', 'genus',
'species', 'acronym_y', 'exists_in_registry_y', 'gram_stain', 'microorganism_name'],
dtype='object')
10 # Plotly
11 import plotly.express as px
12
13 # Import own libraries
14 from pyamr.core.sari import sari
15 from pyamr.datasets.load import load_data_nhs
16
17
18 # --------------------------------------------------------------------
19 # Main
20 # --------------------------------------------------------------------
21 # Load data
22 data, antimicrobials, microorganisms = load_data_nhs(nrows=1000)
23
24 # Create DataFrame
25 dataframe = data.groupby(['specimen_code',
26 'microorganism_code',
27 'antimicrobial_code',
28 'sensitivity']) \
29 .size().unstack().fillna(0)
30
31 # Compute frequency
32 dataframe['freq'] = dataframe.sum(axis=1)
33
34 # Compute sari
35 dataframe['sari'] = sari(dataframe, strategy='hard')
36 dataframe['sari_medium'] = sari(dataframe, strategy='medium')
37 dataframe['sari_soft'] = sari(dataframe, strategy='soft')
38
39 # Reset index
40 dataframe = dataframe.reset_index()
41
42 # --------------------------------------------
43 # Add info for popup
44 # --------------------------------------------
45 dataframe = dataframe.merge(antimicrobials,
46 how='left', left_on='antimicrobial_code',
47 right_on='antimicrobial_code')
48
49 # Add antimicrobials information
50 dataframe = dataframe.merge(microorganisms,
51 how='left', left_on='microorganism_code',
52 right_on='microorganism_code')
53
54 # Format dataframe
55 dataframe = dataframe.round(decimals=3)
56
57 # Show columns
58 print(dataframe.columns)
59
60 # -------------------------------------------
61 # Plot
62 # -------------------------------------------
63 # Show
64 fig = px.treemap(dataframe,
65 path=['specimen_code',
66 'microorganism_code',
67 'antimicrobial_code'],
68 #hover_name=,
69 hover_data=['microorganism_name',
70 'name',
71 'sari_medium',
72 'sari_soft'],
73 values='freq',
74 color='sari',
75 color_continuous_scale='Reds',
76 title='Treemap of <Microorganisms, Antimicrobials> pairs')
77
78 # Save as html (to be used anyware in docs)
79 fig.write_html("../../docs/source/_static/htmls/{0}.html" \
80 .format('plot_pyplot_treemap'))
81
82 # Show (script)
83 #fig.show()
84
85 # Show (sphinx)
86 fig
Total running time of the script: ( 0 minutes 1.925 seconds)