Tree Plot of African Lnaguages
1. Python code
Show code
import pandas as pd
import numpy as np
import plotly.express as px
# Load data
edible_plants = pd.read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2026/2026-02-03/edible_plants.csv' )
df_clean = edible_plants[['cultivation' , 'sunlight' , 'water' ]].copy()
df_clean = df_clean[df_clean['cultivation' ] != "Miscellaneous" ]
df_clean['sunlight' ] = df_clean['sunlight' ].str .strip().str .title()
df_clean['water' ] = df_clean['water' ].str .title()
df_counts = df_clean.value_counts().reset_index(name= 'count' )
df_counts['cultivation_labeled' ] = '<b>Cultivation:</b> ' + df_counts['cultivation' ] + '<br>(n=' + df_counts['count' ].astype(str ) + ')'
df_counts['sunlight_labeled' ] = '<b>Sunlight:</b> ' + df_counts['sunlight' ] + '<br>(n=' + df_counts['count' ].astype(str ) + ')'
df_counts['water_labeled' ] = '<b>Water:</b> ' + df_counts['water' ] + '<br>(n=' + df_counts['count' ].astype(str ) + ')'
fig = px.sunburst(
df_counts,
path= ['cultivation' , 'sunlight' , 'water' ],
values= 'count' ,
title= "Edible Plants: Requirements by Level" ,
color= 'cultivation' ,
color_discrete_sequence= px.colors.qualitative.Safe
)
fig.update_layout(
annotations= [
dict (text= "<b>Level 1:</b> Cultivation" , x= 0.8 , y= 1.1 , showarrow= False , font_size= 14 ),
dict (text= "<b>Level 2:</b> Sunlight" , x= 0.8 , y= 1.05 , showarrow= False , font_size= 14 ),
dict (text= "<b>Level 3:</b> Water" , x= 0.8 , y= 1.0 , showarrow= False , font_size= 14 )
],
margin= dict (t= 100 )
)
fig.update_traces(
hovertemplate= '<b>% {label} </b><br>Count: % {value} <br>Parent: % {parent} '
)
fig.show()
Back to top