.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_examples/plotly/main06_treemap_v3.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr__examples_plotly_main06_treemap_v3.py: 06. Plot Treemap with NHS ------------------------------- This example displays a Treemap using a portion of the NHS dataset. This example needs ``pyAMR`` to load the corresponding data. .. warning:: It is not completed! .. warning:: It might take some time to load. .. note:: It uses ``plotly.express`` rather than ``go.Treemap``. .. note:: https://plotly.com/python/treemaps/ .. GENERATED FROM PYTHON SOURCE LINES 14-221 .. rst-class:: sphx-glr-script-out Out: .. code-block:: none '\n# Set custom data (not needed and inconsistent)\n#fig.data[0].customdata = dataframe.to_numpy()\n#fig.data[0].hovertemplate = htmp\n\n# Uncomment to check the customdata[i] information\n#fig.data[0].hovertemplate = # guide_template(dataframe.columns.tolist())\n\n# Update: I\n#fig.update_traces(hovertemplate=\'labels=%{label}\')\n#fig.update_traces(texttemplate=\'Freq=%{value:.2f}
\')\nfig.update_traces(hovertemplate=htmp_guide)\n#fig.update_traces(hovertemplate=htmp)\n\n# Update: II\n# But it seems to me you want something like\n#fig.data[0].hovertemplate = \'%{label}
%{value}\'\n#fig.data[0].hovertemplate = \'%{Freq}
%{Antimicrobial Name}\'\n\nfig.update_layout(\n margin={\n \'l\': 0,\n \'r\': 0,\n \'b\': 0,\n \'t\': 0,\n \'pad\': 4\n })\n# ----------------------------\n# Save\n# ----------------------------\n# Libraries\nimport time\nfrom pathlib import Path\n\n# Define pipeline path\npath = Path(\'./objects\') / \'plot_main08_treemap\'\nfilename = \'%s.html\' % time.strftime("%Y%m%d-%H%M%S")\n\n# Create folder (if it does not exist)\npath.mkdir(parents=True, exist_ok=True)\n\n# Save\nfig.write_html("%s/%s" % (path, filename))\n\n# Show\nshow(fig)\n' | .. code-block:: default :lineno-start: 14 # Plotly import numpy as np import pandas as pd import plotly.express as px from plotly.io import show # Import own libraries from pyamr.core.sari import sari from pyamr.datasets.load import load_data_nhs try: __file__ TERMINAL = True except: TERMINAL = False # Methods def build_hierarchical_dataframe(df, levels, value_column, color_columns=None): """ Build a hierarchy of levels for Sunburst or Treemap charts. Levels are given starting from the bottom to the top of the hierarchy, ie the last level corresponds to the root. """ df_all_trees = pd.DataFrame(columns=['id', 'parent', 'value', 'color']) for i, level in enumerate(levels): df_tree = pd.DataFrame(columns=['id', 'parent', 'value', 'color']) dfg = df.groupby(levels[i:]).sum() dfg = dfg.reset_index() df_tree['id'] = dfg[level].copy() if i < len(levels) - 1: df_tree['parent'] = dfg[levels[i+1]].copy() else: df_tree['parent'] = 'total' df_tree['value'] = dfg[value_column] df_tree['color'] = dfg[color_columns[0]] df_all_trees = df_all_trees.append(df_tree, ignore_index=True) total = pd.Series(dict(id='total', parent='', value=df[value_column].sum(), color=df[color_columns[0]].sum())) df_all_trees = df_all_trees.append(total, ignore_index=True) return df_all_trees TERMINAL = True """ # -------------------------------------------------------------------- # Main # -------------------------------------------------------------------- # Load data data, antimicrobials, microorganisms = load_data_nhs(nrows=10000) # Create DataFrame dataframe = data.groupby(['specimen_code', 'microorganism_code', 'antimicrobial_code', 'sensitivity']) \ .size().unstack().fillna(0) # Compute frequency dataframe['freq'] = dataframe.sum(axis=1) # Compute sari dataframe['sari'] = sari(dataframe, strategy='hard') dataframe['sari_medium'] = sari(dataframe, strategy='medium') dataframe['sari_soft'] = sari(dataframe, strategy='soft') # Reset index dataframe = dataframe.reset_index() # -------------------------------------------- # Add info for popup # -------------------------------------------- dataframe = dataframe.merge(antimicrobials, how='left', left_on='antimicrobial_code', right_on='antimicrobial_code') # Add antimicrobials information dataframe = dataframe.merge(microorganisms, how='left', left_on='microorganism_code', right_on='microorganism_code') # Format dataframe dataframe = dataframe.round(decimals=3) # Replace dataframe.microorganism_name = \ dataframe.microorganism_name.str.title() dataframe.columns = \ dataframe.columns.str.title().str.replace('_', ' ') # Show if TERMINAL: print("\nColumns:") print(dataframe.dtypes) print("\nDF:") print(dataframe) # ------------------------------------------- # Plot # ------------------------------------------- def guide_template(names): return '
'.join([ "%2d %-45s %%{customdata[%s]}" % (i, n, i ) for i, n in enumerate(names)]) # Guide template htmp_guide = guide_template(dataframe.columns.tolist()) # Define own template htmp = "Specimen: (%{customdata[0]})
" htmp+= "Microorganism: %{customdata[26]} (%{customdata[1]})
" htmp+= "Antimicrobial: %{customdata[12]} (%{customdata[2]})
" htmp+= "Freq: %{customdata[8]}
" htmp+= "SARI: %{customdata[9]}
" htmp = "(%{customdata[0]})
" htmp+= "%{customdata[26]} (%{customdata[1]})
" htmp+= "%{customdata[12]} (%{customdata[2]})
" htmp+= "Freq: %{customdata[8]}
" htmp+= "SARI: %{customdata[9]}
" # Display fig = px.treemap(dataframe, path=['Specimen Code', 'Microorganism Code', 'Antimicrobial Code'], #hover_name=, hover_data=dataframe.columns.tolist(), values='Freq', color='Sari', color_continuous_scale='Reds', title='Treemap of pairs') # Show current template print(fig.data[0].hovertemplate) """ """ The default hover template looks as follows: labels=%{label}
Freq=%{value}
parent=%{parent}
id=%{id}
Microorganism Name=%{customdata[0]}
Name=%{customdata[1]}
Sari Medium=%{customdata[2]}
Sari Soft=%{customdata[3]}
Sari=%{color} """ """ # Set custom data (not needed and inconsistent) #fig.data[0].customdata = dataframe.to_numpy() #fig.data[0].hovertemplate = htmp # Uncomment to check the customdata[i] information #fig.data[0].hovertemplate = \ # guide_template(dataframe.columns.tolist()) # Update: I #fig.update_traces(hovertemplate='labels=%{label}') #fig.update_traces(texttemplate='Freq=%{value:.2f}
') fig.update_traces(hovertemplate=htmp_guide) #fig.update_traces(hovertemplate=htmp) # Update: II # But it seems to me you want something like #fig.data[0].hovertemplate = '%{label}
%{value}' #fig.data[0].hovertemplate = '%{Freq}
%{Antimicrobial Name}' fig.update_layout( margin={ 'l': 0, 'r': 0, 'b': 0, 't': 0, 'pad': 4 }) # ---------------------------- # Save # ---------------------------- # Libraries import time from pathlib import Path # Define pipeline path path = Path('./objects') / 'plot_main08_treemap' filename = '%s.html' % time.strftime("%Y%m%d-%H%M%S") # Create folder (if it does not exist) path.mkdir(parents=True, exist_ok=True) # Save fig.write_html("%s/%s" % (path, filename)) # Show show(fig) """ .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.099 seconds) .. _sphx_glr_download__examples_plotly_main06_treemap_v3.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: main06_treemap_v3.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: main06_treemap_v3.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_