import datetime
import numpy as np
import pandas as pd
import pandas_datareader as pdr
Figures interactives¶
Dès lors qu'on visualise une figure sur un ordinateur, on se dit qu'il serait agréable de pouvoir interagir avec la figure en pouvant zoomer, avoir des informations complémentaire en passant la souris sur une donnée, en déselectionnant un courbe pour mieux voir les autres, etc.
Dans ce cours on va regarder comment faire cela avec Plotly Express Les personnes curieuses ou qui n'apprécient pas Plotly pourront regarder d'autres bibliothèques, le meilleur concurrent de Plotly étant Bokeh à mon avis.
Les données¶
On va utiliser la base de données de la banque fédérale de St Louis sur le chômage aux États-Unis, cf https://fred.stlouisfed.org/.
On construit 3 tableaux : unrate
, workers
et unemployed
.
# we download the unemployment rate since 1950
start = datetime.datetime(1950, 1, 1)
tmp = pdr.get_data_fred('UNRATE', start = start)
unrate = pd.DataFrame(index = tmp.index)
unrate['average'] = tmp
unrate['men'] = pdr.get_data_fred('LNS14000001', start = start)
unrate['women'] = pdr.get_data_fred('LNS14000002', start = start)
unrate['white'] = pdr.get_data_fred('LNS14000003', start = start)
unrate['latino'] = pdr.get_data_fred('LNS14000009', start = start)
unrate['black'] = pdr.get_data_fred('LNS14000006', start = start)
unrate.to_pickle('data/unrate.pkl') # we will use these data in another notebook
unrate.tail(2)
average | men | women | white | latino | black | |
---|---|---|---|---|---|---|
DATE | ||||||
2023-12-01 | 3.7 | 3.9 | 3.6 | 3.5 | 5.0 | 5.2 |
2024-01-01 | 3.7 | 3.9 | 3.4 | 3.4 | 5.0 | 5.3 |
workers = pdr.get_data_fred('LFWA64TTUSM647S')
workers.rename(columns={'LFWA64TTUSM647S':'all'}, inplace=True)
workers['men'] = pdr.get_data_fred('LFWA64MAUSM647N')
workers['women'] = pdr.get_data_fred('LFWA64FEUSM647S')
workers['error'] = workers['all'] - workers.men - workers.women
workers.to_pickle('data/workers.pkl')
workers.tail(2)
all | men | women | error | |
---|---|---|---|---|
DATE | ||||
2023-11-01 | 2.092484e+08 | 104186000.0 | 1.046945e+08 | 367860.867425 |
2023-12-01 | 2.093341e+08 | 104189000.0 | 1.047843e+08 | 360772.152739 |
unemployed = pdr.get_data_fred('LNS13000001')
unemployed.rename(columns={'LNS13000001':'men'}, inplace=True)
unemployed['women'] = pdr.get_data_fred('LNS13000002')
unemployed['white'] = pdr.get_data_fred('LNS13000003')
unemployed['latino'] = pdr.get_data_fred('LNS13000009')
unemployed['black'] = pdr.get_data_fred('LNS13000006')
unemployed['all'] = unemployed.men + unemployed.women
unemployed *= 1000
unemployed.to_pickle('data/unemployed.pkl')
unemployed.tail(2)
men | women | white | latino | black | all | |
---|---|---|---|---|---|---|
DATE | ||||||
2023-12-01 | 3456000 | 2811000 | 4424000 | 1602000 | 1143000 | 6267000 |
2024-01-01 | 3473000 | 2650000 | 4299000 | 1618000 | 1164000 | 6123000 |
Plotly Express¶
Dans cette première partie on va regarder quelques graphiques simples à savoir:
- les courbes
- les barres et histogrammes
- les boîtes à moustaches (statistiques)
- les camemberts
Note : Les graphiques étant dynamiques, ils doivent être regénérés (avec Cell -> Run all
) lorsqu'on ouvre cette feuille et les suivantes de ce chapitre.
Plotly avec Pandas¶
La facon la plus simple est de demander à Pandas de demander à Pandas d'utiliser Plotly à la place de Matplotlib (par défaut) :
pd.options.plotting.backend = "plotly"
Courbes¶
unemployed.plot() # pandas use its plotting backend, now Plotly
Ce graphique est dynamique, vous avez des informations en survolant les courbes et vous pouvez agir dessus via les icones en haut à droite. Il y a aussi des actions cachées :
- on peut cliquer sur une ligne de la légende pour faire apparaître ou disparaître la ligne correspondante. Si on double clique, alors elle est la seule affichée.
- on peut faire glisser les axes en déplacant le curseur
<-->
ou redimensionner les axes en allant à leurs extrémités et en déplacant le curseur->|
.
Contrôle de l'affichage¶
Pour travailler sur l'affichage il est préférable d'utiliser Plotly directement. Une figure pour Plotly a trois parties :
- la composition que l'on définit avec le layout
- les données et le style sont définis pour chaque courbe
- la configuration de l'interaction dynamique et de la barre de menus( cf la liste des options de configuration dans le code source !)
import plotly.express as px # PlotlyExpress is the simplified version of Plotly
selection = ['all', 'men', 'women']
df = 100 * (unemployed[selection] / (unemployed[selection] + workers[selection]))
df = df.dropna()
fig = px.line(df)
fig.update_layout(
title = 'Unemployment in the United-States',
yaxis = dict( title = 'percentage'),
dragmode = 'pan' # select pan mode (by default the zoom mode is selected)
)
Une figure de Plotly est un dictionnaire, donc on peut changer ses valeurs.
print(fig)
Figure({ 'data': [{'hovertemplate': 'variable=all<br>DATE=%{x}<br>value=%{y}<extra></extra>', 'legendgroup': 'all', 'line': {'color': '#636efa', 'dash': 'solid'}, 'marker': {'symbol': 'circle'}, 'mode': 'lines', 'name': 'all', 'orientation': 'v', 'showlegend': True, 'type': 'scatter', 'x': array([datetime.datetime(2019, 3, 1, 0, 0), datetime.datetime(2019, 4, 1, 0, 0), datetime.datetime(2019, 5, 1, 0, 0), datetime.datetime(2019, 6, 1, 0, 0), datetime.datetime(2019, 7, 1, 0, 0), datetime.datetime(2019, 8, 1, 0, 0), datetime.datetime(2019, 9, 1, 0, 0), datetime.datetime(2019, 10, 1, 0, 0), datetime.datetime(2019, 11, 1, 0, 0), datetime.datetime(2019, 12, 1, 0, 0), datetime.datetime(2020, 1, 1, 0, 0), datetime.datetime(2020, 2, 1, 0, 0), datetime.datetime(2020, 3, 1, 0, 0), datetime.datetime(2020, 4, 1, 0, 0), datetime.datetime(2020, 5, 1, 0, 0), datetime.datetime(2020, 6, 1, 0, 0), datetime.datetime(2020, 7, 1, 0, 0), datetime.datetime(2020, 8, 1, 0, 0), datetime.datetime(2020, 9, 1, 0, 0), datetime.datetime(2020, 10, 1, 0, 0), datetime.datetime(2020, 11, 1, 0, 0), datetime.datetime(2020, 12, 1, 0, 0), datetime.datetime(2021, 1, 1, 0, 0), datetime.datetime(2021, 2, 1, 0, 0), datetime.datetime(2021, 3, 1, 0, 0), datetime.datetime(2021, 4, 1, 0, 0), datetime.datetime(2021, 5, 1, 0, 0), datetime.datetime(2021, 6, 1, 0, 0), datetime.datetime(2021, 7, 1, 0, 0), datetime.datetime(2021, 8, 1, 0, 0), datetime.datetime(2021, 9, 1, 0, 0), datetime.datetime(2021, 10, 1, 0, 0), datetime.datetime(2021, 11, 1, 0, 0), datetime.datetime(2021, 12, 1, 0, 0), datetime.datetime(2022, 1, 1, 0, 0), datetime.datetime(2022, 2, 1, 0, 0), datetime.datetime(2022, 3, 1, 0, 0), datetime.datetime(2022, 4, 1, 0, 0), datetime.datetime(2022, 5, 1, 0, 0), datetime.datetime(2022, 6, 1, 0, 0), datetime.datetime(2022, 7, 1, 0, 0), datetime.datetime(2022, 8, 1, 0, 0), datetime.datetime(2022, 9, 1, 0, 0), datetime.datetime(2022, 10, 1, 0, 0), datetime.datetime(2022, 11, 1, 0, 0), datetime.datetime(2022, 12, 1, 0, 0), datetime.datetime(2023, 1, 1, 0, 0), datetime.datetime(2023, 2, 1, 0, 0), datetime.datetime(2023, 3, 1, 0, 0), datetime.datetime(2023, 4, 1, 0, 0), datetime.datetime(2023, 5, 1, 0, 0), datetime.datetime(2023, 6, 1, 0, 0), datetime.datetime(2023, 7, 1, 0, 0), datetime.datetime(2023, 8, 1, 0, 0), datetime.datetime(2023, 9, 1, 0, 0), datetime.datetime(2023, 10, 1, 0, 0), datetime.datetime(2023, 11, 1, 0, 0), datetime.datetime(2023, 12, 1, 0, 0)], dtype=object), 'xaxis': 'x', 'y': array([ 2.92125488, 2.81022307, 2.7921167 , 2.79325793, 2.8529355 , 2.79974574, 2.71278437, 2.76838209, 2.76672548, 2.75892966, 2.76130872, 2.71060065, 3.39253455, 10.04149809, 9.19007278, 7.91010527, 7.3907031 , 6.15735649, 5.74953157, 5.07438333, 4.93881304, 4.96546426, 4.7363772 , 4.64500099, 4.5312796 , 4.55310639, 4.30420078, 4.44087672, 4.09634567, 3.88000941, 3.59338049, 3.40782109, 3.17376534, 2.97884293, 3.06584749, 2.94373178, 2.81629364, 2.83357108, 2.79968341, 2.80475958, 2.71200828, 2.80327092, 2.69884744, 2.78696115, 2.78829657, 2.67103274, 2.67341217, 2.78288349, 2.74148999, 2.67095667, 2.85007962, 2.79409905, 2.75026789, 2.94521789, 2.94582133, 2.98858679, 2.90566024, 2.90675722]), 'yaxis': 'y'}, {'hovertemplate': 'variable=men<br>DATE=%{x}<br>value=%{y}<extra></extra>', 'legendgroup': 'men', 'line': {'color': '#EF553B', 'dash': 'solid'}, 'marker': {'symbol': 'circle'}, 'mode': 'lines', 'name': 'men', 'orientation': 'v', 'showlegend': True, 'type': 'scatter', 'x': array([datetime.datetime(2019, 3, 1, 0, 0), datetime.datetime(2019, 4, 1, 0, 0), datetime.datetime(2019, 5, 1, 0, 0), datetime.datetime(2019, 6, 1, 0, 0), datetime.datetime(2019, 7, 1, 0, 0), datetime.datetime(2019, 8, 1, 0, 0), datetime.datetime(2019, 9, 1, 0, 0), datetime.datetime(2019, 10, 1, 0, 0), datetime.datetime(2019, 11, 1, 0, 0), datetime.datetime(2019, 12, 1, 0, 0), datetime.datetime(2020, 1, 1, 0, 0), datetime.datetime(2020, 2, 1, 0, 0), datetime.datetime(2020, 3, 1, 0, 0), datetime.datetime(2020, 4, 1, 0, 0), datetime.datetime(2020, 5, 1, 0, 0), datetime.datetime(2020, 6, 1, 0, 0), datetime.datetime(2020, 7, 1, 0, 0), datetime.datetime(2020, 8, 1, 0, 0), datetime.datetime(2020, 9, 1, 0, 0), datetime.datetime(2020, 10, 1, 0, 0), datetime.datetime(2020, 11, 1, 0, 0), datetime.datetime(2020, 12, 1, 0, 0), datetime.datetime(2021, 1, 1, 0, 0), datetime.datetime(2021, 2, 1, 0, 0), datetime.datetime(2021, 3, 1, 0, 0), datetime.datetime(2021, 4, 1, 0, 0), datetime.datetime(2021, 5, 1, 0, 0), datetime.datetime(2021, 6, 1, 0, 0), datetime.datetime(2021, 7, 1, 0, 0), datetime.datetime(2021, 8, 1, 0, 0), datetime.datetime(2021, 9, 1, 0, 0), datetime.datetime(2021, 10, 1, 0, 0), datetime.datetime(2021, 11, 1, 0, 0), datetime.datetime(2021, 12, 1, 0, 0), datetime.datetime(2022, 1, 1, 0, 0), datetime.datetime(2022, 2, 1, 0, 0), datetime.datetime(2022, 3, 1, 0, 0), datetime.datetime(2022, 4, 1, 0, 0), datetime.datetime(2022, 5, 1, 0, 0), datetime.datetime(2022, 6, 1, 0, 0), datetime.datetime(2022, 7, 1, 0, 0), datetime.datetime(2022, 8, 1, 0, 0), datetime.datetime(2022, 9, 1, 0, 0), datetime.datetime(2022, 10, 1, 0, 0), datetime.datetime(2022, 11, 1, 0, 0), datetime.datetime(2022, 12, 1, 0, 0), datetime.datetime(2023, 1, 1, 0, 0), datetime.datetime(2023, 2, 1, 0, 0), datetime.datetime(2023, 3, 1, 0, 0), datetime.datetime(2023, 4, 1, 0, 0), datetime.datetime(2023, 5, 1, 0, 0), datetime.datetime(2023, 6, 1, 0, 0), datetime.datetime(2023, 7, 1, 0, 0), datetime.datetime(2023, 8, 1, 0, 0), datetime.datetime(2023, 9, 1, 0, 0), datetime.datetime(2023, 10, 1, 0, 0), datetime.datetime(2023, 11, 1, 0, 0), datetime.datetime(2023, 12, 1, 0, 0)], dtype=object), 'xaxis': 'x', 'y': array([3.19797922, 3.12282409, 3.05259743, 3.04267763, 3.10974214, 3.07282822, 2.99585791, 3.06155518, 2.97604853, 2.92615887, 2.92021154, 2.90943939, 3.61426302, 9.97166686, 9.10777837, 8.09808721, 7.58613145, 6.48700017, 6.04757226, 5.58126973, 5.48694672, 5.35230985, 5.08304589, 5.05055242, 4.90705342, 5.02214964, 4.81941309, 4.90660353, 4.57545924, 4.29480934, 4.03069743, 3.67528041, 3.38379256, 3.20317734, 3.31782071, 3.10789035, 3.00405699, 3.13056757, 2.98586389, 3.03267628, 2.96752995, 3.09137377, 2.93222997, 3.0023596 , 3.03201826, 2.81548992, 2.9324993 , 2.98700935, 3.00097888, 2.89297846, 3.07877184, 3.10182943, 3.04295164, 3.33119736, 3.41196879, 3.37836584, 3.31662955, 3.21055321]), 'yaxis': 'y'}, {'hovertemplate': 'variable=women<br>DATE=%{x}<br>value=%{y}<extra></extra>', 'legendgroup': 'women', 'line': {'color': '#00cc96', 'dash': 'solid'}, 'marker': {'symbol': 'circle'}, 'mode': 'lines', 'name': 'women', 'orientation': 'v', 'showlegend': True, 'type': 'scatter', 'x': array([datetime.datetime(2019, 3, 1, 0, 0), datetime.datetime(2019, 4, 1, 0, 0), datetime.datetime(2019, 5, 1, 0, 0), datetime.datetime(2019, 6, 1, 0, 0), datetime.datetime(2019, 7, 1, 0, 0), datetime.datetime(2019, 8, 1, 0, 0), datetime.datetime(2019, 9, 1, 0, 0), datetime.datetime(2019, 10, 1, 0, 0), datetime.datetime(2019, 11, 1, 0, 0), datetime.datetime(2019, 12, 1, 0, 0), datetime.datetime(2020, 1, 1, 0, 0), datetime.datetime(2020, 2, 1, 0, 0), datetime.datetime(2020, 3, 1, 0, 0), datetime.datetime(2020, 4, 1, 0, 0), datetime.datetime(2020, 5, 1, 0, 0), datetime.datetime(2020, 6, 1, 0, 0), datetime.datetime(2020, 7, 1, 0, 0), datetime.datetime(2020, 8, 1, 0, 0), datetime.datetime(2020, 9, 1, 0, 0), datetime.datetime(2020, 10, 1, 0, 0), datetime.datetime(2020, 11, 1, 0, 0), datetime.datetime(2020, 12, 1, 0, 0), datetime.datetime(2021, 1, 1, 0, 0), datetime.datetime(2021, 2, 1, 0, 0), datetime.datetime(2021, 3, 1, 0, 0), datetime.datetime(2021, 4, 1, 0, 0), datetime.datetime(2021, 5, 1, 0, 0), datetime.datetime(2021, 6, 1, 0, 0), datetime.datetime(2021, 7, 1, 0, 0), datetime.datetime(2021, 8, 1, 0, 0), datetime.datetime(2021, 9, 1, 0, 0), datetime.datetime(2021, 10, 1, 0, 0), datetime.datetime(2021, 11, 1, 0, 0), datetime.datetime(2021, 12, 1, 0, 0), datetime.datetime(2022, 1, 1, 0, 0), datetime.datetime(2022, 2, 1, 0, 0), datetime.datetime(2022, 3, 1, 0, 0), datetime.datetime(2022, 4, 1, 0, 0), datetime.datetime(2022, 5, 1, 0, 0), datetime.datetime(2022, 6, 1, 0, 0), datetime.datetime(2022, 7, 1, 0, 0), datetime.datetime(2022, 8, 1, 0, 0), datetime.datetime(2022, 9, 1, 0, 0), datetime.datetime(2022, 10, 1, 0, 0), datetime.datetime(2022, 11, 1, 0, 0), datetime.datetime(2022, 12, 1, 0, 0), datetime.datetime(2023, 1, 1, 0, 0), datetime.datetime(2023, 2, 1, 0, 0), datetime.datetime(2023, 3, 1, 0, 0), datetime.datetime(2023, 4, 1, 0, 0), datetime.datetime(2023, 5, 1, 0, 0), datetime.datetime(2023, 6, 1, 0, 0), datetime.datetime(2023, 7, 1, 0, 0), datetime.datetime(2023, 8, 1, 0, 0), datetime.datetime(2023, 9, 1, 0, 0), datetime.datetime(2023, 10, 1, 0, 0), datetime.datetime(2023, 11, 1, 0, 0), datetime.datetime(2023, 12, 1, 0, 0)], dtype=object), 'xaxis': 'x', 'y': array([ 2.65169541, 2.50523883, 2.54065452, 2.55344775, 2.60217881, 2.53492114, 2.43701951, 2.48054806, 2.55975443, 2.594753 , 2.60659879, 2.51609122, 3.16813844, 10.12207372, 9.30758233, 7.7418639 , 7.21248799, 5.849431 , 5.4773059 , 4.58964892, 4.4161926 , 4.59884617, 4.38320659, 4.23849584, 4.15565946, 4.09635419, 3.80683931, 3.9906786 , 3.62819929, 3.48044802, 3.17378477, 3.15257535, 2.97580795, 2.7666157 , 2.80578033, 2.7719611 , 2.62155597, 2.53311372, 2.61328655, 2.58189344, 2.46178535, 2.52478617, 2.47546643, 2.58226286, 2.5570816 , 2.53800774, 2.40803602, 2.57258321, 2.4743432 , 2.44226678, 2.61843441, 2.48616158, 2.45744127, 2.56300939, 2.48577749, 2.60669205, 2.50320026, 2.61256743]), 'yaxis': 'y'}], 'layout': {'dragmode': 'pan', 'legend': {'title': {'text': 'variable'}, 'tracegroupgap': 0}, 'margin': {'t': 60}, 'template': '...', 'title': {'text': 'Unemployment in the United-States'}, 'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title': {'text': 'DATE'}}, 'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'percentage'}}} })
Voici comment définir la couleur et le style des lignes (on aurait pu définir les couleurs dans la commande line
avec l'argument color_discrete_sequence=['black','dodgerblue','#ffaa11']
).
fig['data'][0]['line']={'color':'black', 'dash':'dash'} # dash can be dot, dash, dashdot, solid
fig['data'][1]['line']['color'] = 'dodgerblue' # see https://matplotlib.org/stable/gallery/color/named_colors.html
fig['data'][2]['line']['color'] = '#ffaa11' # RGB
fig
Barres et histogrammes¶
Les graphiques avec des barres sont
$\;$ | PlotlyExpress | Pandas |
---|---|---|
Barres | px.bar |
kind = 'bar' |
Barres horizontales | px.bar + orientation = 'h' |
kind = 'barh' |
Histogrammes | px.histogram |
kind = 'histogram' |
Les options de ce type de graphique sont :
barmode = (overlay|group|stack)
pour les histogrammes :
bins = xx
avec xx le nombre de barre désiréeshistnorm ('' | 'percent' | 'probability' | 'density' | 'probability density')
histfunc ('count' | 'sum' | 'avg' | 'min' | 'max')
# both following lines give the same result
#unemployed[['men','women']].plot(kind='bar', barmode='group')
px.bar(unemployed, y=['men','women'], barmode='group')
layout_histo = dict(
title = 'How many months at that rates of unemployement since 1975 ?',
xaxis = dict(title = 'Percentage of unemployeed'),
yaxis = dict(title = 'How many months since 1975'),
)
fig = unrate[unrate.index > '1975'].plot(kind='histogram', bins=40, barmode='group')
fig.update_layout(layout_histo)
Rappelez vous que vous pouvez cliquer ou double cliquer sur les lignes de la légende pour changer ce qui est affiché.
Boîtes à moustaches¶
px.box(unrate[unrate.index > '1975'], title="Unemployment rate") # title can be defined as an argument (not only in layout)
Plotly¶
La bibliothèque sur laquelle s'appuie Plotly Express est, sans surprise, Plotly. Elle est plus difficile à manipuler et plus verbeuse mais on peut la préférer lorsqu'on veut faire des figures compliquées.
On définit chaque jeu de données et sa facon de le représenter, ainsi que la mise en page puis on affiche le graphique interactif avec iplot
:
import plotly
import plotly.offline as py
import plotly.graph_objects as go
py.init_notebook_mode(connected=True)
print("Plotly v.%s" % plotly.__version__)
Plotly v.5.19.0
trace1 = go.Scatter(
x = unrate.index,
y = unrate.men,
name = 'men',
mode='lines',
line = {'dash':'dashdot', 'color':'dodgerblue'}
)
trace2 = go.Scatter(
x = unrate.index,
y = unrate.women,
name = 'women',
mode='lines+markers',
marker = {'symbol':'triangle-down-open', 'color':'red'}
)
layout = go.Layout(
title = 'Unemployment in the United-States',
yaxis = dict(
title = 'percentage',
range = (0,15),
),
dragmode = 'pan',
)
config = {
'scrollZoom': True,
'modeBarButtonsToRemove': ['sendDataToCloud',],
'showLink':False,
}
fig = go.Figure(data = [trace2, trace1], layout=layout)
py.iplot(fig, config=config)
On retrouve la même structure en dictionnaire pour la figure :
print(fig)
Figure({ 'data': [{'marker': {'color': 'red', 'symbol': 'triangle-down-open'}, 'mode': 'lines+markers', 'name': 'women', 'type': 'scatter', 'x': array([datetime.datetime(1950, 1, 1, 0, 0), datetime.datetime(1950, 2, 1, 0, 0), datetime.datetime(1950, 3, 1, 0, 0), ..., datetime.datetime(2023, 11, 1, 0, 0), datetime.datetime(2023, 12, 1, 0, 0), datetime.datetime(2024, 1, 1, 0, 0)], dtype=object), 'y': array([6.9, 6.8, 6.7, ..., 3.4, 3.6, 3.4])}, {'line': {'color': 'dodgerblue', 'dash': 'dashdot'}, 'mode': 'lines', 'name': 'men', 'type': 'scatter', 'x': array([datetime.datetime(1950, 1, 1, 0, 0), datetime.datetime(1950, 2, 1, 0, 0), datetime.datetime(1950, 3, 1, 0, 0), ..., datetime.datetime(2023, 11, 1, 0, 0), datetime.datetime(2023, 12, 1, 0, 0), datetime.datetime(2024, 1, 1, 0, 0)], dtype=object), 'y': array([6.4, 6.2, 6.1, ..., 4. , 3.9, 3.9])}], 'layout': {'dragmode': 'pan', 'template': '...', 'title': {'text': 'Unemployment in the United-States'}, 'yaxis': {'range': [0, 15], 'title': {'text': 'percentage'}}} })
Camember¶
Je trouve difficile de faire un camember avec Plotly Express qui veut tout faire à ma place et qui à la fin n'arrive pas à faire le trop simple. Ainsi il veut une colonne qui lui permet de regrouper des données. Je n'ai pas ce problème avec Plotly qui est plus primitif.
pie_data = unemployed[['white','black', 'latino']].sum()
pie_data
white 323595000 black 92201000 latino 107494000 dtype: int64
trace = go.Pie(labels=pie_data.index, values=pie_data.values,
hoverinfo='label+percent',
textinfo='value', textposition='outside', textfont={'size':15},
hole=0.2, pull=0.1
)
fw=go.FigureWidget(data=[trace])
fw
FigureWidget({ 'data': [{'hole': 0.2, 'hoverinfo': 'label+percent', 'labels': array(['white', 'black', 'latino'], dtype=object), 'pull': 0.1, 'textfont': {'size': 15}, 'textinfo': 'value', 'textposition': 'outside', 'type': 'pie', 'uid': 'c8cf0347-982f-4285-af05-be584aab7196', 'values': array([323595000, 92201000, 107494000])}], 'layout': {'template': '...'} })
avec, comme indiqué dans la référence :
textposition
qui peut être "inside", "outside", "auto" ou "none"textinfo
qui peut être "label", "text", "value", "percent" (éventuellement liés par "+") ou "none"
{{ PreviousNext("11 -- Static graphics with Seaborn.ipynb", "21 -- Dynamic graphics with Plotly -- Scatter and bubbles.ipynb")}}