Pie Charts#
We can create a pie chart using px.pie()
. An example using our global population sample data:
import plotly.express as px # import statement
df = px.data.gapminder().query("year == 2007").query("continent == 'Africa'") # subset data
df # inspect data
country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
---|---|---|---|---|---|---|---|---|
35 | Algeria | Africa | 2007 | 72.301 | 33333216 | 6223.367465 | DZA | 12 |
47 | Angola | Africa | 2007 | 42.731 | 12420476 | 4797.231267 | AGO | 24 |
131 | Benin | Africa | 2007 | 56.728 | 8078314 | 1441.284873 | BEN | 204 |
167 | Botswana | Africa | 2007 | 50.728 | 1639131 | 12569.851770 | BWA | 72 |
203 | Burkina Faso | Africa | 2007 | 52.295 | 14326203 | 1217.032994 | BFA | 854 |
215 | Burundi | Africa | 2007 | 49.580 | 8390505 | 430.070692 | BDI | 108 |
239 | Cameroon | Africa | 2007 | 50.430 | 17696293 | 2042.095240 | CMR | 120 |
263 | Central African Republic | Africa | 2007 | 44.741 | 4369038 | 706.016537 | CAF | 140 |
275 | Chad | Africa | 2007 | 50.651 | 10238807 | 1704.063724 | TCD | 148 |
323 | Comoros | Africa | 2007 | 65.152 | 710960 | 986.147879 | COM | 174 |
335 | Congo, Dem. Rep. | Africa | 2007 | 46.462 | 64606759 | 277.551859 | COD | 180 |
347 | Congo, Rep. | Africa | 2007 | 55.322 | 3800610 | 3632.557798 | COG | 178 |
371 | Cote d'Ivoire | Africa | 2007 | 48.328 | 18013409 | 1544.750112 | CIV | 384 |
431 | Djibouti | Africa | 2007 | 54.791 | 496374 | 2082.481567 | DJI | 262 |
467 | Egypt | Africa | 2007 | 71.338 | 80264543 | 5581.180998 | EGY | 818 |
491 | Equatorial Guinea | Africa | 2007 | 51.579 | 551201 | 12154.089750 | GNQ | 226 |
503 | Eritrea | Africa | 2007 | 58.040 | 4906585 | 641.369524 | ERI | 232 |
515 | Ethiopia | Africa | 2007 | 52.947 | 76511887 | 690.805576 | ETH | 231 |
551 | Gabon | Africa | 2007 | 56.735 | 1454867 | 13206.484520 | GAB | 266 |
563 | Gambia | Africa | 2007 | 59.448 | 1688359 | 752.749726 | GMB | 270 |
587 | Ghana | Africa | 2007 | 60.022 | 22873338 | 1327.608910 | GHA | 288 |
623 | Guinea | Africa | 2007 | 56.007 | 9947814 | 942.654211 | GIN | 324 |
635 | Guinea-Bissau | Africa | 2007 | 46.388 | 1472041 | 579.231743 | GNB | 624 |
827 | Kenya | Africa | 2007 | 54.110 | 35610177 | 1463.249282 | KEN | 404 |
887 | Lesotho | Africa | 2007 | 42.592 | 2012649 | 1569.331442 | LSO | 426 |
899 | Liberia | Africa | 2007 | 45.678 | 3193942 | 414.507341 | LBR | 430 |
911 | Libya | Africa | 2007 | 73.952 | 6036914 | 12057.499280 | LBY | 434 |
923 | Madagascar | Africa | 2007 | 59.443 | 19167654 | 1044.770126 | MDG | 450 |
935 | Malawi | Africa | 2007 | 48.303 | 13327079 | 759.349910 | MWI | 454 |
959 | Mali | Africa | 2007 | 54.467 | 12031795 | 1042.581557 | MLI | 466 |
971 | Mauritania | Africa | 2007 | 64.164 | 3270065 | 1803.151496 | MRT | 478 |
983 | Mauritius | Africa | 2007 | 72.801 | 1250882 | 10956.991120 | MUS | 480 |
1031 | Morocco | Africa | 2007 | 71.164 | 33757175 | 3820.175230 | MAR | 504 |
1043 | Mozambique | Africa | 2007 | 42.082 | 19951656 | 823.685621 | MOZ | 508 |
1067 | Namibia | Africa | 2007 | 52.906 | 2055080 | 4811.060429 | NAM | 516 |
1127 | Niger | Africa | 2007 | 56.867 | 12894865 | 619.676892 | NER | 562 |
1139 | Nigeria | Africa | 2007 | 46.859 | 135031164 | 2013.977305 | NGA | 566 |
1271 | Reunion | Africa | 2007 | 76.442 | 798094 | 7670.122558 | REU | 638 |
1295 | Rwanda | Africa | 2007 | 46.242 | 8860588 | 863.088464 | RWA | 646 |
1307 | Sao Tome and Principe | Africa | 2007 | 65.528 | 199579 | 1598.435089 | STP | 678 |
1331 | Senegal | Africa | 2007 | 63.062 | 12267493 | 1712.472136 | SEN | 686 |
1355 | Sierra Leone | Africa | 2007 | 42.568 | 6144562 | 862.540756 | SLE | 694 |
1403 | Somalia | Africa | 2007 | 48.159 | 9118773 | 926.141068 | SOM | 706 |
1415 | South Africa | Africa | 2007 | 49.339 | 43997828 | 9269.657808 | ZAF | 710 |
1451 | Sudan | Africa | 2007 | 58.556 | 42292929 | 2602.394995 | SDN | 736 |
1463 | Swaziland | Africa | 2007 | 39.613 | 1133066 | 4513.480643 | SWZ | 748 |
1523 | Tanzania | Africa | 2007 | 52.517 | 38139640 | 1107.482182 | TZA | 834 |
1547 | Togo | Africa | 2007 | 58.420 | 5701579 | 882.969944 | TGO | 768 |
1571 | Tunisia | Africa | 2007 | 73.923 | 10276158 | 7092.923025 | TUN | 788 |
1595 | Uganda | Africa | 2007 | 51.542 | 29170398 | 1056.380121 | UGA | 800 |
1691 | Zambia | Africa | 2007 | 42.384 | 11746035 | 1271.211593 | ZMB | 894 |
1703 | Zimbabwe | Africa | 2007 | 43.487 | 12311143 | 469.709298 | ZWE | 716 |
df.loc[df['pop'] < 2.e6, 'country'] = 'Other countries' # filter dataframe to classify countries below a population size threshold as "Other countries"
fig = px.pie(df, values='pop', names='country', title='Population of African continent') # crate figure
fig # show figure
Perhaps a useful example of the limited utility of pie charts. In this example, we pass the filtered dataframe to px.pie()
, and specify the pop
field as the slice value and country
as the slice name.
Another example using the restaurant bill and tip data.
df = px.data.tips() # load data
df # inspect data
fig = px.pie(df, values='tip', names='day') # create plot
fig # show output
In this example, we pass the entire data frame to px.pie()
and assign tip
as the slice value and day
as the slice name. Each day is a slice of the pie, and plotly.express
and the px.pie()
function have done the underlying calculations to show the aggregate tip data as a percent.