# Importation des librairies / fonctions
import plotly
import plotly.offline as py
import plotly.graph_objs as go
import pandas as pd
import numpy as np
from plotly.figure_factory import create_table
import plotly.express as px
gapminder = px.data.gapminder()
# gapninder est bel et bien une DataFrame pandas
type(gapminder)
pandas.core.frame.DataFrame
gapminder.describe()
year | lifeExp | pop | gdpPercap | iso_num | |
---|---|---|---|---|---|
count | 1704.00000 | 1704.000000 | 1.704000e+03 | 1704.000000 | 1704.000000 |
mean | 1979.50000 | 59.474439 | 2.960121e+07 | 7215.327081 | 425.880282 |
std | 17.26533 | 12.917107 | 1.061579e+08 | 9857.454543 | 248.305709 |
min | 1952.00000 | 23.599000 | 6.001100e+04 | 241.165876 | 4.000000 |
25% | 1965.75000 | 48.198000 | 2.793664e+06 | 1202.060309 | 208.000000 |
50% | 1979.50000 | 60.712500 | 7.023596e+06 | 3531.846988 | 410.000000 |
75% | 1993.25000 | 70.845500 | 1.958522e+07 | 9325.462346 | 638.000000 |
max | 2007.00000 | 82.603000 | 1.318683e+09 | 113523.132900 | 894.000000 |
gapminder.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 1704 entries, 0 to 1703 Data columns (total 8 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 country 1704 non-null object 1 continent 1704 non-null object 2 year 1704 non-null int64 3 lifeExp 1704 non-null float64 4 pop 1704 non-null int64 5 gdpPercap 1704 non-null float64 6 iso_alpha 1704 non-null object 7 iso_num 1704 non-null int64 dtypes: float64(2), int64(3), object(3) memory usage: 106.6+ KB
# Evolution de la population du Canada
gapminder['country'].unique()
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'], dtype=object)
data_canada = px.data.gapminder().query("country == 'Canada'")
data_canada
country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
---|---|---|---|---|---|---|---|---|
240 | Canada | Americas | 1952 | 68.750 | 14785584 | 11367.16112 | CAN | 124 |
241 | Canada | Americas | 1957 | 69.960 | 17010154 | 12489.95006 | CAN | 124 |
242 | Canada | Americas | 1962 | 71.300 | 18985849 | 13462.48555 | CAN | 124 |
243 | Canada | Americas | 1967 | 72.130 | 20819767 | 16076.58803 | CAN | 124 |
244 | Canada | Americas | 1972 | 72.880 | 22284500 | 18970.57086 | CAN | 124 |
245 | Canada | Americas | 1977 | 74.210 | 23796400 | 22090.88306 | CAN | 124 |
246 | Canada | Americas | 1982 | 75.760 | 25201900 | 22898.79214 | CAN | 124 |
247 | Canada | Americas | 1987 | 76.860 | 26549700 | 26626.51503 | CAN | 124 |
248 | Canada | Americas | 1992 | 77.950 | 28523502 | 26342.88426 | CAN | 124 |
249 | Canada | Americas | 1997 | 78.610 | 30305843 | 28954.92589 | CAN | 124 |
250 | Canada | Americas | 2002 | 79.770 | 31902268 | 33328.96507 | CAN | 124 |
251 | Canada | Americas | 2007 | 80.653 | 33390141 | 36319.23501 | CAN | 124 |
fig = px.bar(data_canada, x = 'year', y = 'pop', title= "Evolution de la population du canada")
fig.show()
# Autres arguments de px.bar()
fig = px.bar(data_canada, x = 'year', y = 'pop',
hover_data = ['lifeExp', 'gdpPercap'],
color = 'lifeExp',
title= "Evolution de la population du canada")
fig.show()
data_rdc = px.data.gapminder().query("country == 'Congo, Dem. Rep.'")
data_rdc
country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
---|---|---|---|---|---|---|---|---|
324 | Congo, Dem. Rep. | Africa | 1952 | 39.143 | 14100005 | 780.542326 | COD | 180 |
325 | Congo, Dem. Rep. | Africa | 1957 | 40.652 | 15577932 | 905.860230 | COD | 180 |
326 | Congo, Dem. Rep. | Africa | 1962 | 42.122 | 17486434 | 896.314634 | COD | 180 |
327 | Congo, Dem. Rep. | Africa | 1967 | 44.056 | 19941073 | 861.593242 | COD | 180 |
328 | Congo, Dem. Rep. | Africa | 1972 | 45.989 | 23007669 | 904.896068 | COD | 180 |
329 | Congo, Dem. Rep. | Africa | 1977 | 47.804 | 26480870 | 795.757282 | COD | 180 |
330 | Congo, Dem. Rep. | Africa | 1982 | 47.784 | 30646495 | 673.747818 | COD | 180 |
331 | Congo, Dem. Rep. | Africa | 1987 | 47.412 | 35481645 | 672.774812 | COD | 180 |
332 | Congo, Dem. Rep. | Africa | 1992 | 45.548 | 41672143 | 457.719181 | COD | 180 |
333 | Congo, Dem. Rep. | Africa | 1997 | 42.587 | 47798986 | 312.188423 | COD | 180 |
334 | Congo, Dem. Rep. | Africa | 2002 | 44.966 | 55379852 | 241.165876 | COD | 180 |
335 | Congo, Dem. Rep. | Africa | 2007 | 46.462 | 64606759 | 277.551859 | COD | 180 |
# Autres arguments de px.bar()
fig1 = px.bar(data_rdc, x = 'year', y = 'pop',
hover_data = ['lifeExp', 'gdpPercap'],
color = 'lifeExp',
title= "Evolution de la population de la RDC jusqu'à 2007")
fig1.show()
# DataFrame de l'année 2007
gapminder2007 = gapminder.query("year == 2007")
gapminder2007
country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
---|---|---|---|---|---|---|---|---|
11 | Afghanistan | Asia | 2007 | 43.828 | 31889923 | 974.580338 | AFG | 4 |
23 | Albania | Europe | 2007 | 76.423 | 3600523 | 5937.029526 | ALB | 8 |
35 | Algeria | Africa | 2007 | 72.301 | 33333216 | 6223.367465 | DZA | 12 |
47 | Angola | Africa | 2007 | 42.731 | 12420476 | 4797.231267 | AGO | 24 |
59 | Argentina | Americas | 2007 | 75.320 | 40301927 | 12779.379640 | ARG | 32 |
... | ... | ... | ... | ... | ... | ... | ... | ... |
1655 | Vietnam | Asia | 2007 | 74.249 | 85262356 | 2441.576404 | VNM | 704 |
1667 | West Bank and Gaza | Asia | 2007 | 73.422 | 4018332 | 3025.349798 | PSE | 275 |
1679 | Yemen, Rep. | Asia | 2007 | 62.698 | 22211743 | 2280.769906 | YEM | 887 |
1691 | Zambia | Africa | 2007 | 42.384 | 11746035 | 1271.211593 | ZMB | 894 |
1703 | Zimbabwe | Africa | 2007 | 43.487 | 12311143 | 469.709298 | ZWE | 716 |
142 rows × 8 columns
# Expérance de vie en fonction du produit intérieur brut
px.scatter(gapminder2007, x="gdpPercap", y="lifeExp",
title="Expérance de vie en fonction du produit intérieur brut pour 2007")
gapminder['continent'].unique()
array(['Asia', 'Europe', 'Africa', 'Americas', 'Oceania'], dtype=object)
# Colorier les points par une variable catégorielle
px.scatter(gapminder2007, x="gdpPercap", y="lifeExp",
color='continent',
title="Expérance de vie en fonction du produit intérieur brut pour 2007")
px.scatter(gapminder2007, x="gdpPercap", y="lifeExp",
color='continent',
size='pop',
title="Expérance de vie en fonction du produit intérieur brut pour 2007")
# Ajout de la variable 'country'
px.scatter(gapminder2007, x="gdpPercap", y="lifeExp",
color='continent',
size='pop',
hover_name='country',
title="Expérance de vie en fonction du produit intérieur brut pour 2007")
# Animation
px.scatter(gapminder, x="gdpPercap", y="lifeExp",
color='continent',
size='pop',
hover_name='country',
animation_frame="year",
animation_group="country",
title="Expérance de vie en fonction du produit intérieur brut")
# Mettre l'axe x en log
px.scatter(gapminder,
x="gdpPercap",
y="lifeExp",
log_x=True,
color='continent',
size='pop',
hover_name='country',
animation_frame="year",
animation_group="country",
title="Expérance de vie en fonction du produit intérieur brut")
#fig.show()
# facet_col
px.scatter(gapminder,
x="gdpPercap",
y="lifeExp",
log_x=True,
facet_col="continent",
color='continent',
size='pop',
hover_name='country',
animation_frame="year",
animation_group="country",
title="Expérance de vie en fonction du produit intérieur brut")
gapminder.head()
country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
---|---|---|---|---|---|---|---|---|
0 | Afghanistan | Asia | 1952 | 28.801 | 8425333 | 779.445314 | AFG | 4 |
1 | Afghanistan | Asia | 1957 | 30.332 | 9240934 | 820.853030 | AFG | 4 |
2 | Afghanistan | Asia | 1962 | 31.997 | 10267083 | 853.100710 | AFG | 4 |
3 | Afghanistan | Asia | 1967 | 34.020 | 11537966 | 836.197138 | AFG | 4 |
4 | Afghanistan | Asia | 1972 | 36.088 | 13079460 | 739.981106 | AFG | 4 |
fig2 = px.line_geo(gapminder2007, locations= 'iso_alpha', color='continent', projection='orthographic')
fig2.show()
Vous pouvez consulter la documentation de la fonction px.cloropleth()
px.choropleth(gapminder, locations="iso_alpha",
color="lifeExp",
hover_name="country",
animation_frame="year",
color_continuous_scale=px.colors.sequential.Plasma,
projection="natural earth")
# Projection en 2D
px.choropleth(gapminder, locations="iso_alpha",
color="lifeExp",
hover_name="country",
animation_frame="year")
px.line(data_canada, x = "year", y = "lifeExp", hover_name= "pop")
px.line(gapminder, x = "year", y = "lifeExp", hover_name= "country",
color = "continent", line_group = "country",
line_shape = "spline", render_mode = "svg")
px.area(gapminder, x="year", y="pop", color="continent", line_group="country")