Note
Click here to download the full example code or to run this example in your browser via Binder
Example with the plotly graphing library¶
Sphinx-Gallery supports examples made with the
plotly library. Sphinx-Gallery is able to
capture the _repr_html_
of plotly figure objects (see Controlling what output is captured).
To display the figure, the last line in your code block should therefore be
the plotly figure object.
In order to use plotly, the conf.py
of the project should include the
following lines to select the appropriate plotly renderer:
import plotly.io as pio
pio.renderers.default = 'sphinx_gallery'
Optional: the sphinx_gallery
renderer of plotly will not generate png
thumbnails. For png thumbnails, you can use instead the sphinx_gallery_png
renderer, and add plotly.io._sg_scraper.plotly_sg_scraper
to the list of
Image scrapers. The scraper requires you to
install the orca package.
This tutorial gives a few examples of plotly figures, starting with its high-level API plotly express.
Out:
Figure({
'data': [{'alignmentgroup': 'True',
'hovertemplate': 'smoker=No<br>day=Sun<br>sex=%{x}<br>total_bill=%{y}<extra></extra>',
'legendgroup': 'No',
'marker': {'color': '#636efa'},
'name': 'No',
'offsetgroup': 'No',
'orientation': 'v',
'showlegend': True,
'textposition': 'auto',
'type': 'bar',
'x': array(['Female', 'Male', 'Male', 'Male', 'Female', 'Male', 'Male', 'Male',
'Male', 'Male', 'Male', 'Female', 'Male', 'Male', 'Female', 'Male',
'Female', 'Male', 'Female', 'Male', 'Male', 'Male', 'Male', 'Male',
'Male', 'Male', 'Male', 'Male', 'Male', 'Female', 'Female', 'Male',
'Male', 'Male', 'Male', 'Male', 'Female', 'Female', 'Male', 'Male',
'Male', 'Male', 'Male', 'Male', 'Female', 'Male', 'Female', 'Female',
'Male', 'Male', 'Male', 'Female', 'Male', 'Male', 'Male', 'Male',
'Male'], dtype=object),
'xaxis': 'x',
'y': array([16.99, 10.34, 21.01, 23.68, 24.59, 25.29, 8.77, 26.88, 15.04, 14.78,
10.27, 35.26, 15.42, 18.43, 14.83, 21.58, 10.33, 16.29, 16.97, 17.46,
13.94, 9.68, 30.4 , 18.29, 22.23, 32.4 , 28.55, 18.04, 12.54, 10.29,
34.81, 9.94, 25.56, 19.49, 38.07, 23.95, 25.71, 17.31, 29.93, 14.07,
13.13, 17.26, 24.55, 19.77, 29.85, 48.17, 25. , 13.39, 16.49, 21.5 ,
12.66, 16.21, 13.81, 24.52, 20.76, 31.71, 20.69]),
'yaxis': 'y'},
{'alignmentgroup': 'True',
'hovertemplate': 'smoker=No<br>day=Sat<br>sex=%{x}<br>total_bill=%{y}<extra></extra>',
'legendgroup': 'No',
'marker': {'color': '#636efa'},
'name': 'No',
'offsetgroup': 'No',
'orientation': 'v',
'showlegend': False,
'textposition': 'auto',
'type': 'bar',
'x': array(['Male', 'Male', 'Female', 'Female', 'Male', 'Male', 'Male', 'Male',
'Male', 'Male', 'Female', 'Male', 'Male', 'Female', 'Female', 'Male',
'Male', 'Male', 'Female', 'Male', 'Male', 'Male', 'Female', 'Male',
'Male', 'Male', 'Female', 'Male', 'Male', 'Female', 'Female', 'Male',
'Female', 'Male', 'Male', 'Female', 'Male', 'Male', 'Male', 'Male',
'Male', 'Male', 'Female', 'Male', 'Male'], dtype=object),
'xaxis': 'x2',
'y': array([20.65, 17.92, 20.29, 15.77, 39.42, 19.82, 17.81, 13.37, 12.69, 21.7 ,
19.65, 9.55, 18.35, 15.06, 20.69, 17.78, 24.06, 16.31, 16.93, 18.69,
31.27, 16.04, 26.41, 48.27, 17.59, 20.08, 16.45, 20.23, 12.02, 17.07,
14.73, 10.51, 20.92, 18.24, 14. , 7.25, 48.33, 20.45, 13.28, 11.61,
10.77, 10.07, 35.83, 29.03, 17.82]),
'yaxis': 'y2'},
{'alignmentgroup': 'True',
'hovertemplate': 'smoker=No<br>day=Thur<br>sex=%{x}<br>total_bill=%{y}<extra></extra>',
'legendgroup': 'No',
'marker': {'color': '#636efa'},
'name': 'No',
'offsetgroup': 'No',
'orientation': 'v',
'showlegend': False,
'textposition': 'auto',
'type': 'bar',
'x': array(['Male', 'Male', 'Male', 'Male', 'Female', 'Male', 'Female', 'Male',
'Male', 'Male', 'Male', 'Female', 'Female', 'Female', 'Male', 'Female',
'Male', 'Male', 'Female', 'Female', 'Male', 'Female', 'Female', 'Male',
'Male', 'Female', 'Female', 'Female', 'Female', 'Female', 'Female',
'Female', 'Female', 'Female', 'Male', 'Male', 'Female', 'Female',
'Female', 'Female', 'Female', 'Male', 'Male', 'Male', 'Female'],
dtype=object),
'xaxis': 'x3',
'y': array([27.2 , 22.76, 17.29, 16.66, 10.07, 15.98, 34.83, 13.03, 18.28, 24.71,
21.16, 10.65, 12.43, 24.08, 11.69, 13.42, 14.26, 15.95, 12.48, 29.8 ,
8.52, 14.52, 11.38, 22.82, 19.08, 20.27, 11.17, 12.26, 18.26, 8.51,
10.33, 14.15, 13.16, 17.47, 34.3 , 41.19, 27.05, 16.43, 8.35, 18.64,
11.87, 9.78, 7.51, 7.56, 18.78]),
'yaxis': 'y3'},
{'alignmentgroup': 'True',
'hovertemplate': 'smoker=No<br>day=Fri<br>sex=%{x}<br>total_bill=%{y}<extra></extra>',
'legendgroup': 'No',
'marker': {'color': '#636efa'},
'name': 'No',
'offsetgroup': 'No',
'orientation': 'v',
'showlegend': False,
'textposition': 'auto',
'type': 'bar',
'x': array(['Male', 'Female', 'Male', 'Female'], dtype=object),
'xaxis': 'x4',
'y': array([22.49, 22.75, 12.46, 15.98]),
'yaxis': 'y4'},
{'alignmentgroup': 'True',
'hovertemplate': 'smoker=Yes<br>day=Sun<br>sex=%{x}<br>total_bill=%{y}<extra></extra>',
'legendgroup': 'Yes',
'marker': {'color': '#EF553B'},
'name': 'Yes',
'offsetgroup': 'Yes',
'orientation': 'v',
'showlegend': True,
'textposition': 'auto',
'type': 'bar',
'x': array(['Female', 'Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Female',
'Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Female', 'Male',
'Female', 'Male', 'Male'], dtype=object),
'xaxis': 'x',
'y': array([17.51, 7.25, 31.85, 16.82, 32.9 , 17.89, 14.48, 9.6 , 34.63, 34.65,
23.33, 45.35, 23.17, 40.55, 20.9 , 30.46, 18.15, 23.1 , 15.69]),
'yaxis': 'y'},
{'alignmentgroup': 'True',
'hovertemplate': 'smoker=Yes<br>day=Sat<br>sex=%{x}<br>total_bill=%{y}<extra></extra>',
'legendgroup': 'Yes',
'marker': {'color': '#EF553B'},
'name': 'Yes',
'offsetgroup': 'Yes',
'orientation': 'v',
'showlegend': False,
'textposition': 'auto',
'type': 'bar',
'x': array(['Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Female', 'Male',
'Female', 'Female', 'Male', 'Female', 'Female', 'Male', 'Male', 'Male',
'Female', 'Female', 'Female', 'Male', 'Male', 'Male', 'Male', 'Male',
'Female', 'Male', 'Male', 'Female', 'Female', 'Female', 'Male', 'Male',
'Male', 'Female', 'Female', 'Male', 'Male', 'Male', 'Male', 'Male',
'Female', 'Male'], dtype=object),
'xaxis': 'x2',
'y': array([38.01, 11.24, 20.29, 13.81, 11.02, 18.29, 3.07, 15.01, 26.86, 25.28,
17.92, 44.3 , 22.42, 15.36, 20.49, 25.21, 14.31, 10.59, 10.63, 50.81,
15.81, 26.59, 38.73, 24.27, 12.76, 30.06, 25.89, 13.27, 28.17, 12.9 ,
28.15, 11.59, 7.74, 30.14, 22.12, 24.01, 15.69, 15.53, 12.6 , 32.83,
27.18, 22.67]),
'yaxis': 'y2'},
{'alignmentgroup': 'True',
'hovertemplate': 'smoker=Yes<br>day=Thur<br>sex=%{x}<br>total_bill=%{y}<extra></extra>',
'legendgroup': 'Yes',
'marker': {'color': '#EF553B'},
'name': 'Yes',
'offsetgroup': 'Yes',
'orientation': 'v',
'showlegend': False,
'textposition': 'auto',
'type': 'bar',
'x': array(['Male', 'Male', 'Male', 'Female', 'Male', 'Male', 'Male', 'Male',
'Female', 'Female', 'Male', 'Male', 'Female', 'Female', 'Female',
'Male', 'Female'], dtype=object),
'xaxis': 'x3',
'y': array([19.44, 32.68, 16. , 19.81, 28.44, 15.48, 16.58, 10.34, 43.11, 13. ,
13.51, 18.71, 12.74, 13. , 16.4 , 20.53, 16.47]),
'yaxis': 'y3'},
{'alignmentgroup': 'True',
'hovertemplate': 'smoker=Yes<br>day=Fri<br>sex=%{x}<br>total_bill=%{y}<extra></extra>',
'legendgroup': 'Yes',
'marker': {'color': '#EF553B'},
'name': 'Yes',
'offsetgroup': 'Yes',
'orientation': 'v',
'showlegend': False,
'textposition': 'auto',
'type': 'bar',
'x': array(['Male', 'Female', 'Female', 'Male', 'Male', 'Male', 'Male', 'Female',
'Female', 'Male', 'Female', 'Male', 'Male', 'Female', 'Female'],
dtype=object),
'xaxis': 'x4',
'y': array([28.97, 5.75, 16.32, 40.17, 27.28, 12.03, 21.01, 11.35, 15.38, 12.16,
13.42, 8.58, 13.42, 16.27, 10.09]),
'yaxis': 'y4'}],
'layout': {'annotations': [{'font': {},
'showarrow': False,
'text': 'day=Sun',
'x': 0.1175,
'xanchor': 'center',
'xref': 'paper',
'y': 1.0,
'yanchor': 'bottom',
'yref': 'paper'},
{'font': {},
'showarrow': False,
'text': 'day=Sat',
'x': 0.3725,
'xanchor': 'center',
'xref': 'paper',
'y': 1.0,
'yanchor': 'bottom',
'yref': 'paper'},
{'font': {},
'showarrow': False,
'text': 'day=Thur',
'x': 0.6275,
'xanchor': 'center',
'xref': 'paper',
'y': 1.0,
'yanchor': 'bottom',
'yref': 'paper'},
{'font': {},
'showarrow': False,
'text': 'day=Fri',
'x': 0.8824999999999998,
'xanchor': 'center',
'xref': 'paper',
'y': 1.0,
'yanchor': 'bottom',
'yref': 'paper'}],
'barmode': 'group',
'height': 400,
'legend': {'title': {'text': 'smoker'}, 'tracegroupgap': 0},
'margin': {'t': 60},
'template': '...',
'xaxis': {'anchor': 'y', 'domain': [0.0, 0.235], 'title': {'text': 'sex'}},
'xaxis2': {'anchor': 'y2', 'domain': [0.255, 0.49], 'matches': 'x', 'title': {'text': 'sex'}},
'xaxis3': {'anchor': 'y3', 'domain': [0.51, 0.745], 'matches': 'x', 'title': {'text': 'sex'}},
'xaxis4': {'anchor': 'y4',
'domain': [0.7649999999999999, 0.9999999999999999],
'matches': 'x',
'title': {'text': 'sex'}},
'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'total_bill'}},
'yaxis2': {'anchor': 'x2', 'domain': [0.0, 1.0], 'matches': 'y', 'showticklabels': False},
'yaxis3': {'anchor': 'x3', 'domain': [0.0, 1.0], 'matches': 'y', 'showticklabels': False},
'yaxis4': {'anchor': 'x4', 'domain': [0.0, 1.0], 'matches': 'y', 'showticklabels': False}}
})
In addition to the classical scatter or bar charts, plotly provides a large variety of traces, such as the sunburst hierarchical trace of the following example. plotly is an interactive library: click on one of the continents for a more detailed view of the drill-down.
df = px.data.gapminder().query("year == 2007")
fig = px.sunburst(df, path=['continent', 'country'], values='pop',
color='lifeExp', hover_data=['iso_alpha'],
color_continuous_scale='RdBu',
color_continuous_midpoint=np.average(df['lifeExp'], weights=df['pop']))
fig.update_layout(title_text='Life expectancy of countries and continents')
fig
Out:
Figure({
'data': [{'branchvalues': 'total',
'customdata': array([['AFG', 43.828],
['ALB', 76.423],
['DZA', 72.301],
...,
['(?)', 69.44386304205017],
['(?)', 77.89057081069897],
['(?)', 81.06215400970112]], dtype=object),
'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': ('labels=%{label}<br>pop=%{value' ... 'ifeExp=%{color}<extra></extra>'),
'ids': array(['Asia/Afghanistan', 'Europe/Albania', 'Africa/Algeria', 'Africa/Angola',
'Americas/Argentina', 'Oceania/Australia', 'Europe/Austria',
'Asia/Bahrain', 'Asia/Bangladesh', 'Europe/Belgium', 'Africa/Benin',
'Americas/Bolivia', 'Europe/Bosnia and Herzegovina', 'Africa/Botswana',
'Americas/Brazil', 'Europe/Bulgaria', 'Africa/Burkina Faso',
'Africa/Burundi', 'Asia/Cambodia', 'Africa/Cameroon', 'Americas/Canada',
'Africa/Central African Republic', 'Africa/Chad', 'Americas/Chile',
'Asia/China', 'Americas/Colombia', 'Africa/Comoros',
'Africa/Congo, Dem. Rep.', 'Africa/Congo, Rep.', 'Americas/Costa Rica',
"Africa/Cote d'Ivoire", 'Europe/Croatia', 'Americas/Cuba',
'Europe/Czech Republic', 'Europe/Denmark', 'Africa/Djibouti',
'Americas/Dominican Republic', 'Americas/Ecuador', 'Africa/Egypt',
'Americas/El Salvador', 'Africa/Equatorial Guinea', 'Africa/Eritrea',
'Africa/Ethiopia', 'Europe/Finland', 'Europe/France', 'Africa/Gabon',
'Africa/Gambia', 'Europe/Germany', 'Africa/Ghana', 'Europe/Greece',
'Americas/Guatemala', 'Africa/Guinea', 'Africa/Guinea-Bissau',
'Americas/Haiti', 'Americas/Honduras', 'Asia/Hong Kong, China',
'Europe/Hungary', 'Europe/Iceland', 'Asia/India', 'Asia/Indonesia',
'Asia/Iran', 'Asia/Iraq', 'Europe/Ireland', 'Asia/Israel',
'Europe/Italy', 'Americas/Jamaica', 'Asia/Japan', 'Asia/Jordan',
'Africa/Kenya', 'Asia/Korea, Dem. Rep.', 'Asia/Korea, Rep.',
'Asia/Kuwait', 'Asia/Lebanon', 'Africa/Lesotho', 'Africa/Liberia',
'Africa/Libya', 'Africa/Madagascar', 'Africa/Malawi', 'Asia/Malaysia',
'Africa/Mali', 'Africa/Mauritania', 'Africa/Mauritius',
'Americas/Mexico', 'Asia/Mongolia', 'Europe/Montenegro',
'Africa/Morocco', 'Africa/Mozambique', 'Asia/Myanmar', 'Africa/Namibia',
'Asia/Nepal', 'Europe/Netherlands', 'Oceania/New Zealand',
'Americas/Nicaragua', 'Africa/Niger', 'Africa/Nigeria', 'Europe/Norway',
'Asia/Oman', 'Asia/Pakistan', 'Americas/Panama', 'Americas/Paraguay',
'Americas/Peru', 'Asia/Philippines', 'Europe/Poland', 'Europe/Portugal',
'Americas/Puerto Rico', 'Africa/Reunion', 'Europe/Romania',
'Africa/Rwanda', 'Africa/Sao Tome and Principe', 'Asia/Saudi Arabia',
'Africa/Senegal', 'Europe/Serbia', 'Africa/Sierra Leone',
'Asia/Singapore', 'Europe/Slovak Republic', 'Europe/Slovenia',
'Africa/Somalia', 'Africa/South Africa', 'Europe/Spain',
'Asia/Sri Lanka', 'Africa/Sudan', 'Africa/Swaziland', 'Europe/Sweden',
'Europe/Switzerland', 'Asia/Syria', 'Asia/Taiwan', 'Africa/Tanzania',
'Asia/Thailand', 'Africa/Togo', 'Americas/Trinidad and Tobago',
'Africa/Tunisia', 'Europe/Turkey', 'Africa/Uganda',
'Europe/United Kingdom', 'Americas/United States', 'Americas/Uruguay',
'Americas/Venezuela', 'Asia/Vietnam', 'Asia/West Bank and Gaza',
'Asia/Yemen, Rep.', 'Africa/Zambia', 'Africa/Zimbabwe', 'Africa',
'Americas', 'Asia', 'Europe', 'Oceania'], dtype=object),
'labels': array(['Afghanistan', 'Albania', 'Algeria', 'Angola', 'Argentina', 'Australia',
'Austria', 'Bahrain', 'Bangladesh', 'Belgium', 'Benin', 'Bolivia',
'Bosnia and Herzegovina', 'Botswana', 'Brazil', 'Bulgaria',
'Burkina Faso', 'Burundi', 'Cambodia', 'Cameroon', 'Canada',
'Central African Republic', 'Chad', 'Chile', 'China', 'Colombia',
'Comoros', 'Congo, Dem. Rep.', 'Congo, Rep.', 'Costa Rica',
"Cote d'Ivoire", 'Croatia', 'Cuba', 'Czech Republic', 'Denmark',
'Djibouti', 'Dominican Republic', 'Ecuador', 'Egypt', 'El Salvador',
'Equatorial Guinea', 'Eritrea', 'Ethiopia', 'Finland', 'France',
'Gabon', 'Gambia', 'Germany', 'Ghana', 'Greece', 'Guatemala', 'Guinea',
'Guinea-Bissau', 'Haiti', 'Honduras', 'Hong Kong, China', 'Hungary',
'Iceland', 'India', 'Indonesia', 'Iran', 'Iraq', 'Ireland', 'Israel',
'Italy', 'Jamaica', 'Japan', 'Jordan', 'Kenya', 'Korea, Dem. Rep.',
'Korea, Rep.', 'Kuwait', 'Lebanon', 'Lesotho', 'Liberia', 'Libya',
'Madagascar', 'Malawi', 'Malaysia', 'Mali', 'Mauritania', 'Mauritius',
'Mexico', 'Mongolia', 'Montenegro', 'Morocco', 'Mozambique', 'Myanmar',
'Namibia', 'Nepal', 'Netherlands', 'New Zealand', 'Nicaragua', 'Niger',
'Nigeria', 'Norway', 'Oman', 'Pakistan', 'Panama', 'Paraguay', 'Peru',
'Philippines', 'Poland', 'Portugal', 'Puerto Rico', 'Reunion',
'Romania', 'Rwanda', 'Sao Tome and Principe', 'Saudi Arabia', 'Senegal',
'Serbia', 'Sierra Leone', 'Singapore', 'Slovak Republic', 'Slovenia',
'Somalia', 'South Africa', 'Spain', 'Sri Lanka', 'Sudan', 'Swaziland',
'Sweden', 'Switzerland', 'Syria', 'Taiwan', 'Tanzania', 'Thailand',
'Togo', 'Trinidad and Tobago', 'Tunisia', 'Turkey', 'Uganda',
'United Kingdom', 'United States', 'Uruguay', 'Venezuela', 'Vietnam',
'West Bank and Gaza', 'Yemen, Rep.', 'Zambia', 'Zimbabwe', 'Africa',
'Americas', 'Asia', 'Europe', 'Oceania'], dtype=object),
'marker': {'coloraxis': 'coloraxis',
'colors': array([43.828 , 76.423 , 72.301 , 42.731 , 75.32 ,
81.235 , 79.829 , 75.635 , 64.062 , 79.441 ,
56.728 , 65.554 , 74.852 , 50.728 , 72.39 ,
73.005 , 52.295 , 49.58 , 59.723 , 50.43 ,
80.653 , 44.741 , 50.651 , 78.553 , 72.961 ,
72.889 , 65.152 , 46.462 , 55.322 , 78.782 ,
48.328 , 75.748 , 78.273 , 76.486 , 78.332 ,
54.791 , 72.235 , 74.994 , 71.338 , 71.878 ,
51.579 , 58.04 , 52.947 , 79.313 , 80.657 ,
56.735 , 59.448 , 79.406 , 60.022 , 79.483 ,
70.259 , 56.007 , 46.388 , 60.916 , 70.198 ,
82.208 , 73.338 , 81.757 , 64.698 , 70.65 ,
70.964 , 59.545 , 78.885 , 80.745 , 80.546 ,
72.567 , 82.603 , 72.535 , 54.11 , 67.297 ,
78.623 , 77.588 , 71.993 , 42.592 , 45.678 ,
73.952 , 59.443 , 48.303 , 74.241 , 54.467 ,
64.164 , 72.801 , 76.195 , 66.803 , 74.543 ,
71.164 , 42.082 , 62.069 , 52.906 , 63.785 ,
79.762 , 80.204 , 72.899 , 56.867 , 46.859 ,
80.196 , 75.64 , 65.483 , 75.537 , 71.752 ,
71.421 , 71.688 , 75.563 , 78.098 , 78.746 ,
76.442 , 72.476 , 46.242 , 65.528 , 72.777 ,
63.062 , 74.002 , 42.568 , 79.972 , 74.663 ,
77.926 , 48.159 , 49.339 , 80.941 , 72.396 ,
58.556 , 39.613 , 80.884 , 81.701 , 74.143 ,
78.4 , 52.517 , 70.616 , 58.42 , 69.819 ,
73.923 , 71.777 , 51.542 , 79.425 , 78.242 ,
76.384 , 73.747 , 74.249 , 73.422 , 62.698 ,
42.384 , 43.487 , 54.56441058, 75.35668223, 69.44386304,
77.89057081, 81.06215401])},
'name': '',
'parents': array(['Asia', 'Europe', 'Africa', 'Africa', 'Americas', 'Oceania', 'Europe',
'Asia', 'Asia', 'Europe', 'Africa', 'Americas', 'Europe', 'Africa',
'Americas', 'Europe', 'Africa', 'Africa', 'Asia', 'Africa', 'Americas',
'Africa', 'Africa', 'Americas', 'Asia', 'Americas', 'Africa', 'Africa',
'Africa', 'Americas', 'Africa', 'Europe', 'Americas', 'Europe',
'Europe', 'Africa', 'Americas', 'Americas', 'Africa', 'Americas',
'Africa', 'Africa', 'Africa', 'Europe', 'Europe', 'Africa', 'Africa',
'Europe', 'Africa', 'Europe', 'Americas', 'Africa', 'Africa',
'Americas', 'Americas', 'Asia', 'Europe', 'Europe', 'Asia', 'Asia',
'Asia', 'Asia', 'Europe', 'Asia', 'Europe', 'Americas', 'Asia', 'Asia',
'Africa', 'Asia', 'Asia', 'Asia', 'Asia', 'Africa', 'Africa', 'Africa',
'Africa', 'Africa', 'Asia', 'Africa', 'Africa', 'Africa', 'Americas',
'Asia', 'Europe', 'Africa', 'Africa', 'Asia', 'Africa', 'Asia',
'Europe', 'Oceania', 'Americas', 'Africa', 'Africa', 'Europe', 'Asia',
'Asia', 'Americas', 'Americas', 'Americas', 'Asia', 'Europe', 'Europe',
'Americas', 'Africa', 'Europe', 'Africa', 'Africa', 'Asia', 'Africa',
'Europe', 'Africa', 'Asia', 'Europe', 'Europe', 'Africa', 'Africa',
'Europe', 'Asia', 'Africa', 'Africa', 'Europe', 'Europe', 'Asia',
'Asia', 'Africa', 'Asia', 'Africa', 'Americas', 'Africa', 'Europe',
'Africa', 'Europe', 'Americas', 'Americas', 'Americas', 'Asia', 'Asia',
'Asia', 'Africa', 'Africa', '', '', '', '', ''], dtype=object),
'type': 'sunburst',
'values': array([ 31889923, 3600523, 33333216, 12420476, 40301927, 20434176,
8199783, 708573, 150448339, 10392226, 8078314, 9119152,
4552198, 1639131, 190010647, 7322858, 14326203, 8390505,
14131858, 17696293, 33390141, 4369038, 10238807, 16284741,
1318683096, 44227550, 710960, 64606759, 3800610, 4133884,
18013409, 4493312, 11416987, 10228744, 5468120, 496374,
9319622, 13755680, 80264543, 6939688, 551201, 4906585,
76511887, 5238460, 61083916, 1454867, 1688359, 82400996,
22873338, 10706290, 12572928, 9947814, 1472041, 8502814,
7483763, 6980412, 9956108, 301931, 1110396331, 223547000,
69453570, 27499638, 4109086, 6426679, 58147733, 2780132,
127467972, 6053193, 35610177, 23301725, 49044790, 2505559,
3921278, 2012649, 3193942, 6036914, 19167654, 13327079,
24821286, 12031795, 3270065, 1250882, 108700891, 2874127,
684736, 33757175, 19951656, 47761980, 2055080, 28901790,
16570613, 4115771, 5675356, 12894865, 135031164, 4627926,
3204897, 169270617, 3242173, 6667147, 28674757, 91077287,
38518241, 10642836, 3942491, 798094, 22276056, 8860588,
199579, 27601038, 12267493, 10150265, 6144562, 4553009,
5447502, 2009245, 9118773, 43997828, 40448191, 20378239,
42292929, 1133066, 9031088, 7554661, 19314747, 23174294,
38139640, 65068149, 5701579, 1056608, 10276158, 71158647,
29170398, 60776238, 301139947, 3447496, 26084662, 85262356,
4018332, 22211743, 11746035, 12311143, 929539692, 898871184,
3811953827, 586098529, 24549947])}],
'layout': {'coloraxis': {'cmid': 68.91909251904043,
'colorbar': {'title': {'text': 'lifeExp'}},
'colorscale': [[0.0, 'rgb(103,0,31)'], [0.1,
'rgb(178,24,43)'], [0.2,
'rgb(214,96,77)'], [0.3,
'rgb(244,165,130)'], [0.4,
'rgb(253,219,199)'], [0.5,
'rgb(247,247,247)'], [0.6,
'rgb(209,229,240)'], [0.7,
'rgb(146,197,222)'], [0.8,
'rgb(67,147,195)'], [0.9,
'rgb(33,102,172)'], [1.0,
'rgb(5,48,97)']]},
'legend': {'tracegroupgap': 0},
'margin': {'t': 60},
'template': '...',
'title': {'text': 'Life expectancy of countries and continents'}}
})
While plotly express is often the high-level entry point of the plotly
library, complex figures mixing different types of traces can be made
with the low-level graph_objects
imperative API.
from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(rows=1, cols=2, specs=[[{}, {'type':'domain'}]])
fig.add_trace(go.Bar(x=[2018, 2019, 2020], y=[3, 2, 5], showlegend=False), 1, 1)
fig.add_trace(go.Pie(labels=['A', 'B', 'C'], values=[1, 3, 6]), 1, 2)
fig.update_layout(height=400, template='presentation', yaxis_title_text='revenue')
fig
# sphinx_gallery_thumbnail_path = '_static/plotly_logo.png'
Out:
Figure({
'data': [{'showlegend': False, 'type': 'bar', 'x': [2018, 2019, 2020], 'xaxis': 'x', 'y': [3, 2, 5], 'yaxis': 'y'},
{'domain': {'x': [0.55, 1.0], 'y': [0.0, 1.0]},
'labels': [A, B, C],
'type': 'pie',
'values': [1, 3, 6]}],
'layout': {'height': 400,
'template': '...',
'xaxis': {'anchor': 'y', 'domain': [0.0, 0.45]},
'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'revenue'}}}
})
Estimated memory usage: 18 MB