{
"cells": [
{
"cell_type": "markdown",
"id": "4959935a",
"metadata": {},
"source": [
"# plotly: biblioteca para gráficos interactivos\n",
"\n",
"[](https://colab.research.google.com/github/gf0657-programacionsig/2024-ii/blob/main/contenido/3/plotly.ipynb)"
]
},
{
"cell_type": "markdown",
"id": "0a8ee33f",
"metadata": {},
"source": [
"---\n",
"\n",
"**NOTA IMPORTANTE**\n",
"\n",
"Debido a un problema de Jupyter Book (la biblioteca que se utiliza para construir el sitio web de este curso), los gráficos en plotly no se están desplegando, pero pueden verse en:\n",
"\n",
"[https://colab.research.google.com/github/gf0657-programacionsig/2024-ii/blob/main/contenido/3/plotly.ipynb](https://colab.research.google.com/github/gf0657-programacionsig/2024-ii/blob/main/contenido/3/plotly.ipynb)\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "b5cf76a4",
"metadata": {},
"source": [
"## Introducción"
]
},
{
"cell_type": "markdown",
"id": "2352666f",
"metadata": {},
"source": [
"[plotly Python](https://plotly.com/python/) es una biblioteca para la creación de gráficos interactivos que forma parte del [grupo de bibliotecas de graficación de plotly](https://plotly.com/graphing-libraries/), el cual también incluye bibliotecas para otros lenguajes como R, Julia, F# y MATLAB. plotly fue originalmente escrita en [JavaScript](https://en.wikipedia.org/wiki/JavaScript), por lo que es particularmente adecuada para gráficos interactivos en la Web. Cuenta con dos módulos principales para crear gráficos: `plotly.express` y `plotly.graph_objects`.\n",
"\n",
"[`plotly.express`](https://plotly.com/python/plotly-express/) es una interfaz de alto nivel que facilita la creación rápida de gráficos complejos con pocas líneas de código. Se recomienda como punto de partida para programar los tipos de gráficos estadísticos más comunes.\n",
"\n",
"Por su parte, [`plotly.graph_objects`](https://plotly.com/python/graph-objects/) es una interfaz de bajo nivel que ofrece un control más detallado sobre cada aspecto de la visualización. Se utiliza para personalizar gráficos a profundidad o crear visualizaciones más complejas que las que se programan con `plotly.express`."
]
},
{
"cell_type": "markdown",
"id": "ff5b1498",
"metadata": {},
"source": [
"## Carga"
]
},
{
"cell_type": "code",
"execution_count": 210,
"id": "c9c72708",
"metadata": {},
"outputs": [],
"source": [
"# Carga de plotly.express con el alias px\n",
"import plotly.express as px\n",
"\n",
"# Carga de plotly.graph_objects con el alias go\n",
"import plotly.graph_objects as go"
]
},
{
"cell_type": "markdown",
"id": "fd025a52",
"metadata": {},
"source": [
"## Tipos de gráficos"
]
},
{
"cell_type": "markdown",
"id": "49038fc4",
"metadata": {},
"source": [
"En los siguientes ejemplos, se utiliza el conjunto de datos de [países de Natural Earth](https://github.com/gf0657-programacionsig/2024-ii/blob/main/datos/natural-earth/paises.csv), el cual se carga y se configura en el siguiente bloque de código."
]
},
{
"cell_type": "code",
"execution_count": 211,
"id": "772b362e",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" NAME | \n",
" CONTINENT | \n",
" REGION_UN | \n",
" SUBREGION | \n",
" REGION_WB | \n",
" ECONOMY | \n",
" INCOME_GRP | \n",
" POP_EST | \n",
" GDP_MD | \n",
"
\n",
" \n",
" ADM0_ISO | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" BFA | \n",
" Burkina Faso | \n",
" Africa | \n",
" Africa | \n",
" Western Africa | \n",
" Sub-Saharan Africa | \n",
" 7. Least developed region | \n",
" 5. Low income | \n",
" 20,321,378.00 | \n",
" 15990 | \n",
"
\n",
" \n",
" BTN | \n",
" Bhutan | \n",
" Asia | \n",
" Asia | \n",
" Southern Asia | \n",
" South Asia | \n",
" 7. Least developed region | \n",
" 4. Lower middle income | \n",
" 763,092.00 | \n",
" 2530 | \n",
"
\n",
" \n",
" TUR | \n",
" Turkey | \n",
" Asia | \n",
" Asia | \n",
" Western Asia | \n",
" Europe & Central Asia | \n",
" 4. Emerging region: MIKT | \n",
" 3. Upper middle income | \n",
" 83,429,615.00 | \n",
" 761425 | \n",
"
\n",
" \n",
" TTO | \n",
" Trinidad and Tobago | \n",
" North America | \n",
" Americas | \n",
" Caribbean | \n",
" Latin America & Caribbean | \n",
" 6. Developing region | \n",
" 2. High income: nonOECD | \n",
" 1,394,973.00 | \n",
" 24269 | \n",
"
\n",
" \n",
" MOZ | \n",
" Mozambique | \n",
" Africa | \n",
" Africa | \n",
" Eastern Africa | \n",
" Sub-Saharan Africa | \n",
" 7. Least developed region | \n",
" 5. Low income | \n",
" 30,366,036.00 | \n",
" 15291 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" NAME CONTINENT REGION_UN SUBREGION \\\n",
"ADM0_ISO \n",
"BFA Burkina Faso Africa Africa Western Africa \n",
"BTN Bhutan Asia Asia Southern Asia \n",
"TUR Turkey Asia Asia Western Asia \n",
"TTO Trinidad and Tobago North America Americas Caribbean \n",
"MOZ Mozambique Africa Africa Eastern Africa \n",
"\n",
" REGION_WB ECONOMY \\\n",
"ADM0_ISO \n",
"BFA Sub-Saharan Africa 7. Least developed region \n",
"BTN South Asia 7. Least developed region \n",
"TUR Europe & Central Asia 4. Emerging region: MIKT \n",
"TTO Latin America & Caribbean 6. Developing region \n",
"MOZ Sub-Saharan Africa 7. Least developed region \n",
"\n",
" INCOME_GRP POP_EST GDP_MD \n",
"ADM0_ISO \n",
"BFA 5. Low income 20,321,378.00 15990 \n",
"BTN 4. Lower middle income 763,092.00 2530 \n",
"TUR 3. Upper middle income 83,429,615.00 761425 \n",
"TTO 2. High income: nonOECD 1,394,973.00 24269 \n",
"MOZ 5. Low income 30,366,036.00 15291 "
]
},
"execution_count": 211,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"# Configuración de pandas para mostrar separadores de miles y 2 dígitos decimales\n",
"pd.set_option('display.float_format', '{:,.2f}'.format)\n",
"\n",
"# Carga de datos de países en un dataframe\n",
"paises = pd.read_csv(\n",
" \"https://raw.githubusercontent.com/gf0657-programacionsig/2024-ii/refs/heads/main/datos/natural-earth/paises.csv\"\n",
")\n",
"\n",
"# Se usa la columna ADM0_ISO como índice\n",
"paises.set_index('ADM0_ISO', inplace=True)\n",
"\n",
"# Despliegue de una muestra aleatoria de 5 filas\n",
"paises.sample(5)\n"
]
},
{
"cell_type": "markdown",
"id": "d0310766",
"metadata": {},
"source": [
"### Gráficos de dispersión"
]
},
{
"cell_type": "markdown",
"id": "36cc130a",
"metadata": {},
"source": [
"Un [gráfico de dispersión (en inglés, *scatter plot*)](https://es.wikipedia.org/wiki/Diagrama_de_dispersi%C3%B3n) despliega los valores de dos variables numéricas, como puntos en un sistema de coordenadas. El valor de una variable se despliega en el eje X y el de la otra variable en el eje Y. Variables adicionales pueden ser mostradas mediante atributos de los puntos, tales como su tamaño, color o forma.\n",
"\n",
"En `plotly.express`, los gráficos de dispersión se generan con el método [`plotly.express.scatter()`](https://plotly.github.io/plotly.py-docs/generated/plotly.express.scatter.html). Se recomienda revisar el [tutorial](https://plotly.com/python/line-and-scatter/)."
]
},
{
"cell_type": "markdown",
"id": "c15ad649",
"metadata": {},
"source": [
"#### Ejemplos"
]
},
{
"cell_type": "markdown",
"id": "2f889f65",
"metadata": {},
"source": [
"##### Población vs PIB"
]
},
{
"cell_type": "code",
"execution_count": 212,
"id": "66351610",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"hovertemplate": "Población (habitantes)=%{x}
PIB (millones de dólares)=%{y}
ADM0_ISO=%{text}",
"legendgroup": "",
"marker": {
"color": "red",
"symbol": "circle"
},
"mode": "markers+text",
"name": "",
"orientation": "v",
"showlegend": false,
"text": [
"PAN",
"CRI",
"NIC",
"SLV",
"HND",
"GTM",
"BLZ"
],
"textposition": "top center",
"type": "scatter",
"x": [
4246439,
5047561,
6545502,
6453553,
9746117,
16604026,
390353
],
"xaxis": "x",
"y": [
66800,
61801,
12520,
27022,
25095,
76710,
1879
],
"yaxis": "y"
}
],
"layout": {
"legend": {
"tracegroupgap": 0
},
"template": {
"data": {
"bar": [
{
"error_x": {
"color": "#2a3f5f"
},
"error_y": {
"color": "#2a3f5f"
},
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "bar"
}
],
"barpolar": [
{
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "barpolar"
}
],
"carpet": [
{
"aaxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"baxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"type": "carpet"
}
],
"choropleth": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "choropleth"
}
],
"contour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "contour"
}
],
"contourcarpet": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "contourcarpet"
}
],
"heatmap": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmap"
}
],
"heatmapgl": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmapgl"
}
],
"histogram": [
{
"marker": {
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "histogram"
}
],
"histogram2d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2d"
}
],
"histogram2dcontour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2dcontour"
}
],
"mesh3d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "mesh3d"
}
],
"parcoords": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "parcoords"
}
],
"pie": [
{
"automargin": true,
"type": "pie"
}
],
"scatter": [
{
"fillpattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
},
"type": "scatter"
}
],
"scatter3d": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter3d"
}
],
"scattercarpet": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattercarpet"
}
],
"scattergeo": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergeo"
}
],
"scattergl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergl"
}
],
"scattermapbox": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattermapbox"
}
],
"scatterpolar": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolar"
}
],
"scatterpolargl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolargl"
}
],
"scatterternary": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterternary"
}
],
"surface": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "surface"
}
],
"table": [
{
"cells": {
"fill": {
"color": "#EBF0F8"
},
"line": {
"color": "white"
}
},
"header": {
"fill": {
"color": "#C8D4E3"
},
"line": {
"color": "white"
}
},
"type": "table"
}
]
},
"layout": {
"annotationdefaults": {
"arrowcolor": "#2a3f5f",
"arrowhead": 0,
"arrowwidth": 1
},
"autotypenumbers": "strict",
"coloraxis": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"colorscale": {
"diverging": [
[
0,
"#8e0152"
],
[
0.1,
"#c51b7d"
],
[
0.2,
"#de77ae"
],
[
0.3,
"#f1b6da"
],
[
0.4,
"#fde0ef"
],
[
0.5,
"#f7f7f7"
],
[
0.6,
"#e6f5d0"
],
[
0.7,
"#b8e186"
],
[
0.8,
"#7fbc41"
],
[
0.9,
"#4d9221"
],
[
1,
"#276419"
]
],
"sequential": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"sequentialminus": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
]
},
"colorway": [
"#636efa",
"#EF553B",
"#00cc96",
"#ab63fa",
"#FFA15A",
"#19d3f3",
"#FF6692",
"#B6E880",
"#FF97FF",
"#FECB52"
],
"font": {
"color": "#2a3f5f"
},
"geo": {
"bgcolor": "white",
"lakecolor": "white",
"landcolor": "#E5ECF6",
"showlakes": true,
"showland": true,
"subunitcolor": "white"
},
"hoverlabel": {
"align": "left"
},
"hovermode": "closest",
"mapbox": {
"style": "light"
},
"paper_bgcolor": "white",
"plot_bgcolor": "#E5ECF6",
"polar": {
"angularaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"radialaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"scene": {
"xaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"yaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"zaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
}
},
"shapedefaults": {
"line": {
"color": "#2a3f5f"
}
},
"ternary": {
"aaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"baxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"caxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"title": {
"x": 0.05
},
"xaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
},
"yaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
}
}
},
"title": {
"text": "Relación entre población y producto interno bruto (PIB)"
},
"xaxis": {
"anchor": "y",
"domain": [
0,
1
],
"gridcolor": "lightgray",
"gridwidth": 0.5,
"showgrid": true,
"tickformat": ",",
"title": {
"text": "Población (habitantes)"
}
},
"yaxis": {
"anchor": "x",
"domain": [
0,
1
],
"gridcolor": "lightgray",
"gridwidth": 0.5,
"showgrid": true,
"tickformat": ",",
"title": {
"text": "PIB (millones de dólares)"
}
}
}
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Subconjunto de países seleccionados\n",
"paises_seleccionados = paises.loc[['PAN', 'CRI', 'NIC', 'SLV', 'HND', 'GTM', 'BLZ']]\n",
"\n",
"# Creación del gráfico de dispersión\n",
"fig = px.scatter(\n",
" paises_seleccionados,\n",
" x='POP_EST',\n",
" y='GDP_MD',\n",
" text=paises_seleccionados.index, # texto sobre cada punto\n",
" title='Relación entre población y producto interno bruto (PIB)',\n",
" labels={\n",
" 'POP_EST': 'Población (habitantes)',\n",
" 'GDP_MD': 'PIB (millones de dólares)'\n",
" }\n",
")\n",
"\n",
"# Atributos globales de la figura\n",
"fig.update_layout(\n",
" xaxis_tickformat=',',\n",
" yaxis_tickformat=',',\n",
" xaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray'),\n",
" yaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray')\n",
")\n",
"\n",
"# Atributos de los elementos visuales del gráfico\n",
"fig.update_traces(marker=dict(color='red'), textposition='top center')\n",
"\n",
"# Despliegue del gráfico\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"id": "ea391306",
"metadata": {},
"source": [
"##### Esperanza de vida vs PIB per cápita"
]
},
{
"cell_type": "markdown",
"id": "0e31482c",
"metadata": {},
"source": [
"Se agrega la columna `LIFE_EXPECTANCY` (esperanza de vida al nacer) al dataframe `paises`."
]
},
{
"cell_type": "code",
"execution_count": 213,
"id": "9392efd7",
"metadata": {},
"outputs": [],
"source": [
"# Carga de datos de esperanza de vida al nacer por país\n",
"esperanza_vida = pd.read_csv(\n",
" \"https://raw.githubusercontent.com/gf0657-programacionsig/2024-ii/refs/heads/main/datos/world-bank/paises-esperanza-vida.csv\"\n",
")\n",
"\n",
"# Se usa la columna 'Country Code' como índice\n",
"esperanza_vida.set_index('Country Code', inplace=True)\n",
"\n",
"# Reducción de columnas de esperanza_vida\n",
"esperanza_vida = esperanza_vida[['2022']]\n",
"\n",
"# Unión de los dataframes paises y esperanza_vida\n",
"paises = paises.join(esperanza_vida, how=\"left\")\n",
"\n",
"# Cambio de nombre de la nueva columna\n",
"paises.rename(columns={'2022': 'LIFE_EXPECTANCY'}, inplace=True)"
]
},
{
"cell_type": "markdown",
"id": "ea1bec0c",
"metadata": {},
"source": [
"Se agrega la columna `GDP_PC` (producto interno bruto per cápita) al dataframe `paises`."
]
},
{
"cell_type": "code",
"execution_count": 214,
"id": "1cd37b97",
"metadata": {},
"outputs": [],
"source": [
"def pib_per_capita(pib, poblacion):\n",
" \"\"\"\n",
" Retorna el PIB per cápita dados el PIB de un país (en millones de dólares) y su población.\n",
" \"\"\"\n",
"\n",
" return (pib * 1000000) / poblacion\n",
"\n",
"# Creación de la columna GDP_PC (PIB per cápita en dólares)\n",
"paises['GDP_PC'] = pib_per_capita(paises['GDP_MD'], paises['POP_EST'])"
]
},
{
"cell_type": "markdown",
"id": "a2fad799",
"metadata": {},
"source": [
"Se genera el gráfico de dispersión."
]
},
{
"cell_type": "code",
"execution_count": 215,
"id": "c736a2e8",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"customdata": [
[
"Indonesia"
],
[
"Malaysia"
],
[
"Cyprus"
],
[
"India"
],
[
"China"
],
[
"Israel"
],
[
"Lebanon"
],
[
"Syria"
],
[
"South Korea"
],
[
"North Korea"
],
[
"Bhutan"
],
[
"Oman"
],
[
"Uzbekistan"
],
[
"Kazakhstan"
],
[
"Tajikistan"
],
[
"Mongolia"
],
[
"Vietnam"
],
[
"Cambodia"
],
[
"United Arab Emirates"
],
[
"Georgia"
],
[
"Azerbaijan"
],
[
"Turkey"
],
[
"Laos"
],
[
"Kyrgyzstan"
],
[
"Armenia"
],
[
"Iraq"
],
[
"Iran"
],
[
"Qatar"
],
[
"Saudi Arabia"
],
[
"Pakistan"
],
[
"Thailand"
],
[
"Kuwait"
],
[
"Timor-Leste"
],
[
"Brunei"
],
[
"Myanmar"
],
[
"Bangladesh"
],
[
"Afghanistan"
],
[
"Turkmenistan"
],
[
"Jordan"
],
[
"Nepal"
],
[
"Yemen"
],
[
"N. Cyprus"
],
[
"Cyprus U.N. Buffer Zone"
],
[
"Philippines"
],
[
"Sri Lanka"
],
[
"Taiwan"
],
[
"Japan"
],
[
"Singapore"
],
[
"Bahrain"
]
],
"hovertemplate": "Continente=Asia
PIB per cápita (dólares)=%{x:,.2f}
Esperanza de vida (años)=%{y:.2f}
País=%{customdata[0]}",
"legendgroup": "Asia",
"marker": {
"color": "#636efa",
"symbol": "circle"
},
"mode": "markers",
"name": "Asia",
"orientation": "v",
"showlegend": true,
"textfont": {
"size": 6
},
"textposition": "top center",
"type": "scatter",
"x": [
4135.566377822808,
11414.195473101425,
20814.717477003942,
2099.598743943135,
10500.649513928935,
29910.458164387273,
7583.602172377986,
5789.643725723317,
31846.213987333525,
1558.472262369117,
3315.4586865017586,
15342.957749026831,
1724.8326044909793,
9812.341301927792,
870.7203440654229,
4339.620242920754,
2715.2734981755425,
1643.0977460282454,
43103.29563527215,
2038.7692357877397,
4793.522464317704,
9126.555360467622,
2534.781235114803,
1309.2970310830274,
4622.46228612406,
5955.107918046762,
5475.510947463988,
62087.867271501695,
23139.77419747939,
1284.69785729957,
7806.728279844037,
32000.31946125142,
1559.794574203921,
31085.77495182155,
1407.7973674735065,
1855.7382654351488,
507.1006978279708,
6859.708765721954,
4405.399728006016,
1071.0374567745278,
774.3316781383614,
11042.944785276073,
28000,
3485.079513449436,
3853.0477457230654,
47818.30977082937,
40246.87583284705,
65233.18995527187,
23503.93499279783
],
"xaxis": "x",
"y": [
68.25,
76.26,
81.889,
67.744,
78.587,
82.7,
74.416,
72.3,
82.6804878048781,
73.578,
72.229,
73.935,
71.674,
74.44,
71.288,
72.667,
74.58,
69.896,
79.196,
71.587,
73.488,
78.475,
68.999,
72.0487804878049,
73.372,
71.336,
74.556,
81.559,
77.905,
66.431,
79.68,
80.264,
69.056,
74.551,
67.256,
73.698,
62.879,
69.41,
74.215,
70.484,
63.72,
81.889,
81.889,
72.187,
76.61,
null,
83.9963414634146,
82.8951219512195,
79.246
],
"yaxis": "y"
},
{
"customdata": [
[
"Chile"
],
[
"Bolivia"
],
[
"Peru"
],
[
"Argentina"
],
[
"Suriname"
],
[
"Guyana"
],
[
"Brazil"
],
[
"Uruguay"
],
[
"Ecuador"
],
[
"Colombia"
],
[
"Paraguay"
],
[
"Venezuela"
]
],
"hovertemplate": "Continente=South America
PIB per cápita (dólares)=%{x:,.2f}
Esperanza de vida (años)=%{y:.2f}
País=%{customdata[0]}",
"legendgroup": "South America",
"marker": {
"color": "#EF553B",
"symbol": "circle"
},
"mode": "markers",
"name": "South America",
"orientation": "v",
"showlegend": true,
"textfont": {
"size": 6
},
"textposition": "top center",
"type": "scatter",
"x": [
14896.445437688548,
3552.040718833329,
6977.694220378904,
9912.277859676975,
6359.193825544453,
6608.6161126058105,
8717.18608495152,
16189.86323039263,
6183.785548492885,
6428.656749340671,
5414.758122350111,
16915.482274774477
],
"xaxis": "x",
"y": [
79.519,
64.928,
73.385,
76.064,
70.289,
65.989,
73.425,
78,
77.894,
73.659,
70.475,
71.105
],
"yaxis": "y"
},
{
"customdata": [
[
"United Kingdom"
],
[
"France"
],
[
"Ukraine"
],
[
"Belarus"
],
[
"Netherlands"
],
[
"Lithuania"
],
[
"Russia"
],
[
"Czechia"
],
[
"Germany"
],
[
"Estonia"
],
[
"Latvia"
],
[
"Norway"
],
[
"Sweden"
],
[
"Finland"
],
[
"Luxembourg"
],
[
"Belgium"
],
[
"North Macedonia"
],
[
"Albania"
],
[
"Kosovo"
],
[
"Spain"
],
[
"Denmark"
],
[
"Romania"
],
[
"Hungary"
],
[
"Slovakia"
],
[
"Poland"
],
[
"Ireland"
],
[
"Greece"
],
[
"Austria"
],
[
"Italy"
],
[
"Switzerland"
],
[
"Liechtenstein"
],
[
"Serbia"
],
[
"Croatia"
],
[
"Slovenia"
],
[
"Bulgaria"
],
[
"San Marino"
],
[
"Monaco"
],
[
"Andorra"
],
[
"Montenegro"
],
[
"Bosnia and Herz."
],
[
"Portugal"
],
[
"Moldova"
],
[
"Vatican"
],
[
"Iceland"
],
[
"Malta"
]
],
"hovertemplate": "Continente=Europe
PIB per cápita (dólares)=%{x:,.2f}
Esperanza de vida (años)=%{y:.2f}
País=%{customdata[0]}",
"legendgroup": "Europe",
"marker": {
"color": "#00cc96",
"symbol": "circle"
},
"mode": "markers",
"name": "Europe",
"orientation": "v",
"showlegend": true,
"textfont": {
"size": 6
},
"textposition": "top center",
"type": "scatter",
"x": [
42501.354345964275,
40371.98612693919,
3464.6944456992433,
6663.247016749806,
51843.819693736645,
19601.743046973566,
11774.152374948775,
23494.5489141269,
46445.2423886269,
23723.23023692324,
17828.417039202966,
75419.56687265422,
51614.93616275336,
40665.21590761266,
114703.11148966923,
46420.624073987805,
6022.196741092577,
5353.180638576746,
4417.449538748267,
29600.36711091185,
60145.80001944811,
12919.506705329215,
16731.817126169237,
19266.152103208005,
15692.501573706204,
78660.81250743709,
19582.46495392729,
50137.618652647325,
33228.23426736372,
81993.67637756634,
180856.9399510771,
7411.833735902577,
14935.955746773203,
25946.07331798811,
9828.031665649096,
48877.731836975785,
184477.97967354482,
40885.639470068185,
8908.005792936283,
6108.451984247198,
23252.050238100175,
4503.248562538827,
-120000,
66944.72659439323,
29819.776267126625
],
"xaxis": "x",
"y": [
82.0585365853659,
82.2292682926829,
68.5879268292683,
73.1114634146342,
81.7146341463415,
75.7926829268293,
72.5456097560976,
79.0292682926829,
80.7056097560976,
77.9439024390244,
74.580487804878,
82.5609756097561,
83.109756097561,
81.1878048780488,
83.0463414634146,
81.6975609756098,
74.4248780487805,
76.833,
75.4821951219512,
83.0829268292683,
81.3048780487805,
75.3048780487805,
76.0170731707317,
77.0658536585366,
77.3048780487805,
83.0560975609756,
80.6365853658537,
81.0926829268293,
82.9,
83.4536585365854,
84.319512195122,
75.4821951219512,
77.5756097560976,
81.2829268292683,
74.3609756097561,
null,
null,
null,
76.1878048780488,
75.293,
null,
68.621,
null,
82.1707317073171,
82.7
],
"yaxis": "y"
},
{
"customdata": [
[
"Ethiopia"
],
[
"S. Sudan"
],
[
"Somalia"
],
[
"Kenya"
],
[
"Malawi"
],
[
"Tanzania"
],
[
"Somaliland"
],
[
"Morocco"
],
[
"W. Sahara"
],
[
"Congo"
],
[
"Dem. Rep. Congo"
],
[
"Namibia"
],
[
"South Africa"
],
[
"Libya"
],
[
"Tunisia"
],
[
"Zambia"
],
[
"Sierra Leone"
],
[
"Guinea"
],
[
"Liberia"
],
[
"Central African Rep."
],
[
"Sudan"
],
[
"Djibouti"
],
[
"Eritrea"
],
[
"Côte d'Ivoire"
],
[
"Mali"
],
[
"Senegal"
],
[
"Nigeria"
],
[
"Benin"
],
[
"Angola"
],
[
"Botswana"
],
[
"Zimbabwe"
],
[
"Chad"
],
[
"Algeria"
],
[
"Mozambique"
],
[
"eSwatini"
],
[
"Burundi"
],
[
"Rwanda"
],
[
"Uganda"
],
[
"Lesotho"
],
[
"Cameroon"
],
[
"Gabon"
],
[
"Niger"
],
[
"Burkina Faso"
],
[
"Togo"
],
[
"Ghana"
],
[
"Guinea-Bissau"
],
[
"Egypt"
],
[
"Mauritania"
],
[
"Eq. Guinea"
],
[
"Gambia"
],
[
"Madagascar"
],
[
"Comoros"
],
[
"São Tomé and Principe"
],
[
"Cabo Verde"
]
],
"hovertemplate": "Continente=Africa
PIB per cápita (dólares)=%{x:,.2f}
Esperanza de vida (años)=%{y:.2f}
País=%{customdata[0]}",
"legendgroup": "Africa",
"marker": {
"color": "#ab63fa",
"symbol": "circle"
},
"mode": "markers",
"name": "Africa",
"orientation": "v",
"showlegend": true,
"textfont": {
"size": 6
},
"textposition": "top center",
"type": "scatter",
"x": [
855.7555925196511,
1084.6029144703186,
462.99578997604397,
1816.5452323719192,
411.51452644667944,
1089.1560334584349,
3499.890800110436,
3281.990517103791,
1503.5151089178173,
2279.8962477149,
580.708269828448,
4957.246455244074,
6001.389726848146,
7685.926805531046,
3317.394800165784,
1305.0199232631041,
527.439728716028,
962.7878125595577,
621.788019299328,
467.84266577593917,
712.7001232656124,
3414.2733883890055,
339.5713606336648,
2276.3167554707197,
878.9791815874133,
1446.825807278237,
2229.856562232447,
1219.3725849283685,
2790.7046894616374,
7961.116414181205,
1463.9340989308091,
709.4806531385834,
3973.9573410982644,
503.55601238172807,
3894.1583270187175,
261.2184296019801,
819.9921596268299,
794.3375310828466,
1117.976650474199,
1507.4365115986084,
7766.806178279363,
553.8654648731281,
786.8560882042547,
679.2565444326575,
2202.0947169978053,
697.0610987848544,
3019.203287227159,
1679.2997143422801,
8131.352388593982,
777.7805227741463,
523.3356570860349,
1369.1610862089633,
1943.6797857302283,
3602.2439015520017
],
"xaxis": "x",
"y": [
65.645,
55.567,
56.107,
62.055,
62.898,
66.782,
56.107,
74.973,
null,
63.053,
59.743,
58.059,
61.48,
72.151,
74.263,
61.803,
60.411,
58.985,
61.1,
54.477,
null,
62.859,
66.604,
58.916,
59.417,
67.913,
53.633,
59.954,
61.929,
65.913,
59.391,
52.997,
77.129,
59.625,
56.36,
61.977,
67.129,
63.638,
53.036,
60.958,
65.694,
62.08,
59.766,
61.588,
63.945,
59.861,
70.159,
64.691,
61.19,
62.906,
65.23,
63.68,
68.794,
74.722
],
"yaxis": "y"
},
{
"customdata": [
[
"Costa Rica"
],
[
"Nicaragua"
],
[
"Haiti"
],
[
"Dominican Rep."
],
[
"El Salvador"
],
[
"Guatemala"
],
[
"Cuba"
],
[
"Honduras"
],
[
"United States of America"
],
[
"Canada"
],
[
"Mexico"
],
[
"Belize"
],
[
"Panama"
],
[
"Bahamas"
],
[
"Trinidad and Tobago"
],
[
"Grenada"
],
[
"St. Vin. and Gren."
],
[
"Barbados"
],
[
"Saint Lucia"
],
[
"Dominica"
],
[
"Antigua and Barb."
],
[
"St. Kitts and Nevis"
],
[
"Jamaica"
]
],
"hovertemplate": "Continente=North America
PIB per cápita (dólares)=%{x:,.2f}
Esperanza de vida (años)=%{y:.2f}
País=%{customdata[0]}",
"legendgroup": "North America",
"marker": {
"color": "#FFA15A",
"symbol": "circle"
},
"mode": "markers",
"name": "North America",
"orientation": "v",
"showlegend": true,
"textfont": {
"size": 6
},
"textposition": "top center",
"type": "scatter",
"x": [
12243.735142576781,
1912.7639102394285,
1272.4764289545387,
8282.088448432334,
4187.150860928856,
4619.963856958548,
8825.442275777004,
2574.871612971607,
64966.82735041065,
46194.70847818188,
9946.029696651307,
4813.591800242345,
15730.827641701671,
34861.68808828136,
17397.469341700522,
10803.282054944957,
7451.0123068297935,
18148.24492640014,
11608.950161387384,
8104.946524064171,
17102.905743528492,
19930.34788204565,
5582.239672703974
],
"xaxis": "x",
"y": [
77.32,
74.615,
63.728,
74.17,
71.475,
68.674,
78.155,
70.728,
77.4341463414634,
81.2956097560976,
74.832,
70.962,
76.826,
74.358,
74.708,
75.335,
68.972,
77.706,
71.294,
72.981,
79.236,
72.027,
70.629
],
"yaxis": "y"
},
{
"customdata": [
[
"Papua New Guinea"
],
[
"Australia"
],
[
"Fiji"
],
[
"New Zealand"
],
[
"Kiribati"
],
[
"Marshall Is."
],
[
"Tonga"
],
[
"Samoa"
],
[
"Solomon Is."
],
[
"Tuvalu"
],
[
"Nauru"
],
[
"Micronesia"
],
[
"Vanuatu"
],
[
"Palau"
]
],
"hovertemplate": "Continente=Oceania
PIB per cápita (dólares)=%{x:,.2f}
Esperanza de vida (años)=%{y:.2f}
País=%{customdata[0]}",
"legendgroup": "Oceania",
"marker": {
"color": "#19d3f3",
"symbol": "circle"
},
"mode": "markers",
"name": "Oceania",
"orientation": "v",
"showlegend": true,
"textfont": {
"size": 6
},
"textposition": "top center",
"type": "scatter",
"x": [
2829.1581155156573,
55053.069575397276,
6175.607026438475,
41872.18784413436,
1649.57570191997,
3759.0787705601197,
4899.802859494325,
4322.744638426764,
2372.268494811316,
4035.7204190279926,
9379.22263731023,
3523.2614330272813,
3114.5583929679005,
14882.274544646823
],
"xaxis": "x",
"y": [
null,
83.2,
68.312,
82.7609756097561,
67.661,
65.146,
71.27,
72.598,
70.742,
64.854,
64.014,
70.925,
70.492,
null
],
"yaxis": "y"
},
{
"customdata": [
[
"Antarctica"
]
],
"hovertemplate": "Continente=Antarctica
PIB per cápita (dólares)=%{x:,.2f}
Esperanza de vida (años)=%{y:.2f}
País=%{customdata[0]}",
"legendgroup": "Antarctica",
"marker": {
"color": "#FF6692",
"symbol": "circle"
},
"mode": "markers",
"name": "Antarctica",
"orientation": "v",
"showlegend": true,
"textfont": {
"size": 6
},
"textposition": "top center",
"type": "scatter",
"x": [
200000
],
"xaxis": "x",
"y": [
null
],
"yaxis": "y"
},
{
"customdata": [
[
"Seychelles"
],
[
"Mauritius"
],
[
"Maldives"
]
],
"hovertemplate": "Continente=Seven seas (open ocean)
PIB per cápita (dólares)=%{x:,.2f}
Esperanza de vida (años)=%{y:.2f}
País=%{customdata[0]}",
"legendgroup": "Seven seas (open ocean)",
"marker": {
"color": "#B6E880",
"symbol": "circle"
},
"mode": "markers",
"name": "Seven seas (open ocean)",
"orientation": "v",
"showlegend": true,
"textfont": {
"size": 6
},
"textposition": "top center",
"type": "scatter",
"x": [
17444.302176696543,
11098.900143871706,
10626.175951543733
],
"xaxis": "x",
"y": [
73.8487804878049,
73.5146341463415,
80.839
],
"yaxis": "y"
}
],
"layout": {
"legend": {
"title": {
"text": "Continente"
},
"tracegroupgap": 0
},
"template": {
"data": {
"bar": [
{
"error_x": {
"color": "#2a3f5f"
},
"error_y": {
"color": "#2a3f5f"
},
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "bar"
}
],
"barpolar": [
{
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "barpolar"
}
],
"carpet": [
{
"aaxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"baxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"type": "carpet"
}
],
"choropleth": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "choropleth"
}
],
"contour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "contour"
}
],
"contourcarpet": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "contourcarpet"
}
],
"heatmap": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmap"
}
],
"heatmapgl": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmapgl"
}
],
"histogram": [
{
"marker": {
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "histogram"
}
],
"histogram2d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2d"
}
],
"histogram2dcontour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2dcontour"
}
],
"mesh3d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "mesh3d"
}
],
"parcoords": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "parcoords"
}
],
"pie": [
{
"automargin": true,
"type": "pie"
}
],
"scatter": [
{
"fillpattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
},
"type": "scatter"
}
],
"scatter3d": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter3d"
}
],
"scattercarpet": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattercarpet"
}
],
"scattergeo": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergeo"
}
],
"scattergl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergl"
}
],
"scattermapbox": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattermapbox"
}
],
"scatterpolar": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolar"
}
],
"scatterpolargl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolargl"
}
],
"scatterternary": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterternary"
}
],
"surface": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "surface"
}
],
"table": [
{
"cells": {
"fill": {
"color": "#EBF0F8"
},
"line": {
"color": "white"
}
},
"header": {
"fill": {
"color": "#C8D4E3"
},
"line": {
"color": "white"
}
},
"type": "table"
}
]
},
"layout": {
"annotationdefaults": {
"arrowcolor": "#2a3f5f",
"arrowhead": 0,
"arrowwidth": 1
},
"autotypenumbers": "strict",
"coloraxis": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"colorscale": {
"diverging": [
[
0,
"#8e0152"
],
[
0.1,
"#c51b7d"
],
[
0.2,
"#de77ae"
],
[
0.3,
"#f1b6da"
],
[
0.4,
"#fde0ef"
],
[
0.5,
"#f7f7f7"
],
[
0.6,
"#e6f5d0"
],
[
0.7,
"#b8e186"
],
[
0.8,
"#7fbc41"
],
[
0.9,
"#4d9221"
],
[
1,
"#276419"
]
],
"sequential": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"sequentialminus": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
]
},
"colorway": [
"#636efa",
"#EF553B",
"#00cc96",
"#ab63fa",
"#FFA15A",
"#19d3f3",
"#FF6692",
"#B6E880",
"#FF97FF",
"#FECB52"
],
"font": {
"color": "#2a3f5f"
},
"geo": {
"bgcolor": "white",
"lakecolor": "white",
"landcolor": "#E5ECF6",
"showlakes": true,
"showland": true,
"subunitcolor": "white"
},
"hoverlabel": {
"align": "left"
},
"hovermode": "closest",
"mapbox": {
"style": "light"
},
"paper_bgcolor": "white",
"plot_bgcolor": "#E5ECF6",
"polar": {
"angularaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"radialaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"scene": {
"xaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"yaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"zaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
}
},
"shapedefaults": {
"line": {
"color": "#2a3f5f"
}
},
"ternary": {
"aaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"baxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"caxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"title": {
"x": 0.05
},
"xaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
},
"yaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
}
}
},
"title": {
"text": "Relación entre PIB per cápita y esperanza de vida"
},
"xaxis": {
"anchor": "y",
"domain": [
0,
1
],
"gridcolor": "lightgray",
"gridwidth": 0.5,
"range": [
-2500,
210000
],
"showgrid": true,
"tickformat": ",",
"title": {
"text": "PIB per cápita (dólares)"
}
},
"yaxis": {
"anchor": "x",
"domain": [
0,
1
],
"gridcolor": "lightgray",
"gridwidth": 0.5,
"showgrid": true,
"tickformat": ",",
"title": {
"text": "Esperanza de vida (años)"
}
}
}
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Subconjunto de países seleccionados\n",
"paises_seleccionados = paises\n",
"\n",
"# Eliminar valores nulos en las columnas 'GDP_MD' y 'POP_EST'\n",
"paises = paises.dropna(subset=['GDP_MD', 'POP_EST'])\n",
"\n",
"# Creación del gráfico de dispersión\n",
"fig = px.scatter(\n",
" paises_seleccionados,\n",
" x='GDP_PC',\n",
" y='LIFE_EXPECTANCY',\n",
" color='CONTINENT', # para colorear los puntos por continente\n",
" title='Relación entre PIB per cápita y esperanza de vida',\n",
" labels={\n",
" 'NAME': 'País',\n",
" 'GDP_PC': 'PIB per cápita (dólares)',\n",
" 'LIFE_EXPECTANCY': 'Esperanza de vida (años)',\n",
" 'CONTINENT': 'Continente'\n",
" },\n",
" hover_data={\n",
" 'NAME': True, # para mostrar la columna NAME\n",
" 'GDP_PC': ':,.2f', # formato con dos decimales y separador de miles\n",
" 'LIFE_EXPECTANCY': ':.2f', # formato con dos decimales\n",
" }\n",
")\n",
"\n",
"# Atributos globales de la figura\n",
"fig.update_layout(\n",
" xaxis_tickformat=',',\n",
" yaxis_tickformat=',',\n",
" xaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray'),\n",
" yaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray')\n",
")\n",
"\n",
"# Atributos de los elementos visuales del gráfico\n",
"fig.update_traces(\n",
" textposition='top center',\n",
" textfont=dict(size=6)\n",
")\n",
"\n",
"# Ajuste del eje x para que comience en 0\n",
"x_max = paises_seleccionados['GDP_PC'].max() * 1.05 # Añade un 5% de margen superior\n",
"fig.update_xaxes(range=[-2500, x_max])\n",
"\n",
"# Despliegue del gráfico\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"id": "7e6e7034",
"metadata": {},
"source": [
"#### Ejercicios"
]
},
{
"cell_type": "markdown",
"id": "33c4e49d",
"metadata": {},
"source": [
"1. Programe gráficos de dispersión que muestren la relación entre los siguientes [indicadores del Banco Mundial](https://datos.bancomundial.org/indicador):\n",
"- Tasa de alfabetización de adultos y PIB per cápita.\n",
"\n",
"Considere todos los países del mundo y diferentes regiones (continentes, economías, grupos de ingreso, etc.)"
]
},
{
"cell_type": "markdown",
"id": "8a9312e3",
"metadata": {},
"source": [
"### Gráficos de líneas"
]
},
{
"cell_type": "markdown",
"id": "c24bc5b3",
"metadata": {},
"source": [
"Un [gráfico de líneas](https://en.wikipedia.org/wiki/Line_chart) muestra información en la forma de puntos de datos, llamados marcadores (*markers*), conectados por segmentos de líneas rectas. Es similar a un gráfico de dispersión pero, además del uso de segmentos de línea, tiene la particularidad de que los datos están ordenados, usualmente con respecto al eje X. Los gráficos de línea son usados frecuentemente para mostrar tendencias a través del tiempo.\n",
"\n",
"En `plotly.express`, los gráficos de líneas se generan mediante el método [`plotly.express.line()`](https://plotly.github.io/plotly.py-docs/generated/plotly.express.line.html). Se recomienda revisar el [tutorial](https://plotly.com/python/line-charts/)."
]
},
{
"cell_type": "markdown",
"id": "aa8367f0",
"metadata": {},
"source": [
"#### Ejemplos"
]
},
{
"cell_type": "markdown",
"id": "f2d9b4ca",
"metadata": {},
"source": [
"##### Evolución en el tiempo de la esperanza de vida al nacer"
]
},
{
"cell_type": "code",
"execution_count": 216,
"id": "3267f21a",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Country Name | \n",
" Country Code | \n",
" Indicator Name | \n",
" Indicator Code | \n",
" 1960 | \n",
" 1961 | \n",
" 1962 | \n",
" 1963 | \n",
" 1964 | \n",
" 1965 | \n",
" ... | \n",
" 2015 | \n",
" 2016 | \n",
" 2017 | \n",
" 2018 | \n",
" 2019 | \n",
" 2020 | \n",
" 2021 | \n",
" 2022 | \n",
" 2023 | \n",
" Unnamed: 68 | \n",
"
\n",
" \n",
" \n",
" \n",
" 45 | \n",
" Colombia | \n",
" COL | \n",
" Life expectancy at birth, total (years) | \n",
" SP.DYN.LE00.IN | \n",
" 57.13 | \n",
" 57.73 | \n",
" 58.30 | \n",
" 58.89 | \n",
" 59.38 | \n",
" 59.81 | \n",
" ... | \n",
" 76.26 | \n",
" 76.47 | \n",
" 76.65 | \n",
" 76.75 | \n",
" 76.75 | \n",
" 74.77 | \n",
" 72.83 | \n",
" 73.66 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 48 | \n",
" Costa Rica | \n",
" CRI | \n",
" Life expectancy at birth, total (years) | \n",
" SP.DYN.LE00.IN | \n",
" 60.41 | \n",
" 60.90 | \n",
" 61.11 | \n",
" 61.62 | \n",
" 62.08 | \n",
" 62.66 | \n",
" ... | \n",
" 79.09 | \n",
" 79.46 | \n",
" 79.38 | \n",
" 79.48 | \n",
" 79.43 | \n",
" 79.28 | \n",
" 77.02 | \n",
" 77.32 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 77 | \n",
" France | \n",
" FRA | \n",
" Life expectancy at birth, total (years) | \n",
" SP.DYN.LE00.IN | \n",
" 69.87 | \n",
" 70.12 | \n",
" 70.31 | \n",
" 70.51 | \n",
" 70.66 | \n",
" 70.81 | \n",
" ... | \n",
" 82.32 | \n",
" 82.57 | \n",
" 82.58 | \n",
" 82.68 | \n",
" 82.83 | \n",
" 82.18 | \n",
" 82.32 | \n",
" 82.23 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 100 | \n",
" Haiti | \n",
" HTI | \n",
" Life expectancy at birth, total (years) | \n",
" SP.DYN.LE00.IN | \n",
" 43.50 | \n",
" 43.91 | \n",
" 44.29 | \n",
" 42.77 | \n",
" 44.97 | \n",
" 45.35 | \n",
" ... | \n",
" 63.24 | \n",
" 63.39 | \n",
" 63.85 | \n",
" 64.02 | \n",
" 64.25 | \n",
" 64.05 | \n",
" 63.19 | \n",
" 63.73 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 119 | \n",
" Japan | \n",
" JPN | \n",
" Life expectancy at birth, total (years) | \n",
" SP.DYN.LE00.IN | \n",
" 67.70 | \n",
" 68.35 | \n",
" 68.63 | \n",
" 69.71 | \n",
" 70.21 | \n",
" 70.27 | \n",
" ... | \n",
" 83.79 | \n",
" 83.98 | \n",
" 84.10 | \n",
" 84.21 | \n",
" 84.36 | \n",
" 84.56 | \n",
" 84.45 | \n",
" 84.00 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 154 | \n",
" Mexico | \n",
" MEX | \n",
" Life expectancy at birth, total (years) | \n",
" SP.DYN.LE00.IN | \n",
" 55.02 | \n",
" 55.83 | \n",
" 56.60 | \n",
" 57.31 | \n",
" 57.95 | \n",
" 58.50 | \n",
" ... | \n",
" 74.68 | \n",
" 74.41 | \n",
" 74.14 | \n",
" 74.02 | \n",
" 74.20 | \n",
" 70.13 | \n",
" 70.21 | \n",
" 74.83 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 180 | \n",
" New Zealand | \n",
" NZL | \n",
" Life expectancy at birth, total (years) | \n",
" SP.DYN.LE00.IN | \n",
" 71.24 | \n",
" 70.99 | \n",
" 71.23 | \n",
" 71.28 | \n",
" 71.33 | \n",
" 71.23 | \n",
" ... | \n",
" 81.61 | \n",
" 81.66 | \n",
" 81.86 | \n",
" 81.71 | \n",
" 82.06 | \n",
" 82.26 | \n",
" 82.21 | \n",
" 82.76 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 229 | \n",
" Chad | \n",
" TCD | \n",
" Life expectancy at birth, total (years) | \n",
" SP.DYN.LE00.IN | \n",
" 38.37 | \n",
" 38.63 | \n",
" 38.84 | \n",
" 39.07 | \n",
" 39.33 | \n",
" 39.12 | \n",
" ... | \n",
" 51.59 | \n",
" 52.08 | \n",
" 52.31 | \n",
" 52.83 | \n",
" 53.26 | \n",
" 52.78 | \n",
" 52.52 | \n",
" 53.00 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 248 | \n",
" Ukraine | \n",
" UKR | \n",
" Life expectancy at birth, total (years) | \n",
" SP.DYN.LE00.IN | \n",
" 69.49 | \n",
" 69.57 | \n",
" 69.33 | \n",
" 69.79 | \n",
" 70.21 | \n",
" 69.87 | \n",
" ... | \n",
" 71.19 | \n",
" 71.48 | \n",
" 71.78 | \n",
" 71.58 | \n",
" 71.83 | \n",
" 71.19 | \n",
" 69.65 | \n",
" 68.59 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 251 | \n",
" United States | \n",
" USA | \n",
" Life expectancy at birth, total (years) | \n",
" SP.DYN.LE00.IN | \n",
" 69.77 | \n",
" 70.27 | \n",
" 70.12 | \n",
" 69.92 | \n",
" 70.17 | \n",
" 70.21 | \n",
" ... | \n",
" 78.69 | \n",
" 78.54 | \n",
" 78.54 | \n",
" 78.64 | \n",
" 78.79 | \n",
" 76.98 | \n",
" 76.33 | \n",
" 77.43 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 263 | \n",
" South Africa | \n",
" ZAF | \n",
" Life expectancy at birth, total (years) | \n",
" SP.DYN.LE00.IN | \n",
" 52.67 | \n",
" 53.09 | \n",
" 53.38 | \n",
" 53.63 | \n",
" 53.91 | \n",
" 54.19 | \n",
" ... | \n",
" 63.95 | \n",
" 64.75 | \n",
" 65.40 | \n",
" 65.67 | \n",
" 66.17 | \n",
" 65.25 | \n",
" 62.34 | \n",
" 61.48 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
"
\n",
"
11 rows × 69 columns
\n",
"
"
],
"text/plain": [
" Country Name Country Code Indicator Name \\\n",
"45 Colombia COL Life expectancy at birth, total (years) \n",
"48 Costa Rica CRI Life expectancy at birth, total (years) \n",
"77 France FRA Life expectancy at birth, total (years) \n",
"100 Haiti HTI Life expectancy at birth, total (years) \n",
"119 Japan JPN Life expectancy at birth, total (years) \n",
"154 Mexico MEX Life expectancy at birth, total (years) \n",
"180 New Zealand NZL Life expectancy at birth, total (years) \n",
"229 Chad TCD Life expectancy at birth, total (years) \n",
"248 Ukraine UKR Life expectancy at birth, total (years) \n",
"251 United States USA Life expectancy at birth, total (years) \n",
"263 South Africa ZAF Life expectancy at birth, total (years) \n",
"\n",
" Indicator Code 1960 1961 1962 1963 1964 1965 ... 2015 2016 \\\n",
"45 SP.DYN.LE00.IN 57.13 57.73 58.30 58.89 59.38 59.81 ... 76.26 76.47 \n",
"48 SP.DYN.LE00.IN 60.41 60.90 61.11 61.62 62.08 62.66 ... 79.09 79.46 \n",
"77 SP.DYN.LE00.IN 69.87 70.12 70.31 70.51 70.66 70.81 ... 82.32 82.57 \n",
"100 SP.DYN.LE00.IN 43.50 43.91 44.29 42.77 44.97 45.35 ... 63.24 63.39 \n",
"119 SP.DYN.LE00.IN 67.70 68.35 68.63 69.71 70.21 70.27 ... 83.79 83.98 \n",
"154 SP.DYN.LE00.IN 55.02 55.83 56.60 57.31 57.95 58.50 ... 74.68 74.41 \n",
"180 SP.DYN.LE00.IN 71.24 70.99 71.23 71.28 71.33 71.23 ... 81.61 81.66 \n",
"229 SP.DYN.LE00.IN 38.37 38.63 38.84 39.07 39.33 39.12 ... 51.59 52.08 \n",
"248 SP.DYN.LE00.IN 69.49 69.57 69.33 69.79 70.21 69.87 ... 71.19 71.48 \n",
"251 SP.DYN.LE00.IN 69.77 70.27 70.12 69.92 70.17 70.21 ... 78.69 78.54 \n",
"263 SP.DYN.LE00.IN 52.67 53.09 53.38 53.63 53.91 54.19 ... 63.95 64.75 \n",
"\n",
" 2017 2018 2019 2020 2021 2022 2023 Unnamed: 68 \n",
"45 76.65 76.75 76.75 74.77 72.83 73.66 NaN NaN \n",
"48 79.38 79.48 79.43 79.28 77.02 77.32 NaN NaN \n",
"77 82.58 82.68 82.83 82.18 82.32 82.23 NaN NaN \n",
"100 63.85 64.02 64.25 64.05 63.19 63.73 NaN NaN \n",
"119 84.10 84.21 84.36 84.56 84.45 84.00 NaN NaN \n",
"154 74.14 74.02 74.20 70.13 70.21 74.83 NaN NaN \n",
"180 81.86 81.71 82.06 82.26 82.21 82.76 NaN NaN \n",
"229 52.31 52.83 53.26 52.78 52.52 53.00 NaN NaN \n",
"248 71.78 71.58 71.83 71.19 69.65 68.59 NaN NaN \n",
"251 78.54 78.64 78.79 76.98 76.33 77.43 NaN NaN \n",
"263 65.40 65.67 66.17 65.25 62.34 61.48 NaN NaN \n",
"\n",
"[11 rows x 69 columns]"
]
},
"execution_count": 216,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Carga de datos de esperanza de vida al nacer por país\n",
"esperanza_vida = pd.read_csv(\n",
" \"https://raw.githubusercontent.com/gf0657-programacionsig/2024-ii/refs/heads/main/datos/world-bank/paises-esperanza-vida.csv\"\n",
")\n",
"\n",
"# Filtrar datos de países\n",
"paises_seleccionados = esperanza_vida[\n",
" esperanza_vida['Country Code'].isin(\n",
" ['CRI', 'TCD', 'COL', 'FRA', 'HTI', 'JPN', 'MEX', 'NZL', 'UKR', 'USA', 'ZAF']\n",
" )\n",
"]\n",
"\n",
"# Despliegue de los datos de los países\n",
"paises_seleccionados"
]
},
{
"cell_type": "code",
"execution_count": 217,
"id": "6c0975f2",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Country Name | \n",
" Country Code | \n",
" 1960 | \n",
" 1961 | \n",
" 1962 | \n",
" 1963 | \n",
" 1964 | \n",
" 1965 | \n",
" 1966 | \n",
" 1967 | \n",
" ... | \n",
" 2014 | \n",
" 2015 | \n",
" 2016 | \n",
" 2017 | \n",
" 2018 | \n",
" 2019 | \n",
" 2020 | \n",
" 2021 | \n",
" 2022 | \n",
" 2023 | \n",
"
\n",
" \n",
" \n",
" \n",
" 45 | \n",
" Colombia | \n",
" COL | \n",
" 57.13 | \n",
" 57.73 | \n",
" 58.30 | \n",
" 58.89 | \n",
" 59.38 | \n",
" 59.81 | \n",
" 60.20 | \n",
" 60.53 | \n",
" ... | \n",
" 76.04 | \n",
" 76.26 | \n",
" 76.47 | \n",
" 76.65 | \n",
" 76.75 | \n",
" 76.75 | \n",
" 74.77 | \n",
" 72.83 | \n",
" 73.66 | \n",
" NaN | \n",
"
\n",
" \n",
" 48 | \n",
" Costa Rica | \n",
" CRI | \n",
" 60.41 | \n",
" 60.90 | \n",
" 61.11 | \n",
" 61.62 | \n",
" 62.08 | \n",
" 62.66 | \n",
" 63.37 | \n",
" 63.92 | \n",
" ... | \n",
" 78.77 | \n",
" 79.09 | \n",
" 79.46 | \n",
" 79.38 | \n",
" 79.48 | \n",
" 79.43 | \n",
" 79.28 | \n",
" 77.02 | \n",
" 77.32 | \n",
" NaN | \n",
"
\n",
" \n",
" 77 | \n",
" France | \n",
" FRA | \n",
" 69.87 | \n",
" 70.12 | \n",
" 70.31 | \n",
" 70.51 | \n",
" 70.66 | \n",
" 70.81 | \n",
" 70.96 | \n",
" 71.16 | \n",
" ... | \n",
" 82.72 | \n",
" 82.32 | \n",
" 82.57 | \n",
" 82.58 | \n",
" 82.68 | \n",
" 82.83 | \n",
" 82.18 | \n",
" 82.32 | \n",
" 82.23 | \n",
" NaN | \n",
"
\n",
" \n",
" 100 | \n",
" Haiti | \n",
" HTI | \n",
" 43.50 | \n",
" 43.91 | \n",
" 44.29 | \n",
" 42.77 | \n",
" 44.97 | \n",
" 45.35 | \n",
" 45.59 | \n",
" 46.12 | \n",
" ... | \n",
" 62.99 | \n",
" 63.24 | \n",
" 63.39 | \n",
" 63.85 | \n",
" 64.02 | \n",
" 64.25 | \n",
" 64.05 | \n",
" 63.19 | \n",
" 63.73 | \n",
" NaN | \n",
"
\n",
" \n",
" 119 | \n",
" Japan | \n",
" JPN | \n",
" 67.70 | \n",
" 68.35 | \n",
" 68.63 | \n",
" 69.71 | \n",
" 70.21 | \n",
" 70.27 | \n",
" 70.92 | \n",
" 71.47 | \n",
" ... | \n",
" 83.59 | \n",
" 83.79 | \n",
" 83.98 | \n",
" 84.10 | \n",
" 84.21 | \n",
" 84.36 | \n",
" 84.56 | \n",
" 84.45 | \n",
" 84.00 | \n",
" NaN | \n",
"
\n",
" \n",
" 154 | \n",
" Mexico | \n",
" MEX | \n",
" 55.02 | \n",
" 55.83 | \n",
" 56.60 | \n",
" 57.31 | \n",
" 57.95 | \n",
" 58.50 | \n",
" 58.98 | \n",
" 59.42 | \n",
" ... | \n",
" 74.80 | \n",
" 74.68 | \n",
" 74.41 | \n",
" 74.14 | \n",
" 74.02 | \n",
" 74.20 | \n",
" 70.13 | \n",
" 70.21 | \n",
" 74.83 | \n",
" NaN | \n",
"
\n",
" \n",
" 180 | \n",
" New Zealand | \n",
" NZL | \n",
" 71.24 | \n",
" 70.99 | \n",
" 71.23 | \n",
" 71.28 | \n",
" 71.33 | \n",
" 71.23 | \n",
" 71.12 | \n",
" 71.47 | \n",
" ... | \n",
" 81.46 | \n",
" 81.61 | \n",
" 81.66 | \n",
" 81.86 | \n",
" 81.71 | \n",
" 82.06 | \n",
" 82.26 | \n",
" 82.21 | \n",
" 82.76 | \n",
" NaN | \n",
"
\n",
" \n",
" 229 | \n",
" Chad | \n",
" TCD | \n",
" 38.37 | \n",
" 38.63 | \n",
" 38.84 | \n",
" 39.07 | \n",
" 39.33 | \n",
" 39.12 | \n",
" 39.15 | \n",
" 39.48 | \n",
" ... | \n",
" 51.20 | \n",
" 51.59 | \n",
" 52.08 | \n",
" 52.31 | \n",
" 52.83 | \n",
" 53.26 | \n",
" 52.78 | \n",
" 52.52 | \n",
" 53.00 | \n",
" NaN | \n",
"
\n",
" \n",
" 248 | \n",
" Ukraine | \n",
" UKR | \n",
" 69.49 | \n",
" 69.57 | \n",
" 69.33 | \n",
" 69.79 | \n",
" 70.21 | \n",
" 69.87 | \n",
" 69.73 | \n",
" 69.56 | \n",
" ... | \n",
" 71.19 | \n",
" 71.19 | \n",
" 71.48 | \n",
" 71.78 | \n",
" 71.58 | \n",
" 71.83 | \n",
" 71.19 | \n",
" 69.65 | \n",
" 68.59 | \n",
" NaN | \n",
"
\n",
" \n",
" 251 | \n",
" United States | \n",
" USA | \n",
" 69.77 | \n",
" 70.27 | \n",
" 70.12 | \n",
" 69.92 | \n",
" 70.17 | \n",
" 70.21 | \n",
" 70.21 | \n",
" 70.56 | \n",
" ... | \n",
" 78.84 | \n",
" 78.69 | \n",
" 78.54 | \n",
" 78.54 | \n",
" 78.64 | \n",
" 78.79 | \n",
" 76.98 | \n",
" 76.33 | \n",
" 77.43 | \n",
" NaN | \n",
"
\n",
" \n",
" 263 | \n",
" South Africa | \n",
" ZAF | \n",
" 52.67 | \n",
" 53.09 | \n",
" 53.38 | \n",
" 53.63 | \n",
" 53.91 | \n",
" 54.19 | \n",
" 54.39 | \n",
" 54.63 | \n",
" ... | \n",
" 63.38 | \n",
" 63.95 | \n",
" 64.75 | \n",
" 65.40 | \n",
" 65.67 | \n",
" 66.17 | \n",
" 65.25 | \n",
" 62.34 | \n",
" 61.48 | \n",
" NaN | \n",
"
\n",
" \n",
"
\n",
"
11 rows × 66 columns
\n",
"
"
],
"text/plain": [
" Country Name Country Code 1960 1961 1962 1963 1964 1965 1966 \\\n",
"45 Colombia COL 57.13 57.73 58.30 58.89 59.38 59.81 60.20 \n",
"48 Costa Rica CRI 60.41 60.90 61.11 61.62 62.08 62.66 63.37 \n",
"77 France FRA 69.87 70.12 70.31 70.51 70.66 70.81 70.96 \n",
"100 Haiti HTI 43.50 43.91 44.29 42.77 44.97 45.35 45.59 \n",
"119 Japan JPN 67.70 68.35 68.63 69.71 70.21 70.27 70.92 \n",
"154 Mexico MEX 55.02 55.83 56.60 57.31 57.95 58.50 58.98 \n",
"180 New Zealand NZL 71.24 70.99 71.23 71.28 71.33 71.23 71.12 \n",
"229 Chad TCD 38.37 38.63 38.84 39.07 39.33 39.12 39.15 \n",
"248 Ukraine UKR 69.49 69.57 69.33 69.79 70.21 69.87 69.73 \n",
"251 United States USA 69.77 70.27 70.12 69.92 70.17 70.21 70.21 \n",
"263 South Africa ZAF 52.67 53.09 53.38 53.63 53.91 54.19 54.39 \n",
"\n",
" 1967 ... 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 \n",
"45 60.53 ... 76.04 76.26 76.47 76.65 76.75 76.75 74.77 72.83 73.66 NaN \n",
"48 63.92 ... 78.77 79.09 79.46 79.38 79.48 79.43 79.28 77.02 77.32 NaN \n",
"77 71.16 ... 82.72 82.32 82.57 82.58 82.68 82.83 82.18 82.32 82.23 NaN \n",
"100 46.12 ... 62.99 63.24 63.39 63.85 64.02 64.25 64.05 63.19 63.73 NaN \n",
"119 71.47 ... 83.59 83.79 83.98 84.10 84.21 84.36 84.56 84.45 84.00 NaN \n",
"154 59.42 ... 74.80 74.68 74.41 74.14 74.02 74.20 70.13 70.21 74.83 NaN \n",
"180 71.47 ... 81.46 81.61 81.66 81.86 81.71 82.06 82.26 82.21 82.76 NaN \n",
"229 39.48 ... 51.20 51.59 52.08 52.31 52.83 53.26 52.78 52.52 53.00 NaN \n",
"248 69.56 ... 71.19 71.19 71.48 71.78 71.58 71.83 71.19 69.65 68.59 NaN \n",
"251 70.56 ... 78.84 78.69 78.54 78.54 78.64 78.79 76.98 76.33 77.43 NaN \n",
"263 54.63 ... 63.38 63.95 64.75 65.40 65.67 66.17 65.25 62.34 61.48 NaN \n",
"\n",
"[11 rows x 66 columns]"
]
},
"execution_count": 217,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Se seleccionan las columnas de años\n",
"anios = esperanza_vida.columns[4:68]\n",
"paises_seleccionados = paises_seleccionados[['Country Name', 'Country Code'] + list(anios)]\n",
"\n",
"paises_seleccionados"
]
},
{
"cell_type": "code",
"execution_count": 218,
"id": "14c5fcaa",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Country Name | \n",
" Country Code | \n",
" Year | \n",
" Life Expectancy | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Colombia | \n",
" COL | \n",
" 1960 | \n",
" 57.13 | \n",
"
\n",
" \n",
" 1 | \n",
" Costa Rica | \n",
" CRI | \n",
" 1960 | \n",
" 60.41 | \n",
"
\n",
" \n",
" 2 | \n",
" France | \n",
" FRA | \n",
" 1960 | \n",
" 69.87 | \n",
"
\n",
" \n",
" 3 | \n",
" Haiti | \n",
" HTI | \n",
" 1960 | \n",
" 43.50 | \n",
"
\n",
" \n",
" 4 | \n",
" Japan | \n",
" JPN | \n",
" 1960 | \n",
" 67.70 | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 699 | \n",
" New Zealand | \n",
" NZL | \n",
" 2023 | \n",
" NaN | \n",
"
\n",
" \n",
" 700 | \n",
" Chad | \n",
" TCD | \n",
" 2023 | \n",
" NaN | \n",
"
\n",
" \n",
" 701 | \n",
" Ukraine | \n",
" UKR | \n",
" 2023 | \n",
" NaN | \n",
"
\n",
" \n",
" 702 | \n",
" United States | \n",
" USA | \n",
" 2023 | \n",
" NaN | \n",
"
\n",
" \n",
" 703 | \n",
" South Africa | \n",
" ZAF | \n",
" 2023 | \n",
" NaN | \n",
"
\n",
" \n",
"
\n",
"
704 rows × 4 columns
\n",
"
"
],
"text/plain": [
" Country Name Country Code Year Life Expectancy\n",
"0 Colombia COL 1960 57.13\n",
"1 Costa Rica CRI 1960 60.41\n",
"2 France FRA 1960 69.87\n",
"3 Haiti HTI 1960 43.50\n",
"4 Japan JPN 1960 67.70\n",
".. ... ... ... ...\n",
"699 New Zealand NZL 2023 NaN\n",
"700 Chad TCD 2023 NaN\n",
"701 Ukraine UKR 2023 NaN\n",
"702 United States USA 2023 NaN\n",
"703 South Africa ZAF 2023 NaN\n",
"\n",
"[704 rows x 4 columns]"
]
},
"execution_count": 218,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Se transforman los datos de formato ancho a formato largo\n",
"datos_largos = paises_seleccionados.melt(\n",
" id_vars=['Country Name', 'Country Code'],\n",
" value_vars=anios,\n",
" var_name='Year',\n",
" value_name='Life Expectancy'\n",
")\n",
"\n",
"datos_largos"
]
},
{
"cell_type": "code",
"execution_count": 219,
"id": "d6fa6a8b",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"customdata": [
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
],
[
"Colombia"
]
],
"hovertemplate": "País=%{customdata[0]}
Año=%{x}
Esperanza de vida al nacer (años)=%{y:.2f}",
"legendgroup": "Colombia",
"line": {
"color": "#636efa",
"dash": "solid"
},
"marker": {
"symbol": "circle"
},
"mode": "lines+markers",
"name": "Colombia",
"orientation": "v",
"showlegend": true,
"type": "scatter",
"x": [
"1960",
"1961",
"1962",
"1963",
"1964",
"1965",
"1966",
"1967",
"1968",
"1969",
"1970",
"1971",
"1972",
"1973",
"1974",
"1975",
"1976",
"1977",
"1978",
"1979",
"1980",
"1981",
"1982",
"1983",
"1984",
"1985",
"1986",
"1987",
"1988",
"1989",
"1990",
"1991",
"1992",
"1993",
"1994",
"1995",
"1996",
"1997",
"1998",
"1999",
"2000",
"2001",
"2002",
"2003",
"2004",
"2005",
"2006",
"2007",
"2008",
"2009",
"2010",
"2011",
"2012",
"2013",
"2014",
"2015",
"2016",
"2017",
"2018",
"2019",
"2020",
"2021",
"2022",
"2023"
],
"xaxis": "x",
"y": [
57.13,
57.73,
58.301,
58.892,
59.375,
59.815,
60.204,
60.531,
60.871,
61.216,
61.569,
62.051,
62.569,
63.06,
63.64,
64.27,
64.845,
65.424,
65.954,
66.38,
66.868,
67.197,
67.531,
67.745,
67.979,
66.276,
68.26,
68.268,
68.391,
68.533,
68.644,
68.813,
69.038,
69.342,
69.613,
69.933,
70.189,
70.492,
70.738,
70.926,
71.32,
71.502,
71.939,
72.361,
72.695,
73.081,
73.468,
73.837,
74.295,
74.742,
75.033,
75.32,
75.597,
75.827,
76.043,
76.257,
76.471,
76.646,
76.748,
76.752,
74.769,
72.83,
73.659,
null
],
"yaxis": "y"
},
{
"customdata": [
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
],
[
"Costa Rica"
]
],
"hovertemplate": "País=%{customdata[0]}
Año=%{x}
Esperanza de vida al nacer (años)=%{y:.2f}",
"legendgroup": "Costa Rica",
"line": {
"color": "#EF553B",
"dash": "solid"
},
"marker": {
"symbol": "circle"
},
"mode": "lines+markers",
"name": "Costa Rica",
"orientation": "v",
"showlegend": true,
"type": "scatter",
"x": [
"1960",
"1961",
"1962",
"1963",
"1964",
"1965",
"1966",
"1967",
"1968",
"1969",
"1970",
"1971",
"1972",
"1973",
"1974",
"1975",
"1976",
"1977",
"1978",
"1979",
"1980",
"1981",
"1982",
"1983",
"1984",
"1985",
"1986",
"1987",
"1988",
"1989",
"1990",
"1991",
"1992",
"1993",
"1994",
"1995",
"1996",
"1997",
"1998",
"1999",
"2000",
"2001",
"2002",
"2003",
"2004",
"2005",
"2006",
"2007",
"2008",
"2009",
"2010",
"2011",
"2012",
"2013",
"2014",
"2015",
"2016",
"2017",
"2018",
"2019",
"2020",
"2021",
"2022",
"2023"
],
"xaxis": "x",
"y": [
60.415,
60.901,
61.111,
61.624,
62.083,
62.663,
63.367,
63.921,
64.428,
65.016,
65.361,
66.301,
66.716,
67.953,
68.454,
69.4,
70.294,
71.288,
72.239,
72.423,
72.899,
73.799,
74.654,
75.079,
75.091,
75.111,
75.505,
75.741,
76.106,
76.263,
76.63,
76.591,
76.627,
76.703,
76.67,
76.552,
76.804,
77.07,
76.896,
77.138,
77.586,
77.545,
77.983,
78.072,
78.331,
78.505,
78.514,
78.462,
78.445,
78.672,
78.67,
79.348,
79.283,
79.403,
78.774,
79.086,
79.459,
79.377,
79.484,
79.427,
79.277,
77.023,
77.32,
null
],
"yaxis": "y"
},
{
"customdata": [
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
],
[
"France"
]
],
"hovertemplate": "País=%{customdata[0]}
Año=%{x}
Esperanza de vida al nacer (años)=%{y:.2f}",
"legendgroup": "France",
"line": {
"color": "#00cc96",
"dash": "solid"
},
"marker": {
"symbol": "circle"
},
"mode": "lines+markers",
"name": "France",
"orientation": "v",
"showlegend": true,
"type": "scatter",
"x": [
"1960",
"1961",
"1962",
"1963",
"1964",
"1965",
"1966",
"1967",
"1968",
"1969",
"1970",
"1971",
"1972",
"1973",
"1974",
"1975",
"1976",
"1977",
"1978",
"1979",
"1980",
"1981",
"1982",
"1983",
"1984",
"1985",
"1986",
"1987",
"1988",
"1989",
"1990",
"1991",
"1992",
"1993",
"1994",
"1995",
"1996",
"1997",
"1998",
"1999",
"2000",
"2001",
"2002",
"2003",
"2004",
"2005",
"2006",
"2007",
"2008",
"2009",
"2010",
"2011",
"2012",
"2013",
"2014",
"2015",
"2016",
"2017",
"2018",
"2019",
"2020",
"2021",
"2022",
"2023"
],
"xaxis": "x",
"y": [
69.8682926829268,
70.1170731707317,
70.3146341463415,
70.5146341463415,
70.6634146341463,
70.8121951219512,
70.9609756097561,
71.1609756097561,
71.309756097561,
71.4585365853659,
71.6585365853659,
71.9073170731707,
72.1073170731707,
72.3560975609756,
72.6048780487805,
72.8536585365854,
73.1024390243903,
73.3512195121951,
73.6024390243903,
73.8512195121951,
74.0512195121951,
74.3,
74.5,
74.8,
75,
75.3,
75.6,
75.8,
76.1,
76.3487804878049,
76.6,
76.8487804878049,
77.1,
77.3,
77.6487804878049,
77.7512195121951,
77.9536585365854,
78.3048780487805,
78.6048780487805,
78.7560975609756,
79.0560975609756,
79.1585365853659,
79.2609756097561,
79.1146341463415,
80.1634146341463,
80.1634146341463,
80.8121951219512,
81.1121951219512,
81.2146341463415,
81.4146341463415,
81.6634146341463,
82.1146341463415,
81.9682926829268,
82.219512195122,
82.719512195122,
82.3219512195122,
82.5731707317073,
82.5756097560976,
82.6756097560976,
82.8268292682927,
82.1756097560976,
82.3243902439024,
82.2292682926829,
null
],
"yaxis": "y"
},
{
"customdata": [
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
],
[
"Haiti"
]
],
"hovertemplate": "País=%{customdata[0]}
Año=%{x}
Esperanza de vida al nacer (años)=%{y:.2f}",
"legendgroup": "Haiti",
"line": {
"color": "#ab63fa",
"dash": "solid"
},
"marker": {
"symbol": "circle"
},
"mode": "lines+markers",
"name": "Haiti",
"orientation": "v",
"showlegend": true,
"type": "scatter",
"x": [
"1960",
"1961",
"1962",
"1963",
"1964",
"1965",
"1966",
"1967",
"1968",
"1969",
"1970",
"1971",
"1972",
"1973",
"1974",
"1975",
"1976",
"1977",
"1978",
"1979",
"1980",
"1981",
"1982",
"1983",
"1984",
"1985",
"1986",
"1987",
"1988",
"1989",
"1990",
"1991",
"1992",
"1993",
"1994",
"1995",
"1996",
"1997",
"1998",
"1999",
"2000",
"2001",
"2002",
"2003",
"2004",
"2005",
"2006",
"2007",
"2008",
"2009",
"2010",
"2011",
"2012",
"2013",
"2014",
"2015",
"2016",
"2017",
"2018",
"2019",
"2020",
"2021",
"2022",
"2023"
],
"xaxis": "x",
"y": [
43.499,
43.914,
44.291,
42.769,
44.968,
45.351,
45.594,
46.125,
46.512,
46.915,
47.337,
47.719,
47.945,
48.208,
48.419,
48.681,
48.956,
49.209,
49.428,
49.675,
49.902,
50.268,
50.551,
50.871,
51.123,
51.523,
51.875,
52.239,
52.632,
52.755,
52.975,
53.391,
53.842,
54.35,
54.653,
55.547,
56.154,
56.516,
57.135,
57.961,
58.365,
58.489,
58.855,
59.507,
58.475,
60.415,
60.763,
61.103,
61.332,
61.741,
46.019,
61.623,
62.291,
62.605,
62.987,
63.237,
63.392,
63.854,
64.019,
64.255,
64.052,
63.192,
63.728,
null
],
"yaxis": "y"
},
{
"customdata": [
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
],
[
"Japan"
]
],
"hovertemplate": "País=%{customdata[0]}
Año=%{x}
Esperanza de vida al nacer (años)=%{y:.2f}",
"legendgroup": "Japan",
"line": {
"color": "#FFA15A",
"dash": "solid"
},
"marker": {
"symbol": "circle"
},
"mode": "lines+markers",
"name": "Japan",
"orientation": "v",
"showlegend": true,
"type": "scatter",
"x": [
"1960",
"1961",
"1962",
"1963",
"1964",
"1965",
"1966",
"1967",
"1968",
"1969",
"1970",
"1971",
"1972",
"1973",
"1974",
"1975",
"1976",
"1977",
"1978",
"1979",
"1980",
"1981",
"1982",
"1983",
"1984",
"1985",
"1986",
"1987",
"1988",
"1989",
"1990",
"1991",
"1992",
"1993",
"1994",
"1995",
"1996",
"1997",
"1998",
"1999",
"2000",
"2001",
"2002",
"2003",
"2004",
"2005",
"2006",
"2007",
"2008",
"2009",
"2010",
"2011",
"2012",
"2013",
"2014",
"2015",
"2016",
"2017",
"2018",
"2019",
"2020",
"2021",
"2022",
"2023"
],
"xaxis": "x",
"y": [
67.6956097560976,
68.3519512195122,
68.6348780487805,
69.7124390243902,
70.2065853658537,
70.2668292682927,
70.9158536585366,
71.4660975609756,
71.6109756097561,
71.8580487804878,
71.919756097561,
72.8090243902439,
73.1536585365854,
73.2951219512195,
73.6721951219512,
74.2470731707317,
74.6865853658537,
75.2558536585366,
75.5846341463415,
76.1087804878049,
75.9890243902439,
76.3948780487805,
76.8736585365854,
76.9219512195122,
77.2912195121951,
77.5604878048781,
78.0104878048781,
78.429512195122,
78.349756097561,
78.7685365853659,
78.8370731707317,
79.0368292682927,
79.080243902439,
79.3036585365854,
79.6968292682927,
79.5360975609756,
80.219756097561,
80.4241463414634,
80.5014634146342,
80.4609756097561,
81.0760975609756,
81.4163414634146,
81.6907317073171,
81.76,
82.030243902439,
81.9551219512195,
82.3219512195122,
82.5070731707317,
82.5875609756098,
82.9314634146342,
82.8426829268293,
82.5912195121951,
83.0960975609756,
83.3319512195122,
83.5878048780488,
83.7939024390244,
83.9848780487805,
84.099756097561,
84.2109756097561,
84.3563414634146,
84.56,
84.4456097560976,
83.9963414634146,
null
],
"yaxis": "y"
},
{
"customdata": [
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
],
[
"Mexico"
]
],
"hovertemplate": "País=%{customdata[0]}
Año=%{x}
Esperanza de vida al nacer (años)=%{y:.2f}",
"legendgroup": "Mexico",
"line": {
"color": "#19d3f3",
"dash": "solid"
},
"marker": {
"symbol": "circle"
},
"mode": "lines+markers",
"name": "Mexico",
"orientation": "v",
"showlegend": true,
"type": "scatter",
"x": [
"1960",
"1961",
"1962",
"1963",
"1964",
"1965",
"1966",
"1967",
"1968",
"1969",
"1970",
"1971",
"1972",
"1973",
"1974",
"1975",
"1976",
"1977",
"1978",
"1979",
"1980",
"1981",
"1982",
"1983",
"1984",
"1985",
"1986",
"1987",
"1988",
"1989",
"1990",
"1991",
"1992",
"1993",
"1994",
"1995",
"1996",
"1997",
"1998",
"1999",
"2000",
"2001",
"2002",
"2003",
"2004",
"2005",
"2006",
"2007",
"2008",
"2009",
"2010",
"2011",
"2012",
"2013",
"2014",
"2015",
"2016",
"2017",
"2018",
"2019",
"2020",
"2021",
"2022",
"2023"
],
"xaxis": "x",
"y": [
55.017,
55.831,
56.599,
57.31,
57.947,
58.499,
58.981,
59.42,
59.835,
60.25,
60.69,
61.155,
61.614,
62.055,
62.459,
62.81,
63.123,
63.434,
63.778,
64.189,
64.703,
65.313,
65.975,
66.644,
67.29,
67.874,
68.388,
68.844,
69.253,
69.623,
69.971,
70.298,
70.616,
70.942,
71.279,
71.646,
72.037,
72.445,
72.845,
73.225,
73.569,
73.867,
74.106,
74.274,
74.372,
74.376,
74.331,
74.238,
74.152,
74.119,
74.19,
74.365,
74.574,
74.739,
74.797,
74.682,
74.412,
74.138,
74.015,
74.202,
70.133,
70.213,
74.832,
null
],
"yaxis": "y"
},
{
"customdata": [
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
],
[
"New Zealand"
]
],
"hovertemplate": "País=%{customdata[0]}
Año=%{x}
Esperanza de vida al nacer (años)=%{y:.2f}",
"legendgroup": "New Zealand",
"line": {
"color": "#FF6692",
"dash": "solid"
},
"marker": {
"symbol": "circle"
},
"mode": "lines+markers",
"name": "New Zealand",
"orientation": "v",
"showlegend": true,
"type": "scatter",
"x": [
"1960",
"1961",
"1962",
"1963",
"1964",
"1965",
"1966",
"1967",
"1968",
"1969",
"1970",
"1971",
"1972",
"1973",
"1974",
"1975",
"1976",
"1977",
"1978",
"1979",
"1980",
"1981",
"1982",
"1983",
"1984",
"1985",
"1986",
"1987",
"1988",
"1989",
"1990",
"1991",
"1992",
"1993",
"1994",
"1995",
"1996",
"1997",
"1998",
"1999",
"2000",
"2001",
"2002",
"2003",
"2004",
"2005",
"2006",
"2007",
"2008",
"2009",
"2010",
"2011",
"2012",
"2013",
"2014",
"2015",
"2016",
"2017",
"2018",
"2019",
"2020",
"2021",
"2022",
"2023"
],
"xaxis": "x",
"y": [
71.2365853658537,
70.9853658536585,
71.2317073170732,
71.280487804878,
71.3292682926829,
71.2268292682927,
71.1243902439025,
71.4731707317073,
71.1243902439025,
71.4731707317073,
71.2731707317073,
71.7731707317073,
71.8292682926829,
71.6682926829268,
71.9243902439024,
72.219512195122,
72.4219512195122,
72.1682926829268,
73.019512195122,
73.0682926829269,
72.8292682926829,
73.6219512195122,
73.7243902439024,
73.7756097560976,
74.3707317073171,
73.8292682926829,
74.1219512195122,
74.1780487804878,
74.4243902439024,
74.8243902439024,
75.3780487804878,
76.0317073170732,
76.1243902439025,
76.4341463414634,
76.8829268292683,
76.7341463414634,
76.7878048780488,
77.3341463414634,
78.0853658536585,
77.890243902439,
78.6365853658537,
78.6926829268293,
78.8463414634146,
79.1463414634146,
79.5487804878049,
79.8512195121951,
80.0487804878049,
80.1512195121951,
80.3512195121951,
80.7024390243902,
80.9048780487805,
81.1560975609756,
81.4073170731707,
81.4048780487805,
81.4568292682927,
81.6124390243903,
81.6585365853659,
81.8585365853659,
81.7073170731707,
82.0560975609756,
82.2560975609756,
82.2073170731707,
82.7609756097561,
null
],
"yaxis": "y"
},
{
"customdata": [
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
],
[
"Chad"
]
],
"hovertemplate": "País=%{customdata[0]}
Año=%{x}
Esperanza de vida al nacer (años)=%{y:.2f}",
"legendgroup": "Chad",
"line": {
"color": "#B6E880",
"dash": "solid"
},
"marker": {
"symbol": "circle"
},
"mode": "lines+markers",
"name": "Chad",
"orientation": "v",
"showlegend": true,
"type": "scatter",
"x": [
"1960",
"1961",
"1962",
"1963",
"1964",
"1965",
"1966",
"1967",
"1968",
"1969",
"1970",
"1971",
"1972",
"1973",
"1974",
"1975",
"1976",
"1977",
"1978",
"1979",
"1980",
"1981",
"1982",
"1983",
"1984",
"1985",
"1986",
"1987",
"1988",
"1989",
"1990",
"1991",
"1992",
"1993",
"1994",
"1995",
"1996",
"1997",
"1998",
"1999",
"2000",
"2001",
"2002",
"2003",
"2004",
"2005",
"2006",
"2007",
"2008",
"2009",
"2010",
"2011",
"2012",
"2013",
"2014",
"2015",
"2016",
"2017",
"2018",
"2019",
"2020",
"2021",
"2022",
"2023"
],
"xaxis": "x",
"y": [
38.374,
38.631,
38.835,
39.072,
39.333,
39.12,
39.15,
39.479,
39.903,
40.265,
40.603,
40.19,
41.399,
41.78,
41.785,
41.872,
42.036,
42.255,
42.504,
42.81,
43.06,
43.312,
43.73,
44.179,
44.515,
45.05,
45.21,
42.798,
45.785,
45.875,
44.686,
45.941,
46.102,
46.318,
46.449,
46.711,
47.013,
47.217,
47.224,
47.372,
47.443,
47.85,
47.946,
48.206,
48.232,
48.312,
48.286,
48.751,
49.024,
49.196,
49.584,
50.009,
50.54,
50.781,
51.202,
51.589,
52.083,
52.308,
52.825,
53.259,
52.777,
52.525,
52.997,
null
],
"yaxis": "y"
},
{
"customdata": [
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
],
[
"Ukraine"
]
],
"hovertemplate": "País=%{customdata[0]}
Año=%{x}
Esperanza de vida al nacer (años)=%{y:.2f}",
"legendgroup": "Ukraine",
"line": {
"color": "#FF97FF",
"dash": "solid"
},
"marker": {
"symbol": "circle"
},
"mode": "lines+markers",
"name": "Ukraine",
"orientation": "v",
"showlegend": true,
"type": "scatter",
"x": [
"1960",
"1961",
"1962",
"1963",
"1964",
"1965",
"1966",
"1967",
"1968",
"1969",
"1970",
"1971",
"1972",
"1973",
"1974",
"1975",
"1976",
"1977",
"1978",
"1979",
"1980",
"1981",
"1982",
"1983",
"1984",
"1985",
"1986",
"1987",
"1988",
"1989",
"1990",
"1991",
"1992",
"1993",
"1994",
"1995",
"1996",
"1997",
"1998",
"1999",
"2000",
"2001",
"2002",
"2003",
"2004",
"2005",
"2006",
"2007",
"2008",
"2009",
"2010",
"2011",
"2012",
"2013",
"2014",
"2015",
"2016",
"2017",
"2018",
"2019",
"2020",
"2021",
"2022",
"2023"
],
"xaxis": "x",
"y": [
69.4850487804878,
69.5723658536586,
69.3345853658537,
69.7874878048781,
70.205512195122,
69.8683902439025,
69.7309512195122,
69.5615365853659,
69.5547804878049,
68.9756097560976,
68.8089756097561,
69.037756097561,
68.9172926829269,
69.0430975609756,
68.9340243902439,
69.0463170731707,
69.0428536585366,
68.8185609756098,
68.7599756097561,
68.4696829268293,
68.3301707317073,
68.5981951219512,
68.8756829268293,
68.8463902439025,
68.5058048780488,
68.6131219512195,
70.0662926829268,
70.4951219512195,
70.4975609756098,
70.519512195122,
70.0975609756098,
69.2980487804878,
68.7709756097561,
68.1307317073171,
67.5412195121951,
66.7419512195122,
67.0224390243903,
67.5763414634146,
68.3748780487805,
67.9809756097561,
67.6756097560976,
67.8370731707317,
68.2756097560976,
68.2107317073171,
68.1853658536585,
67.9568292682927,
68.0775609756098,
68.2221951219512,
68.2514634146342,
69.19,
70.2653658536586,
70.8092682926829,
70.9441463414634,
71.159512195122,
71.1865853658537,
71.189512195122,
71.4763414634146,
71.7809756097561,
71.5826829268293,
71.8273170731707,
71.1851219512195,
69.6478048780488,
68.5879268292683,
null
],
"yaxis": "y"
},
{
"customdata": [
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
],
[
"United States"
]
],
"hovertemplate": "País=%{customdata[0]}
Año=%{x}
Esperanza de vida al nacer (años)=%{y:.2f}",
"legendgroup": "United States",
"line": {
"color": "#FECB52",
"dash": "solid"
},
"marker": {
"symbol": "circle"
},
"mode": "lines+markers",
"name": "United States",
"orientation": "v",
"showlegend": true,
"type": "scatter",
"x": [
"1960",
"1961",
"1962",
"1963",
"1964",
"1965",
"1966",
"1967",
"1968",
"1969",
"1970",
"1971",
"1972",
"1973",
"1974",
"1975",
"1976",
"1977",
"1978",
"1979",
"1980",
"1981",
"1982",
"1983",
"1984",
"1985",
"1986",
"1987",
"1988",
"1989",
"1990",
"1991",
"1992",
"1993",
"1994",
"1995",
"1996",
"1997",
"1998",
"1999",
"2000",
"2001",
"2002",
"2003",
"2004",
"2005",
"2006",
"2007",
"2008",
"2009",
"2010",
"2011",
"2012",
"2013",
"2014",
"2015",
"2016",
"2017",
"2018",
"2019",
"2020",
"2021",
"2022",
"2023"
],
"xaxis": "x",
"y": [
69.7707317073171,
70.2707317073171,
70.119512195122,
69.9170731707317,
70.1658536585366,
70.2146341463415,
70.2121951219512,
70.5609756097561,
69.9512195121951,
70.5073170731708,
70.8073170731707,
71.1073170731707,
71.1560975609756,
71.3560975609756,
71.9560975609756,
72.6048780487805,
72.8560975609756,
73.2560975609756,
73.3560975609756,
73.8048780487805,
73.609756097561,
74.009756097561,
74.3609756097561,
74.4634146341463,
74.5634146341464,
74.5634146341464,
74.6146341463415,
74.7658536585366,
74.7658536585366,
75.0170731707317,
75.2146341463415,
75.3658536585366,
75.6170731707317,
75.419512195122,
75.619512195122,
75.6219512195122,
76.0268292682927,
76.4292682926829,
76.5804878048781,
76.5829268292683,
76.6365853658537,
76.8365853658537,
76.9365853658537,
77.0365853658537,
77.4878048780488,
77.4878048780488,
77.6878048780488,
77.9878048780488,
78.0390243902439,
78.390243902439,
78.5414634146342,
78.6414634146341,
78.7414634146342,
78.7414634146342,
78.8414634146341,
78.690243902439,
78.5390243902439,
78.5390243902439,
78.6390243902439,
78.7878048780488,
76.9804878048781,
76.3292682926829,
77.4341463414634,
null
],
"yaxis": "y"
},
{
"customdata": [
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
],
[
"South Africa"
]
],
"hovertemplate": "País=%{customdata[0]}
Año=%{x}
Esperanza de vida al nacer (años)=%{y:.2f}",
"legendgroup": "South Africa",
"line": {
"color": "#636efa",
"dash": "solid"
},
"marker": {
"symbol": "circle"
},
"mode": "lines+markers",
"name": "South Africa",
"orientation": "v",
"showlegend": true,
"type": "scatter",
"x": [
"1960",
"1961",
"1962",
"1963",
"1964",
"1965",
"1966",
"1967",
"1968",
"1969",
"1970",
"1971",
"1972",
"1973",
"1974",
"1975",
"1976",
"1977",
"1978",
"1979",
"1980",
"1981",
"1982",
"1983",
"1984",
"1985",
"1986",
"1987",
"1988",
"1989",
"1990",
"1991",
"1992",
"1993",
"1994",
"1995",
"1996",
"1997",
"1998",
"1999",
"2000",
"2001",
"2002",
"2003",
"2004",
"2005",
"2006",
"2007",
"2008",
"2009",
"2010",
"2011",
"2012",
"2013",
"2014",
"2015",
"2016",
"2017",
"2018",
"2019",
"2020",
"2021",
"2022",
"2023"
],
"xaxis": "x",
"y": [
52.669,
53.085,
53.376,
53.633,
53.906,
54.192,
54.391,
54.626,
54.876,
55.194,
55.579,
55.955,
56.272,
56.722,
57.117,
57.512,
58.006,
58.492,
58.979,
59.395,
59.813,
60.364,
60.933,
61.465,
61.907,
62.38,
62.912,
63.041,
63.345,
63.31,
63.375,
63.265,
63.341,
63.045,
62.612,
62.262,
61.459,
60.805,
60.001,
59.241,
58.47,
57.261,
55.666,
54.331,
54.043,
53.98,
54.278,
54.992,
56.022,
57.447,
58.899,
60.651,
61.846,
62.533,
63.38,
63.95,
64.747,
65.402,
65.674,
66.175,
65.252,
62.341,
61.48,
null
],
"yaxis": "y"
}
],
"layout": {
"legend": {
"title": {
"text": "País"
},
"tracegroupgap": 0
},
"template": {
"data": {
"bar": [
{
"error_x": {
"color": "#2a3f5f"
},
"error_y": {
"color": "#2a3f5f"
},
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "bar"
}
],
"barpolar": [
{
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "barpolar"
}
],
"carpet": [
{
"aaxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"baxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"type": "carpet"
}
],
"choropleth": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "choropleth"
}
],
"contour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "contour"
}
],
"contourcarpet": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "contourcarpet"
}
],
"heatmap": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmap"
}
],
"heatmapgl": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmapgl"
}
],
"histogram": [
{
"marker": {
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "histogram"
}
],
"histogram2d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2d"
}
],
"histogram2dcontour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2dcontour"
}
],
"mesh3d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "mesh3d"
}
],
"parcoords": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "parcoords"
}
],
"pie": [
{
"automargin": true,
"type": "pie"
}
],
"scatter": [
{
"fillpattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
},
"type": "scatter"
}
],
"scatter3d": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter3d"
}
],
"scattercarpet": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattercarpet"
}
],
"scattergeo": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergeo"
}
],
"scattergl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergl"
}
],
"scattermapbox": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattermapbox"
}
],
"scatterpolar": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolar"
}
],
"scatterpolargl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolargl"
}
],
"scatterternary": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterternary"
}
],
"surface": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "surface"
}
],
"table": [
{
"cells": {
"fill": {
"color": "#EBF0F8"
},
"line": {
"color": "white"
}
},
"header": {
"fill": {
"color": "#C8D4E3"
},
"line": {
"color": "white"
}
},
"type": "table"
}
]
},
"layout": {
"annotationdefaults": {
"arrowcolor": "#2a3f5f",
"arrowhead": 0,
"arrowwidth": 1
},
"autotypenumbers": "strict",
"coloraxis": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"colorscale": {
"diverging": [
[
0,
"#8e0152"
],
[
0.1,
"#c51b7d"
],
[
0.2,
"#de77ae"
],
[
0.3,
"#f1b6da"
],
[
0.4,
"#fde0ef"
],
[
0.5,
"#f7f7f7"
],
[
0.6,
"#e6f5d0"
],
[
0.7,
"#b8e186"
],
[
0.8,
"#7fbc41"
],
[
0.9,
"#4d9221"
],
[
1,
"#276419"
]
],
"sequential": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"sequentialminus": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
]
},
"colorway": [
"#636efa",
"#EF553B",
"#00cc96",
"#ab63fa",
"#FFA15A",
"#19d3f3",
"#FF6692",
"#B6E880",
"#FF97FF",
"#FECB52"
],
"font": {
"color": "#2a3f5f"
},
"geo": {
"bgcolor": "white",
"lakecolor": "white",
"landcolor": "#E5ECF6",
"showlakes": true,
"showland": true,
"subunitcolor": "white"
},
"hoverlabel": {
"align": "left"
},
"hovermode": "closest",
"mapbox": {
"style": "light"
},
"paper_bgcolor": "white",
"plot_bgcolor": "#E5ECF6",
"polar": {
"angularaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"radialaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"scene": {
"xaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"yaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"zaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
}
},
"shapedefaults": {
"line": {
"color": "#2a3f5f"
}
},
"ternary": {
"aaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"baxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"caxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"title": {
"x": 0.05
},
"xaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
},
"yaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
}
}
},
"title": {
"text": "Evolución en el tiempo de la esperanza de vida al nacer"
},
"xaxis": {
"anchor": "y",
"domain": [
0,
1
],
"gridcolor": "lightgray",
"gridwidth": 0.5,
"showgrid": true,
"title": {
"text": "Año"
}
},
"yaxis": {
"anchor": "x",
"domain": [
0,
1
],
"gridcolor": "lightgray",
"gridwidth": 0.5,
"showgrid": true,
"title": {
"text": "Esperanza de vida al nacer (años)"
}
}
}
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Creación del gráfico de líneas\n",
"fig = px.line(\n",
" datos_largos,\n",
" x='Year',\n",
" y='Life Expectancy',\n",
" color='Country Name',\n",
" markers=True,\n",
" title='Evolución en el tiempo de la esperanza de vida al nacer', \n",
" labels={\n",
" 'Year': 'Año',\n",
" 'Life Expectancy': 'Esperanza de vida al nacer (años)',\n",
" 'Country Name': 'País'\n",
" },\n",
" hover_data={\n",
" 'Country Name': True, # para mostrar la columna NAME\n",
" 'Year': True, # formato con dos decimales y separador de miles\n",
" 'Life Expectancy': ':.2f', # formato con dos decimales\n",
" } \n",
")\n",
"\n",
"# Atributos globales de la figura\n",
"fig.update_layout(\n",
" legend_title_text='País',\n",
" xaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray'),\n",
" yaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray') \n",
")\n",
"\n",
"# Despliegue del gráfico\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"id": "d2a9e582",
"metadata": {},
"source": [
"#### Ejercicios"
]
},
{
"cell_type": "markdown",
"id": "9517eb09",
"metadata": {},
"source": [
"1. Agregue más países al gráfico de evolución en el tiempo de la esperanza de vida al nacer.\n",
"2. Elabore un gráfico similar para el indicador de mortalidad infantil. Utilice datos de varios países.\n",
"3. Modifique el gráfico de la esperanza de vida para incluir todos los países de [África subsahariana](https://es.wikipedia.org/wiki/%C3%81frica_subsahariana), sin listar uno por uno todos los países. "
]
},
{
"cell_type": "markdown",
"id": "cf2672d2",
"metadata": {},
"source": [
"### Gráficos de barras"
]
},
{
"cell_type": "markdown",
"id": "e763b260",
"metadata": {},
"source": [
"Un [gráfico de barras](https://es.wikipedia.org/wiki/Diagrama_de_barras) se compone de barras rectangulares con longitud proporcional a estadísticas (ej. frecuencias, promedios, mínimos, máximos) asociadas a una variable categórica o discreta. Las barras pueden ser horizontales o verticales y se recomienda que estén ordenadas según su longitud, a menos que exista un orden inherente a la variable (ej. el orden de los días de la semana). Es uno de los tipos de gráficos estadísticos más antiguos y comunes y tiene la ventaja de ser muy fácil de comprender.\n",
"\n",
"En `plotly.express`, los gráficos de barras se generan mediante el método [`plotly.express.bar()`](https://plotly.github.io/plotly.py-docs/generated/plotly.express.bar.html). Se recomiendar leer el [tutorial](https://plotly.com/python/bar-charts/)."
]
},
{
"cell_type": "markdown",
"id": "71408518",
"metadata": {},
"source": [
"#### Ejemplos"
]
},
{
"cell_type": "markdown",
"id": "f4648ce9",
"metadata": {},
"source": [
"##### Suma de población por continente"
]
},
{
"cell_type": "markdown",
"id": "cc11ed90",
"metadata": {},
"source": [
"Gráfico de barras verticales:"
]
},
{
"cell_type": "code",
"execution_count": 220,
"id": "7a9dc79c",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"alignmentgroup": "True",
"hovertemplate": "Continente=%{x}
Población (habitantes)=%{y}",
"legendgroup": "",
"marker": {
"color": "#636efa",
"pattern": {
"shape": ""
}
},
"name": "",
"offsetgroup": "",
"orientation": "v",
"showlegend": false,
"textposition": "auto",
"type": "bar",
"x": [
"Asia",
"Africa",
"Europe",
"North America",
"South America",
"Oceania",
"Seven seas (open ocean)",
"Antarctica"
],
"xaxis": "x",
"y": [
4562304603,
1307986092.3,
747708092,
584776221,
427063263,
41589500,
1894289,
4490
],
"yaxis": "y"
}
],
"layout": {
"barmode": "relative",
"height": 600,
"legend": {
"tracegroupgap": 0
},
"template": {
"data": {
"bar": [
{
"error_x": {
"color": "#2a3f5f"
},
"error_y": {
"color": "#2a3f5f"
},
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "bar"
}
],
"barpolar": [
{
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "barpolar"
}
],
"carpet": [
{
"aaxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"baxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"type": "carpet"
}
],
"choropleth": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "choropleth"
}
],
"contour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "contour"
}
],
"contourcarpet": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "contourcarpet"
}
],
"heatmap": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmap"
}
],
"heatmapgl": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmapgl"
}
],
"histogram": [
{
"marker": {
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "histogram"
}
],
"histogram2d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2d"
}
],
"histogram2dcontour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2dcontour"
}
],
"mesh3d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "mesh3d"
}
],
"parcoords": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "parcoords"
}
],
"pie": [
{
"automargin": true,
"type": "pie"
}
],
"scatter": [
{
"fillpattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
},
"type": "scatter"
}
],
"scatter3d": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter3d"
}
],
"scattercarpet": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattercarpet"
}
],
"scattergeo": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergeo"
}
],
"scattergl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergl"
}
],
"scattermapbox": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattermapbox"
}
],
"scatterpolar": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolar"
}
],
"scatterpolargl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolargl"
}
],
"scatterternary": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterternary"
}
],
"surface": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "surface"
}
],
"table": [
{
"cells": {
"fill": {
"color": "#EBF0F8"
},
"line": {
"color": "white"
}
},
"header": {
"fill": {
"color": "#C8D4E3"
},
"line": {
"color": "white"
}
},
"type": "table"
}
]
},
"layout": {
"annotationdefaults": {
"arrowcolor": "#2a3f5f",
"arrowhead": 0,
"arrowwidth": 1
},
"autotypenumbers": "strict",
"coloraxis": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"colorscale": {
"diverging": [
[
0,
"#8e0152"
],
[
0.1,
"#c51b7d"
],
[
0.2,
"#de77ae"
],
[
0.3,
"#f1b6da"
],
[
0.4,
"#fde0ef"
],
[
0.5,
"#f7f7f7"
],
[
0.6,
"#e6f5d0"
],
[
0.7,
"#b8e186"
],
[
0.8,
"#7fbc41"
],
[
0.9,
"#4d9221"
],
[
1,
"#276419"
]
],
"sequential": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"sequentialminus": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
]
},
"colorway": [
"#636efa",
"#EF553B",
"#00cc96",
"#ab63fa",
"#FFA15A",
"#19d3f3",
"#FF6692",
"#B6E880",
"#FF97FF",
"#FECB52"
],
"font": {
"color": "#2a3f5f"
},
"geo": {
"bgcolor": "white",
"lakecolor": "white",
"landcolor": "#E5ECF6",
"showlakes": true,
"showland": true,
"subunitcolor": "white"
},
"hoverlabel": {
"align": "left"
},
"hovermode": "closest",
"mapbox": {
"style": "light"
},
"paper_bgcolor": "white",
"plot_bgcolor": "#E5ECF6",
"polar": {
"angularaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"radialaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"scene": {
"xaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"yaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"zaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
}
},
"shapedefaults": {
"line": {
"color": "#2a3f5f"
}
},
"ternary": {
"aaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"baxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"caxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"title": {
"x": 0.05
},
"xaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
},
"yaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
}
}
},
"title": {
"font": {
"size": 20
},
"text": "Suma de población por continente",
"x": 0.5
},
"width": 1000,
"xaxis": {
"anchor": "y",
"domain": [
0,
1
],
"title": {
"font": {
"size": 16
},
"text": "Continente"
}
},
"yaxis": {
"anchor": "x",
"domain": [
0,
1
],
"tickformat": ",d",
"title": {
"font": {
"size": 16
},
"text": "Población (habitantes)"
}
}
}
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Cálculo de la suma de población por continente\n",
"poblacion_continente_suma = paises.groupby('CONTINENT')['POP_EST'].sum().sort_values(ascending=False).reset_index()\n",
"\n",
"# Creación del gráfico de barras verticales\n",
"fig = px.bar(\n",
" poblacion_continente_suma,\n",
" x='CONTINENT',\n",
" y='POP_EST',\n",
" title='Suma de población por continente',\n",
" labels={\n",
" 'CONTINENT': 'Continente',\n",
" 'POP_EST': 'Población (habitantes)'\n",
" },\n",
" width=1000, # Ancho de la figura en píxeles\n",
" height=600 # Alto de la figura en píxeles\n",
")\n",
"\n",
"# Actualizar el formato del eje y evitar notación científica\n",
"fig.update_yaxes(tickformat=\",d\")\n",
"\n",
"# Atributos globales de la figura\n",
"fig.update_layout(\n",
" title=dict(\n",
" x=0.5, # Centrar el título\n",
" font=dict(size=20)\n",
" ),\n",
" xaxis_title=dict(\n",
" font=dict(size=16)\n",
" ),\n",
" yaxis_title=dict(\n",
" font=dict(size=16)\n",
" )\n",
")\n",
"\n",
"# Despliegue del gráfico\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"id": "4a13fee8",
"metadata": {},
"source": [
"Gráfico de barras horizontales (debe utilizarse el argumento `orientation=h` en `px.bar()`):"
]
},
{
"cell_type": "code",
"execution_count": 221,
"id": "09fce6c0",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"alignmentgroup": "True",
"hovertemplate": "Población (habitantes)=%{x}
Continente=%{y}",
"legendgroup": "",
"marker": {
"color": "#636efa",
"pattern": {
"shape": ""
}
},
"name": "",
"offsetgroup": "",
"orientation": "h",
"showlegend": false,
"textposition": "auto",
"type": "bar",
"x": [
4490,
1894289,
41589500,
427063263,
584776221,
747708092,
1307986092.3,
4562304603
],
"xaxis": "x",
"y": [
"Antarctica",
"Seven seas (open ocean)",
"Oceania",
"South America",
"North America",
"Europe",
"Africa",
"Asia"
],
"yaxis": "y"
}
],
"layout": {
"barmode": "relative",
"height": 600,
"legend": {
"tracegroupgap": 0
},
"template": {
"data": {
"bar": [
{
"error_x": {
"color": "#2a3f5f"
},
"error_y": {
"color": "#2a3f5f"
},
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "bar"
}
],
"barpolar": [
{
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "barpolar"
}
],
"carpet": [
{
"aaxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"baxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"type": "carpet"
}
],
"choropleth": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "choropleth"
}
],
"contour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "contour"
}
],
"contourcarpet": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "contourcarpet"
}
],
"heatmap": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmap"
}
],
"heatmapgl": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmapgl"
}
],
"histogram": [
{
"marker": {
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "histogram"
}
],
"histogram2d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2d"
}
],
"histogram2dcontour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2dcontour"
}
],
"mesh3d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "mesh3d"
}
],
"parcoords": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "parcoords"
}
],
"pie": [
{
"automargin": true,
"type": "pie"
}
],
"scatter": [
{
"fillpattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
},
"type": "scatter"
}
],
"scatter3d": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter3d"
}
],
"scattercarpet": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattercarpet"
}
],
"scattergeo": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergeo"
}
],
"scattergl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergl"
}
],
"scattermapbox": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattermapbox"
}
],
"scatterpolar": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolar"
}
],
"scatterpolargl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolargl"
}
],
"scatterternary": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterternary"
}
],
"surface": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "surface"
}
],
"table": [
{
"cells": {
"fill": {
"color": "#EBF0F8"
},
"line": {
"color": "white"
}
},
"header": {
"fill": {
"color": "#C8D4E3"
},
"line": {
"color": "white"
}
},
"type": "table"
}
]
},
"layout": {
"annotationdefaults": {
"arrowcolor": "#2a3f5f",
"arrowhead": 0,
"arrowwidth": 1
},
"autotypenumbers": "strict",
"coloraxis": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"colorscale": {
"diverging": [
[
0,
"#8e0152"
],
[
0.1,
"#c51b7d"
],
[
0.2,
"#de77ae"
],
[
0.3,
"#f1b6da"
],
[
0.4,
"#fde0ef"
],
[
0.5,
"#f7f7f7"
],
[
0.6,
"#e6f5d0"
],
[
0.7,
"#b8e186"
],
[
0.8,
"#7fbc41"
],
[
0.9,
"#4d9221"
],
[
1,
"#276419"
]
],
"sequential": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"sequentialminus": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
]
},
"colorway": [
"#636efa",
"#EF553B",
"#00cc96",
"#ab63fa",
"#FFA15A",
"#19d3f3",
"#FF6692",
"#B6E880",
"#FF97FF",
"#FECB52"
],
"font": {
"color": "#2a3f5f"
},
"geo": {
"bgcolor": "white",
"lakecolor": "white",
"landcolor": "#E5ECF6",
"showlakes": true,
"showland": true,
"subunitcolor": "white"
},
"hoverlabel": {
"align": "left"
},
"hovermode": "closest",
"mapbox": {
"style": "light"
},
"paper_bgcolor": "white",
"plot_bgcolor": "#E5ECF6",
"polar": {
"angularaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"radialaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"scene": {
"xaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"yaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"zaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
}
},
"shapedefaults": {
"line": {
"color": "#2a3f5f"
}
},
"ternary": {
"aaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"baxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"caxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"title": {
"x": 0.05
},
"xaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
},
"yaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
}
}
},
"title": {
"font": {
"size": 20
},
"text": "Total de población por continente",
"x": 0.5
},
"width": 1000,
"xaxis": {
"anchor": "y",
"domain": [
0,
1
],
"tickformat": ",d",
"title": {
"font": {
"size": 16
},
"text": "Población (habitantes)"
}
},
"yaxis": {
"anchor": "x",
"domain": [
0,
1
],
"title": {
"font": {
"size": 16
},
"text": "Continente"
}
}
}
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Cálculo de la suma de población por continente\n",
"poblacion_continente_suma = paises.groupby('CONTINENT')['POP_EST'].sum().sort_values(ascending=True).reset_index()\n",
"\n",
"# Creación de gráfico de barras verticales\n",
"fig = px.bar(\n",
" poblacion_continente_suma,\n",
" x='POP_EST',\n",
" y='CONTINENT',\n",
" orientation='h',\n",
" title='Total de población por continente',\n",
" labels={\n",
" 'CONTINENT': 'Continente',\n",
" 'POP_EST': 'Población (habitantes)'\n",
" },\n",
" width=1000, # Ancho de la figura en píxeles\n",
" height=600 # Alto de la figura en píxeles\n",
")\n",
"\n",
"# Actualizar el formato del eje y evitar notación científica\n",
"fig.update_xaxes(tickformat=\",d\")\n",
"\n",
"# Atributos globales de la figura\n",
"fig.update_layout(\n",
" title=dict(\n",
" x=0.5, # Centrar el título\n",
" font=dict(size=20)\n",
" ),\n",
" xaxis_title=dict(font=dict(size=16)),\n",
" yaxis_title=dict(font=dict(size=16))\n",
")\n",
"\n",
"# Despliegue del gráfico\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"id": "d7bf07a5",
"metadata": {},
"source": [
"##### Suma de población por región y subregión de la ONU"
]
},
{
"cell_type": "markdown",
"id": "a1cd4a08",
"metadata": {},
"source": [
"###### Barras apiladas"
]
},
{
"cell_type": "markdown",
"id": "3fff20c6",
"metadata": {},
"source": [
"Con el argumento `color=SUBREGION` puede generarse un gráfico de barras apiladas (en inglés, *stacked*) en el que para cada región se muestran sus subregiones."
]
},
{
"cell_type": "code",
"execution_count": 222,
"id": "7f6cc8f7",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Eastern Africa
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Eastern Africa",
"marker": {
"color": "#636efa",
"pattern": {
"shape": ""
}
},
"name": "Eastern Africa",
"offsetgroup": "Eastern Africa",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Africa"
],
"xaxis": "x",
"y": [
435175445.3
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Middle Africa
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Middle Africa",
"marker": {
"color": "#EF553B",
"pattern": {
"shape": ""
}
},
"name": "Middle Africa",
"offsetgroup": "Middle Africa",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Africa"
],
"xaxis": "x",
"y": [
174308432
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Northern Africa
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Northern Africa",
"marker": {
"color": "#00cc96",
"pattern": {
"shape": ""
}
},
"name": "Northern Africa",
"offsetgroup": "Northern Africa",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Africa"
],
"xaxis": "x",
"y": [
241801558
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Southern Africa
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Southern Africa",
"marker": {
"color": "#ab63fa",
"pattern": {
"shape": ""
}
},
"name": "Southern Africa",
"offsetgroup": "Southern Africa",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Africa"
],
"xaxis": "x",
"y": [
66629895
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Western Africa
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Western Africa",
"marker": {
"color": "#FFA15A",
"pattern": {
"shape": ""
}
},
"name": "Western Africa",
"offsetgroup": "Western Africa",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Africa"
],
"xaxis": "x",
"y": [
391434098
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Caribbean
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Caribbean",
"marker": {
"color": "#19d3f3",
"pattern": {
"shape": ""
}
},
"name": "Caribbean",
"offsetgroup": "Caribbean",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Americas"
],
"xaxis": "x",
"y": [
38982419
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Central America
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Central America",
"marker": {
"color": "#FF6692",
"pattern": {
"shape": ""
}
},
"name": "Central America",
"offsetgroup": "Central America",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Americas"
],
"xaxis": "x",
"y": [
176609080
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Northern America
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Northern America",
"marker": {
"color": "#B6E880",
"pattern": {
"shape": ""
}
},
"name": "Northern America",
"offsetgroup": "Northern America",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Americas"
],
"xaxis": "x",
"y": [
369184722
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=South America
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "South America",
"marker": {
"color": "#FF97FF",
"pattern": {
"shape": ""
}
},
"name": "South America",
"offsetgroup": "South America",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Americas"
],
"xaxis": "x",
"y": [
427063263
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Antarctica
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Antarctica",
"marker": {
"color": "#FECB52",
"pattern": {
"shape": ""
}
},
"name": "Antarctica",
"offsetgroup": "Antarctica",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Antarctica"
],
"xaxis": "x",
"y": [
4490
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Central Asia
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Central Asia",
"marker": {
"color": "#636efa",
"pattern": {
"shape": ""
}
},
"name": "Central Asia",
"offsetgroup": "Central Asia",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Asia"
],
"xaxis": "x",
"y": [
73814587
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Eastern Asia
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Eastern Asia",
"marker": {
"color": "#EF553B",
"pattern": {
"shape": ""
}
},
"name": "Eastern Asia",
"offsetgroup": "Eastern Asia",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Asia"
],
"xaxis": "x",
"y": [
1636296580
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=South-Eastern Asia
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "South-Eastern Asia",
"marker": {
"color": "#00cc96",
"pattern": {
"shape": ""
}
},
"name": "South-Eastern Asia",
"offsetgroup": "South-Eastern Asia",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Asia"
],
"xaxis": "x",
"y": [
661911038
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Southern Asia
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Southern Asia",
"marker": {
"color": "#ab63fa",
"pattern": {
"shape": ""
}
},
"name": "Southern Asia",
"offsetgroup": "Southern Asia",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Asia"
],
"xaxis": "x",
"y": [
1918690648
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Western Asia
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Western Asia",
"marker": {
"color": "#FFA15A",
"pattern": {
"shape": ""
}
},
"name": "Western Asia",
"offsetgroup": "Western Asia",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Asia"
],
"xaxis": "x",
"y": [
272122703
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Eastern Europe
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Eastern Europe",
"marker": {
"color": "#19d3f3",
"pattern": {
"shape": ""
}
},
"name": "Eastern Europe",
"offsetgroup": "Eastern Europe",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Europe"
],
"xaxis": "x",
"y": [
291080093
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Northern Europe
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Northern Europe",
"marker": {
"color": "#FF6692",
"pattern": {
"shape": ""
}
},
"name": "Northern Europe",
"offsetgroup": "Northern Europe",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Europe"
],
"xaxis": "x",
"y": [
105802448
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Southern Europe
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Southern Europe",
"marker": {
"color": "#B6E880",
"pattern": {
"shape": ""
}
},
"name": "Southern Europe",
"offsetgroup": "Southern Europe",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Europe"
],
"xaxis": "x",
"y": [
152729852
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Western Europe
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Western Europe",
"marker": {
"color": "#FF97FF",
"pattern": {
"shape": ""
}
},
"name": "Western Europe",
"offsetgroup": "Western Europe",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Europe"
],
"xaxis": "x",
"y": [
198095699
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Australia and New Zealand
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Australia and New Zealand",
"marker": {
"color": "#FECB52",
"pattern": {
"shape": ""
}
},
"name": "Australia and New Zealand",
"offsetgroup": "Australia and New Zealand",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Oceania"
],
"xaxis": "x",
"y": [
30319695
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Melanesia
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Melanesia",
"marker": {
"color": "#636efa",
"pattern": {
"shape": ""
}
},
"name": "Melanesia",
"offsetgroup": "Melanesia",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Oceania"
],
"xaxis": "x",
"y": [
10635767
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Micronesia
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Micronesia",
"marker": {
"color": "#EF553B",
"pattern": {
"shape": ""
}
},
"name": "Micronesia",
"offsetgroup": "Micronesia",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Oceania"
],
"xaxis": "x",
"y": [
320801
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Polynesia
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Polynesia",
"marker": {
"color": "#00cc96",
"pattern": {
"shape": ""
}
},
"name": "Polynesia",
"offsetgroup": "Polynesia",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Oceania"
],
"xaxis": "x",
"y": [
313237
],
"yaxis": "y"
}
],
"layout": {
"barmode": "relative",
"height": 600,
"legend": {
"font": {
"size": 14
},
"title": {
"font": {
"size": 16
},
"text": "Subregión"
},
"tracegroupgap": 0,
"x": 1.05,
"y": 1
},
"template": {
"data": {
"bar": [
{
"error_x": {
"color": "#2a3f5f"
},
"error_y": {
"color": "#2a3f5f"
},
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "bar"
}
],
"barpolar": [
{
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "barpolar"
}
],
"carpet": [
{
"aaxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"baxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"type": "carpet"
}
],
"choropleth": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "choropleth"
}
],
"contour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "contour"
}
],
"contourcarpet": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "contourcarpet"
}
],
"heatmap": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmap"
}
],
"heatmapgl": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmapgl"
}
],
"histogram": [
{
"marker": {
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "histogram"
}
],
"histogram2d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2d"
}
],
"histogram2dcontour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2dcontour"
}
],
"mesh3d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "mesh3d"
}
],
"parcoords": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "parcoords"
}
],
"pie": [
{
"automargin": true,
"type": "pie"
}
],
"scatter": [
{
"fillpattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
},
"type": "scatter"
}
],
"scatter3d": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter3d"
}
],
"scattercarpet": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattercarpet"
}
],
"scattergeo": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergeo"
}
],
"scattergl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergl"
}
],
"scattermapbox": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattermapbox"
}
],
"scatterpolar": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolar"
}
],
"scatterpolargl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolargl"
}
],
"scatterternary": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterternary"
}
],
"surface": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "surface"
}
],
"table": [
{
"cells": {
"fill": {
"color": "#EBF0F8"
},
"line": {
"color": "white"
}
},
"header": {
"fill": {
"color": "#C8D4E3"
},
"line": {
"color": "white"
}
},
"type": "table"
}
]
},
"layout": {
"annotationdefaults": {
"arrowcolor": "#2a3f5f",
"arrowhead": 0,
"arrowwidth": 1
},
"autotypenumbers": "strict",
"coloraxis": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"colorscale": {
"diverging": [
[
0,
"#8e0152"
],
[
0.1,
"#c51b7d"
],
[
0.2,
"#de77ae"
],
[
0.3,
"#f1b6da"
],
[
0.4,
"#fde0ef"
],
[
0.5,
"#f7f7f7"
],
[
0.6,
"#e6f5d0"
],
[
0.7,
"#b8e186"
],
[
0.8,
"#7fbc41"
],
[
0.9,
"#4d9221"
],
[
1,
"#276419"
]
],
"sequential": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"sequentialminus": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
]
},
"colorway": [
"#636efa",
"#EF553B",
"#00cc96",
"#ab63fa",
"#FFA15A",
"#19d3f3",
"#FF6692",
"#B6E880",
"#FF97FF",
"#FECB52"
],
"font": {
"color": "#2a3f5f"
},
"geo": {
"bgcolor": "white",
"lakecolor": "white",
"landcolor": "#E5ECF6",
"showlakes": true,
"showland": true,
"subunitcolor": "white"
},
"hoverlabel": {
"align": "left"
},
"hovermode": "closest",
"mapbox": {
"style": "light"
},
"paper_bgcolor": "white",
"plot_bgcolor": "#E5ECF6",
"polar": {
"angularaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"radialaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"scene": {
"xaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"yaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"zaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
}
},
"shapedefaults": {
"line": {
"color": "#2a3f5f"
}
},
"ternary": {
"aaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"baxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"caxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"title": {
"x": 0.05
},
"xaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
},
"yaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
}
}
},
"title": {
"font": {
"size": 20
},
"text": "Total de población por región y subregión de la ONU",
"x": 0.5
},
"width": 1000,
"xaxis": {
"anchor": "y",
"categoryarray": [
"Asia",
"Africa",
"Americas",
"Europe",
"Oceania",
"Antarctica"
],
"categoryorder": "array",
"domain": [
0,
1
],
"title": {
"font": {
"size": 16
},
"text": "Región"
}
},
"yaxis": {
"anchor": "x",
"domain": [
0,
1
],
"tickformat": ",d",
"title": {
"font": {
"size": 16
},
"text": "Población (habitantes)"
}
}
}
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Suma de población por región y subregión de la ONU\n",
"# Con reset_index() se evita la creación de un índice y \n",
"# así es posible usar más fácilmente todas las columnas en el gráfico\n",
"poblacion_region_subregion_suma = paises.groupby(['REGION_UN', 'SUBREGION'])['POP_EST'].sum().reset_index()\n",
"\n",
"# Ordenar las regiones por población total descendente\n",
"poblacion_total_region = poblacion_region_subregion_suma.groupby('REGION_UN')['POP_EST'].sum().sort_values(ascending=False).index\n",
"\n",
"# Asignar el orden de las categorías en el eje X\n",
"poblacion_region_subregion_suma['REGION_UN'] = pd.Categorical(\n",
" poblacion_region_subregion_suma['REGION_UN'],\n",
" categories=poblacion_total_region,\n",
" ordered=True\n",
")\n",
"\n",
"# Creación del gráfico de barras apiladas\n",
"fig = px.bar(\n",
" poblacion_region_subregion_suma,\n",
" x='REGION_UN',\n",
" y='POP_EST',\n",
" color='SUBREGION',\n",
" title='Total de población por región y subregión de la ONU',\n",
" labels={\n",
" 'REGION_UN': 'Región',\n",
" 'SUBREGION': 'Subregión',\n",
" 'POP_EST': 'Población (habitantes)'\n",
" },\n",
" category_orders={'REGION_UN': poblacion_total_region}, # Aplicar el orden de las regiones\n",
" width=1000, # Ancho de la figura en píxeles\n",
" height=600 # Alto de la figura en píxeles\n",
")\n",
"\n",
"# Actualizar el formato del eje y para evitar notación científica\n",
"fig.update_yaxes(tickformat=\",d\")\n",
"\n",
"# Atributos globales de la figura\n",
"fig.update_layout(\n",
" title=dict(\n",
" x=0.5, # Centrar el título\n",
" font=dict(size=20)\n",
" ),\n",
" xaxis_title=dict(font=dict(size=16)),\n",
" yaxis_title=dict(font=dict(size=16)),\n",
" legend_title=dict(\n",
" text='Subregión',\n",
" font=dict(size=16)\n",
" ),\n",
" legend=dict(\n",
" title_font_size=16,\n",
" font_size=14,\n",
" x=1.05, # Posición horizontal de la leyenda\n",
" y=1 # Posición vertical de la leyenda\n",
" )\n",
")\n",
"\n",
"# Despliegue del gráfico\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"id": "f85f6863",
"metadata": {},
"source": [
"###### Barras agrupadas"
]
},
{
"cell_type": "markdown",
"id": "6d1663b4",
"metadata": {},
"source": [
"Otra forma de mostrar barras con diferentes niveles de agrupación son las barras agrupadas, con el argumento `barmode=group` en la función `fig.update.layout()`."
]
},
{
"cell_type": "code",
"execution_count": 223,
"id": "feccc4de",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Eastern Africa
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Eastern Africa",
"marker": {
"color": "#636efa",
"pattern": {
"shape": ""
}
},
"name": "Eastern Africa",
"offsetgroup": "Eastern Africa",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Africa"
],
"xaxis": "x",
"y": [
435175445.3
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Middle Africa
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Middle Africa",
"marker": {
"color": "#EF553B",
"pattern": {
"shape": ""
}
},
"name": "Middle Africa",
"offsetgroup": "Middle Africa",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Africa"
],
"xaxis": "x",
"y": [
174308432
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Northern Africa
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Northern Africa",
"marker": {
"color": "#00cc96",
"pattern": {
"shape": ""
}
},
"name": "Northern Africa",
"offsetgroup": "Northern Africa",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Africa"
],
"xaxis": "x",
"y": [
241801558
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Southern Africa
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Southern Africa",
"marker": {
"color": "#ab63fa",
"pattern": {
"shape": ""
}
},
"name": "Southern Africa",
"offsetgroup": "Southern Africa",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Africa"
],
"xaxis": "x",
"y": [
66629895
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Western Africa
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Western Africa",
"marker": {
"color": "#FFA15A",
"pattern": {
"shape": ""
}
},
"name": "Western Africa",
"offsetgroup": "Western Africa",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Africa"
],
"xaxis": "x",
"y": [
391434098
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Caribbean
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Caribbean",
"marker": {
"color": "#19d3f3",
"pattern": {
"shape": ""
}
},
"name": "Caribbean",
"offsetgroup": "Caribbean",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Americas"
],
"xaxis": "x",
"y": [
38982419
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Central America
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Central America",
"marker": {
"color": "#FF6692",
"pattern": {
"shape": ""
}
},
"name": "Central America",
"offsetgroup": "Central America",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Americas"
],
"xaxis": "x",
"y": [
176609080
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Northern America
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Northern America",
"marker": {
"color": "#B6E880",
"pattern": {
"shape": ""
}
},
"name": "Northern America",
"offsetgroup": "Northern America",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Americas"
],
"xaxis": "x",
"y": [
369184722
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=South America
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "South America",
"marker": {
"color": "#FF97FF",
"pattern": {
"shape": ""
}
},
"name": "South America",
"offsetgroup": "South America",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Americas"
],
"xaxis": "x",
"y": [
427063263
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Antarctica
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Antarctica",
"marker": {
"color": "#FECB52",
"pattern": {
"shape": ""
}
},
"name": "Antarctica",
"offsetgroup": "Antarctica",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Antarctica"
],
"xaxis": "x",
"y": [
4490
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Central Asia
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Central Asia",
"marker": {
"color": "#636efa",
"pattern": {
"shape": ""
}
},
"name": "Central Asia",
"offsetgroup": "Central Asia",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Asia"
],
"xaxis": "x",
"y": [
73814587
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Eastern Asia
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Eastern Asia",
"marker": {
"color": "#EF553B",
"pattern": {
"shape": ""
}
},
"name": "Eastern Asia",
"offsetgroup": "Eastern Asia",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Asia"
],
"xaxis": "x",
"y": [
1636296580
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=South-Eastern Asia
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "South-Eastern Asia",
"marker": {
"color": "#00cc96",
"pattern": {
"shape": ""
}
},
"name": "South-Eastern Asia",
"offsetgroup": "South-Eastern Asia",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Asia"
],
"xaxis": "x",
"y": [
661911038
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Southern Asia
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Southern Asia",
"marker": {
"color": "#ab63fa",
"pattern": {
"shape": ""
}
},
"name": "Southern Asia",
"offsetgroup": "Southern Asia",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Asia"
],
"xaxis": "x",
"y": [
1918690648
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Western Asia
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Western Asia",
"marker": {
"color": "#FFA15A",
"pattern": {
"shape": ""
}
},
"name": "Western Asia",
"offsetgroup": "Western Asia",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Asia"
],
"xaxis": "x",
"y": [
272122703
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Eastern Europe
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Eastern Europe",
"marker": {
"color": "#19d3f3",
"pattern": {
"shape": ""
}
},
"name": "Eastern Europe",
"offsetgroup": "Eastern Europe",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Europe"
],
"xaxis": "x",
"y": [
291080093
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Northern Europe
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Northern Europe",
"marker": {
"color": "#FF6692",
"pattern": {
"shape": ""
}
},
"name": "Northern Europe",
"offsetgroup": "Northern Europe",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Europe"
],
"xaxis": "x",
"y": [
105802448
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Southern Europe
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Southern Europe",
"marker": {
"color": "#B6E880",
"pattern": {
"shape": ""
}
},
"name": "Southern Europe",
"offsetgroup": "Southern Europe",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Europe"
],
"xaxis": "x",
"y": [
152729852
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Western Europe
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Western Europe",
"marker": {
"color": "#FF97FF",
"pattern": {
"shape": ""
}
},
"name": "Western Europe",
"offsetgroup": "Western Europe",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Europe"
],
"xaxis": "x",
"y": [
198095699
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Australia and New Zealand
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Australia and New Zealand",
"marker": {
"color": "#FECB52",
"pattern": {
"shape": ""
}
},
"name": "Australia and New Zealand",
"offsetgroup": "Australia and New Zealand",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Oceania"
],
"xaxis": "x",
"y": [
30319695
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Melanesia
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Melanesia",
"marker": {
"color": "#636efa",
"pattern": {
"shape": ""
}
},
"name": "Melanesia",
"offsetgroup": "Melanesia",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Oceania"
],
"xaxis": "x",
"y": [
10635767
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Micronesia
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Micronesia",
"marker": {
"color": "#EF553B",
"pattern": {
"shape": ""
}
},
"name": "Micronesia",
"offsetgroup": "Micronesia",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Oceania"
],
"xaxis": "x",
"y": [
320801
],
"yaxis": "y"
},
{
"alignmentgroup": "True",
"hovertemplate": "Subregión=Polynesia
Región=%{x}
Población (habitantes)=%{y}",
"legendgroup": "Polynesia",
"marker": {
"color": "#00cc96",
"pattern": {
"shape": ""
}
},
"name": "Polynesia",
"offsetgroup": "Polynesia",
"orientation": "v",
"showlegend": true,
"textposition": "auto",
"type": "bar",
"x": [
"Oceania"
],
"xaxis": "x",
"y": [
313237
],
"yaxis": "y"
}
],
"layout": {
"barmode": "group",
"height": 600,
"legend": {
"font": {
"size": 14
},
"title": {
"font": {
"size": 16
},
"text": "Subregión"
},
"tracegroupgap": 0,
"x": 1.05,
"y": 1
},
"template": {
"data": {
"bar": [
{
"error_x": {
"color": "#2a3f5f"
},
"error_y": {
"color": "#2a3f5f"
},
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "bar"
}
],
"barpolar": [
{
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "barpolar"
}
],
"carpet": [
{
"aaxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"baxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"type": "carpet"
}
],
"choropleth": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "choropleth"
}
],
"contour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "contour"
}
],
"contourcarpet": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "contourcarpet"
}
],
"heatmap": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmap"
}
],
"heatmapgl": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmapgl"
}
],
"histogram": [
{
"marker": {
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "histogram"
}
],
"histogram2d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2d"
}
],
"histogram2dcontour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2dcontour"
}
],
"mesh3d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "mesh3d"
}
],
"parcoords": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "parcoords"
}
],
"pie": [
{
"automargin": true,
"type": "pie"
}
],
"scatter": [
{
"fillpattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
},
"type": "scatter"
}
],
"scatter3d": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter3d"
}
],
"scattercarpet": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattercarpet"
}
],
"scattergeo": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergeo"
}
],
"scattergl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergl"
}
],
"scattermapbox": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattermapbox"
}
],
"scatterpolar": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolar"
}
],
"scatterpolargl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolargl"
}
],
"scatterternary": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterternary"
}
],
"surface": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "surface"
}
],
"table": [
{
"cells": {
"fill": {
"color": "#EBF0F8"
},
"line": {
"color": "white"
}
},
"header": {
"fill": {
"color": "#C8D4E3"
},
"line": {
"color": "white"
}
},
"type": "table"
}
]
},
"layout": {
"annotationdefaults": {
"arrowcolor": "#2a3f5f",
"arrowhead": 0,
"arrowwidth": 1
},
"autotypenumbers": "strict",
"coloraxis": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"colorscale": {
"diverging": [
[
0,
"#8e0152"
],
[
0.1,
"#c51b7d"
],
[
0.2,
"#de77ae"
],
[
0.3,
"#f1b6da"
],
[
0.4,
"#fde0ef"
],
[
0.5,
"#f7f7f7"
],
[
0.6,
"#e6f5d0"
],
[
0.7,
"#b8e186"
],
[
0.8,
"#7fbc41"
],
[
0.9,
"#4d9221"
],
[
1,
"#276419"
]
],
"sequential": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"sequentialminus": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
]
},
"colorway": [
"#636efa",
"#EF553B",
"#00cc96",
"#ab63fa",
"#FFA15A",
"#19d3f3",
"#FF6692",
"#B6E880",
"#FF97FF",
"#FECB52"
],
"font": {
"color": "#2a3f5f"
},
"geo": {
"bgcolor": "white",
"lakecolor": "white",
"landcolor": "#E5ECF6",
"showlakes": true,
"showland": true,
"subunitcolor": "white"
},
"hoverlabel": {
"align": "left"
},
"hovermode": "closest",
"mapbox": {
"style": "light"
},
"paper_bgcolor": "white",
"plot_bgcolor": "#E5ECF6",
"polar": {
"angularaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"radialaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"scene": {
"xaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"yaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"zaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
}
},
"shapedefaults": {
"line": {
"color": "#2a3f5f"
}
},
"ternary": {
"aaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"baxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"caxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"title": {
"x": 0.05
},
"xaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
},
"yaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
}
}
},
"title": {
"font": {
"size": 20
},
"text": "Suma de población por región y subregión de la ONU",
"x": 0.5
},
"width": 1000,
"xaxis": {
"anchor": "y",
"categoryarray": [
"Asia",
"Africa",
"Americas",
"Europe",
"Oceania",
"Antarctica"
],
"categoryorder": "array",
"domain": [
0,
1
],
"title": {
"font": {
"size": 16
},
"text": "Región"
}
},
"yaxis": {
"anchor": "x",
"domain": [
0,
1
],
"tickformat": ",d",
"title": {
"font": {
"size": 16
},
"text": "Población (habitantes)"
}
}
}
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Suma de población por región y subregión de la ONU\n",
"poblacion_region_subregion_suma = paises.groupby(['REGION_UN', 'SUBREGION'])['POP_EST'].sum().reset_index()\n",
"\n",
"# Ordenar las regiones por población total descendente\n",
"poblacion_total_region = poblacion_region_subregion_suma.groupby('REGION_UN')['POP_EST'].sum().sort_values(ascending=False).index\n",
"\n",
"# Asignar el orden de las categorías en el eje X\n",
"poblacion_region_subregion_suma['REGION_UN'] = pd.Categorical(\n",
" poblacion_region_subregion_suma['REGION_UN'],\n",
" categories=poblacion_total_region,\n",
" ordered=True\n",
")\n",
"\n",
"# Creación del gráfico de barras agrupadas con Plotly Express\n",
"fig = px.bar(\n",
" poblacion_region_subregion_suma,\n",
" x='REGION_UN',\n",
" y='POP_EST',\n",
" color='SUBREGION',\n",
" title='Suma de población por región y subregión de la ONU',\n",
" labels={\n",
" 'REGION_UN': 'Región',\n",
" 'SUBREGION': 'Subregión',\n",
" 'POP_EST': 'Población (habitantes)'\n",
" },\n",
" category_orders={'REGION_UN': poblacion_total_region}, # Aplicar el orden de las regiones\n",
" width=1000, # Ancho de la figura en píxeles\n",
" height=600 # Alto de la figura en píxeles\n",
")\n",
"\n",
"# Actualizar el formato del eje y para evitar notación científica\n",
"fig.update_yaxes(tickformat=\",d\")\n",
"\n",
"# Personalizar el diseño para barras agrupadas\n",
"fig.update_layout(\n",
" barmode='group', # Establecer el modo de barras a 'group' para barras agrupadas\n",
" title=dict(\n",
" x=0.5, # Centrar el título\n",
" font=dict(size=20)\n",
" ),\n",
" xaxis_title=dict(font=dict(size=16)),\n",
" yaxis_title=dict(font=dict(size=16)),\n",
" legend_title=dict(\n",
" text='Subregión',\n",
" font=dict(size=16)\n",
" ),\n",
" legend=dict(\n",
" title_font_size=16,\n",
" font_size=14,\n",
" x=1.05, # Posición horizontal de la leyenda\n",
" y=1 # Posición vertical de la leyenda\n",
" )\n",
")\n",
"\n",
"# Despliegue del gráfico\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"id": "aabb7486",
"metadata": {},
"source": [
"#### Ejercicios"
]
},
{
"cell_type": "markdown",
"id": "d31b7a47",
"metadata": {},
"source": [
"1. Programe gráficos de barras para:\n",
"- Suma de PIB (`GDP_MD`) por economía (`ECONOMY`).\n",
"- Promedio de PIB per cápita por grupo de ingresos (`INCOME_GRP`)."
]
},
{
"cell_type": "markdown",
"id": "c74f67a8",
"metadata": {},
"source": [
"### Gráficos de pastel"
]
},
{
"cell_type": "markdown",
"id": "f12848a9",
"metadata": {},
"source": [
"Un [gráfico de pastel](https://es.wikipedia.org/wiki/Gr%C3%A1fico_circular) representa porcentajes y porciones en secciones (*slices*) de un círculo. Son muy populares, pero también son criticados debido a la dificultad del cerebro humano de comparar áreas de sectores circulares, por lo que [algunos expertos recomiendan sustituirlos por otros tipos de gráficos como, por ejemplo, gráficos de barras](https://www.data-to-viz.com/caveat/pie.html).\n",
"\n",
"En `plotly.express`, los gráficos de pastel se implementan con el método [`plotly.express.pie()`](https://plotly.github.io/plotly.py-docs/generated/plotly.express.pie.html). Se recomienda leer el [tutorial](https://plotly.com/python/pie-charts/)."
]
},
{
"cell_type": "code",
"execution_count": 224,
"id": "7cc702ca",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"domain": {
"x": [
0,
1
],
"y": [
0,
1
]
},
"hovertemplate": "Continente=%{label}
Población (habitantes)=%{value}",
"labels": [
"Antarctica",
"Seven seas (open ocean)",
"Oceania",
"South America",
"North America",
"Europe",
"Africa",
"Asia"
],
"legendgroup": "",
"name": "",
"showlegend": true,
"textinfo": "percent",
"textposition": "inside",
"type": "pie",
"values": [
4490,
1894289,
41589500,
427063263,
584776221,
747708092,
1307986092.3,
4562304603
]
}
],
"layout": {
"legend": {
"title": {
"text": "Continente"
},
"tracegroupgap": 0
},
"template": {
"data": {
"bar": [
{
"error_x": {
"color": "#2a3f5f"
},
"error_y": {
"color": "#2a3f5f"
},
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "bar"
}
],
"barpolar": [
{
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "barpolar"
}
],
"carpet": [
{
"aaxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"baxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"type": "carpet"
}
],
"choropleth": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "choropleth"
}
],
"contour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "contour"
}
],
"contourcarpet": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "contourcarpet"
}
],
"heatmap": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmap"
}
],
"heatmapgl": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmapgl"
}
],
"histogram": [
{
"marker": {
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "histogram"
}
],
"histogram2d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2d"
}
],
"histogram2dcontour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2dcontour"
}
],
"mesh3d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "mesh3d"
}
],
"parcoords": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "parcoords"
}
],
"pie": [
{
"automargin": true,
"type": "pie"
}
],
"scatter": [
{
"fillpattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
},
"type": "scatter"
}
],
"scatter3d": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter3d"
}
],
"scattercarpet": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattercarpet"
}
],
"scattergeo": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergeo"
}
],
"scattergl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergl"
}
],
"scattermapbox": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattermapbox"
}
],
"scatterpolar": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolar"
}
],
"scatterpolargl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolargl"
}
],
"scatterternary": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterternary"
}
],
"surface": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "surface"
}
],
"table": [
{
"cells": {
"fill": {
"color": "#EBF0F8"
},
"line": {
"color": "white"
}
},
"header": {
"fill": {
"color": "#C8D4E3"
},
"line": {
"color": "white"
}
},
"type": "table"
}
]
},
"layout": {
"annotationdefaults": {
"arrowcolor": "#2a3f5f",
"arrowhead": 0,
"arrowwidth": 1
},
"autotypenumbers": "strict",
"coloraxis": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"colorscale": {
"diverging": [
[
0,
"#8e0152"
],
[
0.1,
"#c51b7d"
],
[
0.2,
"#de77ae"
],
[
0.3,
"#f1b6da"
],
[
0.4,
"#fde0ef"
],
[
0.5,
"#f7f7f7"
],
[
0.6,
"#e6f5d0"
],
[
0.7,
"#b8e186"
],
[
0.8,
"#7fbc41"
],
[
0.9,
"#4d9221"
],
[
1,
"#276419"
]
],
"sequential": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"sequentialminus": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
]
},
"colorway": [
"#636efa",
"#EF553B",
"#00cc96",
"#ab63fa",
"#FFA15A",
"#19d3f3",
"#FF6692",
"#B6E880",
"#FF97FF",
"#FECB52"
],
"font": {
"color": "#2a3f5f"
},
"geo": {
"bgcolor": "white",
"lakecolor": "white",
"landcolor": "#E5ECF6",
"showlakes": true,
"showland": true,
"subunitcolor": "white"
},
"hoverlabel": {
"align": "left"
},
"hovermode": "closest",
"mapbox": {
"style": "light"
},
"paper_bgcolor": "white",
"plot_bgcolor": "#E5ECF6",
"polar": {
"angularaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"radialaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"scene": {
"xaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"yaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"zaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
}
},
"shapedefaults": {
"line": {
"color": "#2a3f5f"
}
},
"ternary": {
"aaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"baxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"caxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"title": {
"x": 0.05
},
"xaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
},
"yaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
}
}
},
"title": {
"text": "Total de población por continente"
}
}
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Cálculo de la suma de población por continente\n",
"poblacion_continente_suma = paises.groupby('CONTINENT')['POP_EST'].sum().sort_values(ascending=True).reset_index()\n",
"\n",
"# Creación del gráfico de pastel\n",
"fig = px.pie(\n",
" poblacion_continente_suma,\n",
" names='CONTINENT',\n",
" values='POP_EST',\n",
" title='Total de población por continente',\n",
" labels={'CONTINENT': 'Continente', 'POP_EST': 'Población (habitantes)'}\n",
")\n",
"\n",
"# Atributos globales de la figura\n",
"fig.update_layout(\n",
" legend_title_text='Continente'\n",
")\n",
"\n",
"# Atributos de las propiedades visuales\n",
"fig.update_traces(textposition='inside', textinfo='percent')\n",
"\n",
"# Despliegue del gráfico\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"id": "4ca7ce24",
"metadata": {},
"source": [
"#### Ejercicios\n",
"\n",
"1. Pruebe los siguientes argumentos y observe el resultado:\n",
"- `hole=0.3` (en `px.pie()`)\n",
"- `textposition='outside'` (en `fig.update_traces()`)\n",
"- `textinfo='percent+label'` (en `fig.update_traces()`)\n",
"- `marker=dict(colors=px.colors.qualitative.Pastel)` (en `fig.update_traces()`)"
]
},
{
"cell_type": "markdown",
"id": "9ddb81bf",
"metadata": {},
"source": [
"### Histogramas"
]
},
{
"cell_type": "markdown",
"id": "bebecffc",
"metadata": {},
"source": [
"Un [histograma](https://es.wikipedia.org/wiki/Histograma) es una representación gráfica de la distribución de una variable numérica en forma de barras (en este caso, llamadas en inglés *bins*). La longitud de cada barra representa la frecuencia de un rango de valores de la variable. La graficación de la distribución de las variables es, frecuentemente, una de las primeras tareas que se realiza cuando se explora un conjunto de datos.\n",
"\n",
"En `plotly.express`, los histogramas se implementan con la función [`plotly.express.histogram()`](https://plotly.github.io/plotly.py-docs/generated/plotly.express.histogram.html). Se recomienda leer el [tutorial](https://plotly.com/python/histograms/).\n",
"\n",
"El siguiente bloque de código muestra la distibución de la variable población estimada (`POP_EST`) mediante un histograma."
]
},
{
"cell_type": "code",
"execution_count": 225,
"id": "8d5fc1c2",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"alignmentgroup": "True",
"bingroup": "x",
"hovertemplate": "Población estimada (habitantes)=%{x}
count=%{y}",
"legendgroup": "",
"marker": {
"color": "#636efa",
"pattern": {
"shape": ""
}
},
"name": "",
"nbinsx": 10,
"offsetgroup": "",
"orientation": "v",
"showlegend": false,
"type": "histogram",
"x": [
270625568,
31949777,
18952038,
11513100,
32510453,
44938712,
67366465,
1198575,
1366417754,
1405862845,
13738606,
6855713,
112078730,
11062113,
10192317.3,
52573973,
18628747,
58005463,
17070135,
5096159,
67692632,
581363,
782766,
51709098,
25666161,
36471769,
603253,
5047561,
6545502,
5380508,
86790567,
763092,
44385155,
9466856,
2494530,
58558270,
17637435,
4974986,
33580650,
18513930,
9321018,
2786844,
211049527,
3461734,
3225167,
144373535,
10669709,
83132799,
1326590,
1912789,
5347896,
10285453,
5550198,
96462106,
16486542,
619896,
9770529,
11484055,
245246,
2083459,
2854191,
10023318,
1794248,
83429615,
47076781,
7169455,
6456900,
2957731,
5923456,
6777452,
11694719,
19356544,
9769949,
5454073,
37970874,
4941444,
10716322,
17861030,
7813215,
12771246,
4937374,
4745185,
42813238,
973560,
6081196,
8877067,
39309783,
60297396,
8574832,
82913906,
38019,
25716544,
6944975,
19658031,
16296364,
200963599,
11801151,
31825295,
4067500,
2087946,
2832067,
34268528,
2303697,
14645468,
216565318,
6975761,
69625582,
33860,
11263077,
10738958,
15946876,
4207083,
6453553,
16604026,
1293119,
433285,
38964,
43053054,
30366036,
1148130,
11530580,
12626950,
54045420,
163046161,
77142,
38041754,
622137,
3301000,
44269594,
11333483,
9746117,
17373662,
50339443,
7044636,
10269417,
2657637,
5942089,
10101694,
28608710,
2125268,
25876380,
2172579,
23310715,
20321378,
8082366,
30417856,
1920922,
331595460,
37589262,
127575529,
390353,
4246439,
28515829,
8776109,
100388073,
29161922,
4525696,
1355986,
2347706,
825,
326000,
10000,
4490,
25368867,
889953,
4950828,
26969307,
108116615,
21803000,
389482,
23568378,
126264931,
361313,
97625,
117606,
58791,
1394973,
112003,
110589,
287025,
182790,
71808,
97118,
52834,
2948279,
1265711,
850886,
215056,
549935,
502653,
5703569,
104494,
197097,
669823,
11646,
530953,
12581,
113815,
299882,
18008,
1641172
],
"xaxis": "x",
"yaxis": "y"
}
],
"layout": {
"barmode": "relative",
"legend": {
"tracegroupgap": 0
},
"template": {
"data": {
"bar": [
{
"error_x": {
"color": "#2a3f5f"
},
"error_y": {
"color": "#2a3f5f"
},
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "bar"
}
],
"barpolar": [
{
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "barpolar"
}
],
"carpet": [
{
"aaxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"baxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"type": "carpet"
}
],
"choropleth": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "choropleth"
}
],
"contour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "contour"
}
],
"contourcarpet": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "contourcarpet"
}
],
"heatmap": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmap"
}
],
"heatmapgl": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmapgl"
}
],
"histogram": [
{
"marker": {
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "histogram"
}
],
"histogram2d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2d"
}
],
"histogram2dcontour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2dcontour"
}
],
"mesh3d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "mesh3d"
}
],
"parcoords": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "parcoords"
}
],
"pie": [
{
"automargin": true,
"type": "pie"
}
],
"scatter": [
{
"fillpattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
},
"type": "scatter"
}
],
"scatter3d": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter3d"
}
],
"scattercarpet": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattercarpet"
}
],
"scattergeo": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergeo"
}
],
"scattergl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergl"
}
],
"scattermapbox": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattermapbox"
}
],
"scatterpolar": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolar"
}
],
"scatterpolargl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolargl"
}
],
"scatterternary": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterternary"
}
],
"surface": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "surface"
}
],
"table": [
{
"cells": {
"fill": {
"color": "#EBF0F8"
},
"line": {
"color": "white"
}
},
"header": {
"fill": {
"color": "#C8D4E3"
},
"line": {
"color": "white"
}
},
"type": "table"
}
]
},
"layout": {
"annotationdefaults": {
"arrowcolor": "#2a3f5f",
"arrowhead": 0,
"arrowwidth": 1
},
"autotypenumbers": "strict",
"coloraxis": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"colorscale": {
"diverging": [
[
0,
"#8e0152"
],
[
0.1,
"#c51b7d"
],
[
0.2,
"#de77ae"
],
[
0.3,
"#f1b6da"
],
[
0.4,
"#fde0ef"
],
[
0.5,
"#f7f7f7"
],
[
0.6,
"#e6f5d0"
],
[
0.7,
"#b8e186"
],
[
0.8,
"#7fbc41"
],
[
0.9,
"#4d9221"
],
[
1,
"#276419"
]
],
"sequential": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"sequentialminus": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
]
},
"colorway": [
"#636efa",
"#EF553B",
"#00cc96",
"#ab63fa",
"#FFA15A",
"#19d3f3",
"#FF6692",
"#B6E880",
"#FF97FF",
"#FECB52"
],
"font": {
"color": "#2a3f5f"
},
"geo": {
"bgcolor": "white",
"lakecolor": "white",
"landcolor": "#E5ECF6",
"showlakes": true,
"showland": true,
"subunitcolor": "white"
},
"hoverlabel": {
"align": "left"
},
"hovermode": "closest",
"mapbox": {
"style": "light"
},
"paper_bgcolor": "white",
"plot_bgcolor": "#E5ECF6",
"polar": {
"angularaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"radialaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"scene": {
"xaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"yaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"zaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
}
},
"shapedefaults": {
"line": {
"color": "#2a3f5f"
}
},
"ternary": {
"aaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"baxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"caxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"title": {
"x": 0.05
},
"xaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
},
"yaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
}
}
},
"title": {
"text": "Distribución de la población estimada por país"
},
"xaxis": {
"anchor": "y",
"domain": [
0,
1
],
"gridcolor": "lightgray",
"gridwidth": 0.5,
"showgrid": true,
"tickformat": ",",
"title": {
"text": "Población estimada (habitantes)"
}
},
"yaxis": {
"anchor": "x",
"domain": [
0,
1
],
"gridcolor": "lightgray",
"gridwidth": 0.5,
"showgrid": true,
"tickformat": ",",
"title": {
"text": "Cantidad de países"
}
}
}
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Para evitar países con población menor que 0\n",
"paises_seleccionados = paises[paises['POP_EST'] >= 0]\n",
"\n",
"# Creación del histograma\n",
"fig = px.histogram(\n",
" paises_seleccionados,\n",
" x='POP_EST',\n",
" nbins=10, # Cantidad de bins\n",
" title='Distribución de la población estimada por país',\n",
" labels={\n",
" 'POP_EST': 'Población estimada (habitantes)'\n",
" }\n",
")\n",
"\n",
"# Atributos globales de la figura\n",
"fig.update_layout(\n",
" xaxis_tickformat=',',\n",
" yaxis_tickformat=',',\n",
" xaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray'),\n",
" yaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray'),\n",
" yaxis_title='Cantidad de países'\n",
")\n",
"\n",
"# Mostrar el gráfico\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"id": "37e4b753",
"metadata": {},
"source": [
"### Gráficos de caja"
]
},
{
"cell_type": "markdown",
"id": "8f2b0181",
"metadata": {},
"source": [
"Un [gráfico de caja (*boxplot*)](https://es.wikipedia.org/wiki/Diagrama_de_caja) muestra información de una variable numérica a través de su [mediana](https://es.wikipedia.org/wiki/Mediana), sus [cuartiles](https://es.wikipedia.org/wiki/Cuartiles) (Q1, Q2 y Q3) y sus [valores atípicos](https://es.wikipedia.org/wiki/Valor_at%C3%ADpico). Se acostumbra combinarlo con variables categóricas para agrupar los datos y facilitar la comparación entre categorías. Es especialmente útil para identificar la dispersión, la mediana, la variabilidad y posibles valores atípicos en los datos.\n",
"\n",
"En `plotly.express`, los gráficos de caja se implementan con la función [`plotly.express.box()`](https://plotly.github.io/plotly.py-docs/generated/plotly.express.box.html). Se recomienda leer el [tutorial](https://plotly.com/python/box-plots/).\n",
"\n",
"La {numref}`figure-diagrama-caja` muestra los componentes de un gráfico de caja.\n",
"\n",
"```{figure} img/diagrama-caja.jpg\n",
":name: figure-diagrama-caja\n",
"\n",
"Componentes de un diagrama de caja. Imagen de [Onkel Dagobert](https://commons.wikimedia.org/wiki/File:Diagrama_de_caja.jpg)..\n",
"```\n",
"\n",
"El siguiente bloque de código genera un gráfico de caja para la esperanza de vida al nacer."
]
},
{
"cell_type": "code",
"execution_count": 226,
"id": "3373dd2f",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"alignmentgroup": "True",
"customdata": [
[
"Indonesia"
],
[
"Malaysia"
],
[
"Chile"
],
[
"Bolivia"
],
[
"Peru"
],
[
"Argentina"
],
[
"United Kingdom"
],
[
"Cyprus"
],
[
"India"
],
[
"China"
],
[
"Israel"
],
[
"Lebanon"
],
[
"Ethiopia"
],
[
"S. Sudan"
],
[
"Somalia"
],
[
"Kenya"
],
[
"Malawi"
],
[
"Tanzania"
],
[
"Syria"
],
[
"Somaliland"
],
[
"France"
],
[
"Suriname"
],
[
"Guyana"
],
[
"South Korea"
],
[
"North Korea"
],
[
"Morocco"
],
[
"W. Sahara"
],
[
"Costa Rica"
],
[
"Nicaragua"
],
[
"Congo"
],
[
"Dem. Rep. Congo"
],
[
"Bhutan"
],
[
"Ukraine"
],
[
"Belarus"
],
[
"Namibia"
],
[
"South Africa"
],
[
"Netherlands"
],
[
"Oman"
],
[
"Uzbekistan"
],
[
"Kazakhstan"
],
[
"Tajikistan"
],
[
"Lithuania"
],
[
"Brazil"
],
[
"Uruguay"
],
[
"Mongolia"
],
[
"Russia"
],
[
"Czechia"
],
[
"Germany"
],
[
"Estonia"
],
[
"Latvia"
],
[
"Norway"
],
[
"Sweden"
],
[
"Finland"
],
[
"Vietnam"
],
[
"Cambodia"
],
[
"Luxembourg"
],
[
"United Arab Emirates"
],
[
"Belgium"
],
[
"Georgia"
],
[
"North Macedonia"
],
[
"Albania"
],
[
"Azerbaijan"
],
[
"Kosovo"
],
[
"Turkey"
],
[
"Spain"
],
[
"Laos"
],
[
"Kyrgyzstan"
],
[
"Armenia"
],
[
"Denmark"
],
[
"Libya"
],
[
"Tunisia"
],
[
"Romania"
],
[
"Hungary"
],
[
"Slovakia"
],
[
"Poland"
],
[
"Ireland"
],
[
"Greece"
],
[
"Zambia"
],
[
"Sierra Leone"
],
[
"Guinea"
],
[
"Liberia"
],
[
"Central African Rep."
],
[
"Sudan"
],
[
"Djibouti"
],
[
"Eritrea"
],
[
"Austria"
],
[
"Iraq"
],
[
"Italy"
],
[
"Switzerland"
],
[
"Iran"
],
[
"Liechtenstein"
],
[
"Côte d'Ivoire"
],
[
"Serbia"
],
[
"Mali"
],
[
"Senegal"
],
[
"Nigeria"
],
[
"Benin"
],
[
"Angola"
],
[
"Croatia"
],
[
"Slovenia"
],
[
"Qatar"
],
[
"Saudi Arabia"
],
[
"Botswana"
],
[
"Zimbabwe"
],
[
"Pakistan"
],
[
"Bulgaria"
],
[
"Thailand"
],
[
"San Marino"
],
[
"Haiti"
],
[
"Dominican Rep."
],
[
"Chad"
],
[
"Kuwait"
],
[
"El Salvador"
],
[
"Guatemala"
],
[
"Timor-Leste"
],
[
"Brunei"
],
[
"Monaco"
],
[
"Algeria"
],
[
"Mozambique"
],
[
"eSwatini"
],
[
"Burundi"
],
[
"Rwanda"
],
[
"Myanmar"
],
[
"Bangladesh"
],
[
"Andorra"
],
[
"Afghanistan"
],
[
"Montenegro"
],
[
"Bosnia and Herz."
],
[
"Uganda"
],
[
"Cuba"
],
[
"Honduras"
],
[
"Ecuador"
],
[
"Colombia"
],
[
"Paraguay"
],
[
"Portugal"
],
[
"Moldova"
],
[
"Turkmenistan"
],
[
"Jordan"
],
[
"Nepal"
],
[
"Lesotho"
],
[
"Cameroon"
],
[
"Gabon"
],
[
"Niger"
],
[
"Burkina Faso"
],
[
"Togo"
],
[
"Ghana"
],
[
"Guinea-Bissau"
],
[
"United States of America"
],
[
"Canada"
],
[
"Mexico"
],
[
"Belize"
],
[
"Panama"
],
[
"Venezuela"
],
[
"Papua New Guinea"
],
[
"Egypt"
],
[
"Yemen"
],
[
"Mauritania"
],
[
"Eq. Guinea"
],
[
"Gambia"
],
[
"Vatican"
],
[
"N. Cyprus"
],
[
"Cyprus U.N. Buffer Zone"
],
[
"Antarctica"
],
[
"Australia"
],
[
"Fiji"
],
[
"New Zealand"
],
[
"Madagascar"
],
[
"Philippines"
],
[
"Sri Lanka"
],
[
"Bahamas"
],
[
"Taiwan"
],
[
"Japan"
],
[
"Iceland"
],
[
"Seychelles"
],
[
"Kiribati"
],
[
"Marshall Is."
],
[
"Trinidad and Tobago"
],
[
"Grenada"
],
[
"St. Vin. and Gren."
],
[
"Barbados"
],
[
"Saint Lucia"
],
[
"Dominica"
],
[
"Antigua and Barb."
],
[
"St. Kitts and Nevis"
],
[
"Jamaica"
],
[
"Mauritius"
],
[
"Comoros"
],
[
"São Tomé and Principe"
],
[
"Cabo Verde"
],
[
"Malta"
],
[
"Singapore"
],
[
"Tonga"
],
[
"Samoa"
],
[
"Solomon Is."
],
[
"Tuvalu"
],
[
"Maldives"
],
[
"Nauru"
],
[
"Micronesia"
],
[
"Vanuatu"
],
[
"Palau"
],
[
"Bahrain"
]
],
"hovertemplate": "Esperanza de vida al nacer (años)=%{y}
NAME=%{customdata[0]}",
"legendgroup": "",
"marker": {
"color": "#636efa"
},
"name": "",
"notched": false,
"offsetgroup": "",
"orientation": "v",
"showlegend": false,
"type": "box",
"x0": " ",
"xaxis": "x",
"y": [
68.25,
76.26,
79.519,
64.928,
73.385,
76.064,
82.0585365853659,
81.889,
67.744,
78.587,
82.7,
74.416,
65.645,
55.567,
56.107,
62.055,
62.898,
66.782,
72.3,
56.107,
82.2292682926829,
70.289,
65.989,
82.6804878048781,
73.578,
74.973,
null,
77.32,
74.615,
63.053,
59.743,
72.229,
68.5879268292683,
73.1114634146342,
58.059,
61.48,
81.7146341463415,
73.935,
71.674,
74.44,
71.288,
75.7926829268293,
73.425,
78,
72.667,
72.5456097560976,
79.0292682926829,
80.7056097560976,
77.9439024390244,
74.580487804878,
82.5609756097561,
83.109756097561,
81.1878048780488,
74.58,
69.896,
83.0463414634146,
79.196,
81.6975609756098,
71.587,
74.4248780487805,
76.833,
73.488,
75.4821951219512,
78.475,
83.0829268292683,
68.999,
72.0487804878049,
73.372,
81.3048780487805,
72.151,
74.263,
75.3048780487805,
76.0170731707317,
77.0658536585366,
77.3048780487805,
83.0560975609756,
80.6365853658537,
61.803,
60.411,
58.985,
61.1,
54.477,
null,
62.859,
66.604,
81.0926829268293,
71.336,
82.9,
83.4536585365854,
74.556,
84.319512195122,
58.916,
75.4821951219512,
59.417,
67.913,
53.633,
59.954,
61.929,
77.5756097560976,
81.2829268292683,
81.559,
77.905,
65.913,
59.391,
66.431,
74.3609756097561,
79.68,
null,
63.728,
74.17,
52.997,
80.264,
71.475,
68.674,
69.056,
74.551,
null,
77.129,
59.625,
56.36,
61.977,
67.129,
67.256,
73.698,
null,
62.879,
76.1878048780488,
75.293,
63.638,
78.155,
70.728,
77.894,
73.659,
70.475,
null,
68.621,
69.41,
74.215,
70.484,
53.036,
60.958,
65.694,
62.08,
59.766,
61.588,
63.945,
59.861,
77.4341463414634,
81.2956097560976,
74.832,
70.962,
76.826,
71.105,
null,
70.159,
63.72,
64.691,
61.19,
62.906,
null,
81.889,
81.889,
null,
83.2,
68.312,
82.7609756097561,
65.23,
72.187,
76.61,
74.358,
null,
83.9963414634146,
82.1707317073171,
73.8487804878049,
67.661,
65.146,
74.708,
75.335,
68.972,
77.706,
71.294,
72.981,
79.236,
72.027,
70.629,
73.5146341463415,
63.68,
68.794,
74.722,
82.7,
82.8951219512195,
71.27,
72.598,
70.742,
64.854,
80.839,
64.014,
70.925,
70.492,
null,
79.246
],
"y0": " ",
"yaxis": "y"
}
],
"layout": {
"boxmode": "group",
"legend": {
"tracegroupgap": 0
},
"template": {
"data": {
"bar": [
{
"error_x": {
"color": "#2a3f5f"
},
"error_y": {
"color": "#2a3f5f"
},
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "bar"
}
],
"barpolar": [
{
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "barpolar"
}
],
"carpet": [
{
"aaxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"baxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"type": "carpet"
}
],
"choropleth": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "choropleth"
}
],
"contour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "contour"
}
],
"contourcarpet": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "contourcarpet"
}
],
"heatmap": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmap"
}
],
"heatmapgl": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmapgl"
}
],
"histogram": [
{
"marker": {
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "histogram"
}
],
"histogram2d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2d"
}
],
"histogram2dcontour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2dcontour"
}
],
"mesh3d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "mesh3d"
}
],
"parcoords": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "parcoords"
}
],
"pie": [
{
"automargin": true,
"type": "pie"
}
],
"scatter": [
{
"fillpattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
},
"type": "scatter"
}
],
"scatter3d": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter3d"
}
],
"scattercarpet": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattercarpet"
}
],
"scattergeo": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergeo"
}
],
"scattergl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergl"
}
],
"scattermapbox": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattermapbox"
}
],
"scatterpolar": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolar"
}
],
"scatterpolargl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolargl"
}
],
"scatterternary": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterternary"
}
],
"surface": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "surface"
}
],
"table": [
{
"cells": {
"fill": {
"color": "#EBF0F8"
},
"line": {
"color": "white"
}
},
"header": {
"fill": {
"color": "#C8D4E3"
},
"line": {
"color": "white"
}
},
"type": "table"
}
]
},
"layout": {
"annotationdefaults": {
"arrowcolor": "#2a3f5f",
"arrowhead": 0,
"arrowwidth": 1
},
"autotypenumbers": "strict",
"coloraxis": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"colorscale": {
"diverging": [
[
0,
"#8e0152"
],
[
0.1,
"#c51b7d"
],
[
0.2,
"#de77ae"
],
[
0.3,
"#f1b6da"
],
[
0.4,
"#fde0ef"
],
[
0.5,
"#f7f7f7"
],
[
0.6,
"#e6f5d0"
],
[
0.7,
"#b8e186"
],
[
0.8,
"#7fbc41"
],
[
0.9,
"#4d9221"
],
[
1,
"#276419"
]
],
"sequential": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"sequentialminus": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
]
},
"colorway": [
"#636efa",
"#EF553B",
"#00cc96",
"#ab63fa",
"#FFA15A",
"#19d3f3",
"#FF6692",
"#B6E880",
"#FF97FF",
"#FECB52"
],
"font": {
"color": "#2a3f5f"
},
"geo": {
"bgcolor": "white",
"lakecolor": "white",
"landcolor": "#E5ECF6",
"showlakes": true,
"showland": true,
"subunitcolor": "white"
},
"hoverlabel": {
"align": "left"
},
"hovermode": "closest",
"mapbox": {
"style": "light"
},
"paper_bgcolor": "white",
"plot_bgcolor": "#E5ECF6",
"polar": {
"angularaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"radialaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"scene": {
"xaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"yaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"zaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
}
},
"shapedefaults": {
"line": {
"color": "#2a3f5f"
}
},
"ternary": {
"aaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"baxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"caxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"title": {
"x": 0.05
},
"xaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
},
"yaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
}
}
},
"title": {
"text": "Esperanza de vida al nacer en países"
},
"xaxis": {
"anchor": "y",
"domain": [
0,
1
]
},
"yaxis": {
"anchor": "x",
"domain": [
0,
1
],
"title": {
"text": "Esperanza de vida al nacer (años)"
}
}
}
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Generación del diagrama de caja\n",
"fig = px.box(\n",
" paises, \n",
" y='LIFE_EXPECTANCY', \n",
" title='Esperanza de vida al nacer en países',\n",
" labels={'LIFE_EXPECTANCY': 'Esperanza de vida al nacer (años)'},\n",
" hover_data=['NAME']\n",
")\n",
"\n",
"# Despliegue del gráfico\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"id": "db9a9d5e",
"metadata": {},
"source": [
"El siguiente bloque muestra el mismo gráfico categorizado por continente."
]
},
{
"cell_type": "code",
"execution_count": 227,
"id": "38926a72",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"alignmentgroup": "True",
"customdata": [
[
"Indonesia"
],
[
"Malaysia"
],
[
"Chile"
],
[
"Bolivia"
],
[
"Peru"
],
[
"Argentina"
],
[
"United Kingdom"
],
[
"Cyprus"
],
[
"India"
],
[
"China"
],
[
"Israel"
],
[
"Lebanon"
],
[
"Ethiopia"
],
[
"S. Sudan"
],
[
"Somalia"
],
[
"Kenya"
],
[
"Malawi"
],
[
"Tanzania"
],
[
"Syria"
],
[
"Somaliland"
],
[
"France"
],
[
"Suriname"
],
[
"Guyana"
],
[
"South Korea"
],
[
"North Korea"
],
[
"Morocco"
],
[
"W. Sahara"
],
[
"Costa Rica"
],
[
"Nicaragua"
],
[
"Congo"
],
[
"Dem. Rep. Congo"
],
[
"Bhutan"
],
[
"Ukraine"
],
[
"Belarus"
],
[
"Namibia"
],
[
"South Africa"
],
[
"Netherlands"
],
[
"Oman"
],
[
"Uzbekistan"
],
[
"Kazakhstan"
],
[
"Tajikistan"
],
[
"Lithuania"
],
[
"Brazil"
],
[
"Uruguay"
],
[
"Mongolia"
],
[
"Russia"
],
[
"Czechia"
],
[
"Germany"
],
[
"Estonia"
],
[
"Latvia"
],
[
"Norway"
],
[
"Sweden"
],
[
"Finland"
],
[
"Vietnam"
],
[
"Cambodia"
],
[
"Luxembourg"
],
[
"United Arab Emirates"
],
[
"Belgium"
],
[
"Georgia"
],
[
"North Macedonia"
],
[
"Albania"
],
[
"Azerbaijan"
],
[
"Kosovo"
],
[
"Turkey"
],
[
"Spain"
],
[
"Laos"
],
[
"Kyrgyzstan"
],
[
"Armenia"
],
[
"Denmark"
],
[
"Libya"
],
[
"Tunisia"
],
[
"Romania"
],
[
"Hungary"
],
[
"Slovakia"
],
[
"Poland"
],
[
"Ireland"
],
[
"Greece"
],
[
"Zambia"
],
[
"Sierra Leone"
],
[
"Guinea"
],
[
"Liberia"
],
[
"Central African Rep."
],
[
"Sudan"
],
[
"Djibouti"
],
[
"Eritrea"
],
[
"Austria"
],
[
"Iraq"
],
[
"Italy"
],
[
"Switzerland"
],
[
"Iran"
],
[
"Liechtenstein"
],
[
"Côte d'Ivoire"
],
[
"Serbia"
],
[
"Mali"
],
[
"Senegal"
],
[
"Nigeria"
],
[
"Benin"
],
[
"Angola"
],
[
"Croatia"
],
[
"Slovenia"
],
[
"Qatar"
],
[
"Saudi Arabia"
],
[
"Botswana"
],
[
"Zimbabwe"
],
[
"Pakistan"
],
[
"Bulgaria"
],
[
"Thailand"
],
[
"San Marino"
],
[
"Haiti"
],
[
"Dominican Rep."
],
[
"Chad"
],
[
"Kuwait"
],
[
"El Salvador"
],
[
"Guatemala"
],
[
"Timor-Leste"
],
[
"Brunei"
],
[
"Monaco"
],
[
"Algeria"
],
[
"Mozambique"
],
[
"eSwatini"
],
[
"Burundi"
],
[
"Rwanda"
],
[
"Myanmar"
],
[
"Bangladesh"
],
[
"Andorra"
],
[
"Afghanistan"
],
[
"Montenegro"
],
[
"Bosnia and Herz."
],
[
"Uganda"
],
[
"Cuba"
],
[
"Honduras"
],
[
"Ecuador"
],
[
"Colombia"
],
[
"Paraguay"
],
[
"Portugal"
],
[
"Moldova"
],
[
"Turkmenistan"
],
[
"Jordan"
],
[
"Nepal"
],
[
"Lesotho"
],
[
"Cameroon"
],
[
"Gabon"
],
[
"Niger"
],
[
"Burkina Faso"
],
[
"Togo"
],
[
"Ghana"
],
[
"Guinea-Bissau"
],
[
"United States of America"
],
[
"Canada"
],
[
"Mexico"
],
[
"Belize"
],
[
"Panama"
],
[
"Venezuela"
],
[
"Papua New Guinea"
],
[
"Egypt"
],
[
"Yemen"
],
[
"Mauritania"
],
[
"Eq. Guinea"
],
[
"Gambia"
],
[
"Vatican"
],
[
"N. Cyprus"
],
[
"Cyprus U.N. Buffer Zone"
],
[
"Antarctica"
],
[
"Australia"
],
[
"Fiji"
],
[
"New Zealand"
],
[
"Madagascar"
],
[
"Philippines"
],
[
"Sri Lanka"
],
[
"Bahamas"
],
[
"Taiwan"
],
[
"Japan"
],
[
"Iceland"
],
[
"Seychelles"
],
[
"Kiribati"
],
[
"Marshall Is."
],
[
"Trinidad and Tobago"
],
[
"Grenada"
],
[
"St. Vin. and Gren."
],
[
"Barbados"
],
[
"Saint Lucia"
],
[
"Dominica"
],
[
"Antigua and Barb."
],
[
"St. Kitts and Nevis"
],
[
"Jamaica"
],
[
"Mauritius"
],
[
"Comoros"
],
[
"São Tomé and Principe"
],
[
"Cabo Verde"
],
[
"Malta"
],
[
"Singapore"
],
[
"Tonga"
],
[
"Samoa"
],
[
"Solomon Is."
],
[
"Tuvalu"
],
[
"Maldives"
],
[
"Nauru"
],
[
"Micronesia"
],
[
"Vanuatu"
],
[
"Palau"
],
[
"Bahrain"
]
],
"hovertemplate": "Continente=%{x}
Esperanza de vida al nacer=%{y:.2f}
País=%{customdata[0]}",
"legendgroup": "",
"marker": {
"color": "#636efa"
},
"name": "",
"notched": false,
"offsetgroup": "",
"orientation": "v",
"showlegend": false,
"type": "box",
"x": [
"Asia",
"Asia",
"South America",
"South America",
"South America",
"South America",
"Europe",
"Asia",
"Asia",
"Asia",
"Asia",
"Asia",
"Africa",
"Africa",
"Africa",
"Africa",
"Africa",
"Africa",
"Asia",
"Africa",
"Europe",
"South America",
"South America",
"Asia",
"Asia",
"Africa",
"Africa",
"North America",
"North America",
"Africa",
"Africa",
"Asia",
"Europe",
"Europe",
"Africa",
"Africa",
"Europe",
"Asia",
"Asia",
"Asia",
"Asia",
"Europe",
"South America",
"South America",
"Asia",
"Europe",
"Europe",
"Europe",
"Europe",
"Europe",
"Europe",
"Europe",
"Europe",
"Asia",
"Asia",
"Europe",
"Asia",
"Europe",
"Asia",
"Europe",
"Europe",
"Asia",
"Europe",
"Asia",
"Europe",
"Asia",
"Asia",
"Asia",
"Europe",
"Africa",
"Africa",
"Europe",
"Europe",
"Europe",
"Europe",
"Europe",
"Europe",
"Africa",
"Africa",
"Africa",
"Africa",
"Africa",
"Africa",
"Africa",
"Africa",
"Europe",
"Asia",
"Europe",
"Europe",
"Asia",
"Europe",
"Africa",
"Europe",
"Africa",
"Africa",
"Africa",
"Africa",
"Africa",
"Europe",
"Europe",
"Asia",
"Asia",
"Africa",
"Africa",
"Asia",
"Europe",
"Asia",
"Europe",
"North America",
"North America",
"Africa",
"Asia",
"North America",
"North America",
"Asia",
"Asia",
"Europe",
"Africa",
"Africa",
"Africa",
"Africa",
"Africa",
"Asia",
"Asia",
"Europe",
"Asia",
"Europe",
"Europe",
"Africa",
"North America",
"North America",
"South America",
"South America",
"South America",
"Europe",
"Europe",
"Asia",
"Asia",
"Asia",
"Africa",
"Africa",
"Africa",
"Africa",
"Africa",
"Africa",
"Africa",
"Africa",
"North America",
"North America",
"North America",
"North America",
"North America",
"South America",
"Oceania",
"Africa",
"Asia",
"Africa",
"Africa",
"Africa",
"Europe",
"Asia",
"Asia",
"Antarctica",
"Oceania",
"Oceania",
"Oceania",
"Africa",
"Asia",
"Asia",
"North America",
"Asia",
"Asia",
"Europe",
"Seven seas (open ocean)",
"Oceania",
"Oceania",
"North America",
"North America",
"North America",
"North America",
"North America",
"North America",
"North America",
"North America",
"North America",
"Seven seas (open ocean)",
"Africa",
"Africa",
"Africa",
"Europe",
"Asia",
"Oceania",
"Oceania",
"Oceania",
"Oceania",
"Seven seas (open ocean)",
"Oceania",
"Oceania",
"Oceania",
"Oceania",
"Asia"
],
"x0": " ",
"xaxis": "x",
"y": [
68.25,
76.26,
79.519,
64.928,
73.385,
76.064,
82.0585365853659,
81.889,
67.744,
78.587,
82.7,
74.416,
65.645,
55.567,
56.107,
62.055,
62.898,
66.782,
72.3,
56.107,
82.2292682926829,
70.289,
65.989,
82.6804878048781,
73.578,
74.973,
null,
77.32,
74.615,
63.053,
59.743,
72.229,
68.5879268292683,
73.1114634146342,
58.059,
61.48,
81.7146341463415,
73.935,
71.674,
74.44,
71.288,
75.7926829268293,
73.425,
78,
72.667,
72.5456097560976,
79.0292682926829,
80.7056097560976,
77.9439024390244,
74.580487804878,
82.5609756097561,
83.109756097561,
81.1878048780488,
74.58,
69.896,
83.0463414634146,
79.196,
81.6975609756098,
71.587,
74.4248780487805,
76.833,
73.488,
75.4821951219512,
78.475,
83.0829268292683,
68.999,
72.0487804878049,
73.372,
81.3048780487805,
72.151,
74.263,
75.3048780487805,
76.0170731707317,
77.0658536585366,
77.3048780487805,
83.0560975609756,
80.6365853658537,
61.803,
60.411,
58.985,
61.1,
54.477,
null,
62.859,
66.604,
81.0926829268293,
71.336,
82.9,
83.4536585365854,
74.556,
84.319512195122,
58.916,
75.4821951219512,
59.417,
67.913,
53.633,
59.954,
61.929,
77.5756097560976,
81.2829268292683,
81.559,
77.905,
65.913,
59.391,
66.431,
74.3609756097561,
79.68,
null,
63.728,
74.17,
52.997,
80.264,
71.475,
68.674,
69.056,
74.551,
null,
77.129,
59.625,
56.36,
61.977,
67.129,
67.256,
73.698,
null,
62.879,
76.1878048780488,
75.293,
63.638,
78.155,
70.728,
77.894,
73.659,
70.475,
null,
68.621,
69.41,
74.215,
70.484,
53.036,
60.958,
65.694,
62.08,
59.766,
61.588,
63.945,
59.861,
77.4341463414634,
81.2956097560976,
74.832,
70.962,
76.826,
71.105,
null,
70.159,
63.72,
64.691,
61.19,
62.906,
null,
81.889,
81.889,
null,
83.2,
68.312,
82.7609756097561,
65.23,
72.187,
76.61,
74.358,
null,
83.9963414634146,
82.1707317073171,
73.8487804878049,
67.661,
65.146,
74.708,
75.335,
68.972,
77.706,
71.294,
72.981,
79.236,
72.027,
70.629,
73.5146341463415,
63.68,
68.794,
74.722,
82.7,
82.8951219512195,
71.27,
72.598,
70.742,
64.854,
80.839,
64.014,
70.925,
70.492,
null,
79.246
],
"y0": " ",
"yaxis": "y"
}
],
"layout": {
"boxmode": "group",
"legend": {
"tracegroupgap": 0
},
"template": {
"data": {
"bar": [
{
"error_x": {
"color": "#2a3f5f"
},
"error_y": {
"color": "#2a3f5f"
},
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "bar"
}
],
"barpolar": [
{
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "barpolar"
}
],
"carpet": [
{
"aaxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"baxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"type": "carpet"
}
],
"choropleth": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "choropleth"
}
],
"contour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "contour"
}
],
"contourcarpet": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "contourcarpet"
}
],
"heatmap": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmap"
}
],
"heatmapgl": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmapgl"
}
],
"histogram": [
{
"marker": {
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "histogram"
}
],
"histogram2d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2d"
}
],
"histogram2dcontour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2dcontour"
}
],
"mesh3d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "mesh3d"
}
],
"parcoords": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "parcoords"
}
],
"pie": [
{
"automargin": true,
"type": "pie"
}
],
"scatter": [
{
"fillpattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
},
"type": "scatter"
}
],
"scatter3d": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter3d"
}
],
"scattercarpet": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattercarpet"
}
],
"scattergeo": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergeo"
}
],
"scattergl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergl"
}
],
"scattermapbox": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattermapbox"
}
],
"scatterpolar": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolar"
}
],
"scatterpolargl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolargl"
}
],
"scatterternary": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterternary"
}
],
"surface": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "surface"
}
],
"table": [
{
"cells": {
"fill": {
"color": "#EBF0F8"
},
"line": {
"color": "white"
}
},
"header": {
"fill": {
"color": "#C8D4E3"
},
"line": {
"color": "white"
}
},
"type": "table"
}
]
},
"layout": {
"annotationdefaults": {
"arrowcolor": "#2a3f5f",
"arrowhead": 0,
"arrowwidth": 1
},
"autotypenumbers": "strict",
"coloraxis": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"colorscale": {
"diverging": [
[
0,
"#8e0152"
],
[
0.1,
"#c51b7d"
],
[
0.2,
"#de77ae"
],
[
0.3,
"#f1b6da"
],
[
0.4,
"#fde0ef"
],
[
0.5,
"#f7f7f7"
],
[
0.6,
"#e6f5d0"
],
[
0.7,
"#b8e186"
],
[
0.8,
"#7fbc41"
],
[
0.9,
"#4d9221"
],
[
1,
"#276419"
]
],
"sequential": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"sequentialminus": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
]
},
"colorway": [
"#636efa",
"#EF553B",
"#00cc96",
"#ab63fa",
"#FFA15A",
"#19d3f3",
"#FF6692",
"#B6E880",
"#FF97FF",
"#FECB52"
],
"font": {
"color": "#2a3f5f"
},
"geo": {
"bgcolor": "white",
"lakecolor": "white",
"landcolor": "#E5ECF6",
"showlakes": true,
"showland": true,
"subunitcolor": "white"
},
"hoverlabel": {
"align": "left"
},
"hovermode": "closest",
"mapbox": {
"style": "light"
},
"paper_bgcolor": "white",
"plot_bgcolor": "#E5ECF6",
"polar": {
"angularaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"radialaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"scene": {
"xaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"yaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"zaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
}
},
"shapedefaults": {
"line": {
"color": "#2a3f5f"
}
},
"ternary": {
"aaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"baxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"caxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"title": {
"x": 0.05
},
"xaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
},
"yaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
}
}
},
"title": {
"text": "Esperanza de vida al nacer por continente"
},
"xaxis": {
"anchor": "y",
"domain": [
0,
1
],
"title": {
"text": "Continente"
}
},
"yaxis": {
"anchor": "x",
"domain": [
0,
1
],
"title": {
"text": "Esperanza de vida al nacer"
}
}
}
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Generación del gráfico\n",
"fig = px.box(\n",
" paises,\n",
" x='CONTINENT',\n",
" y='LIFE_EXPECTANCY',\n",
" title='Esperanza de vida al nacer por continente',\n",
" labels={\n",
" 'LIFE_EXPECTANCY': 'Esperanza de vida al nacer',\n",
" 'CONTINENT': 'Continente',\n",
" 'NAME': 'País'\n",
" },\n",
" hover_data={\n",
" 'NAME': True,\n",
" 'LIFE_EXPECTANCY': ':.2f', # formato con dos decimales\n",
" }\n",
")\n",
"\n",
"# Despliegue del gráfico\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"id": "62718e75",
"metadata": {},
"source": [
"#### Ejercicios\n",
"\n",
"1. Pruebe el argumento `points='all'` en `px.bo()` y observe el resultado.\n",
"2. Programe un gráfico de caja para la variable mortalidad infantil categorizada por regiones del Banco Mundial."
]
},
{
"cell_type": "markdown",
"id": "58a032ee",
"metadata": {},
"source": [
"### Otros"
]
},
{
"cell_type": "markdown",
"id": "c95b9d11",
"metadata": {},
"source": [
"#### Climogramas"
]
},
{
"cell_type": "markdown",
"id": "3c46be5d",
"metadata": {},
"source": [
"Un [climograma](https://es.wikipedia.org/wiki/Climograma) es un gráfico estadístico que combina las variables climáticas de temperatura y precitipación a lo largo de un período de tiempo, para una región o lugar específico. Permite visualizar las condiciones climáticas de un área, facilitando el análisis de patrones estacionales y tendencias climáticas.\n",
"\n",
"Un climograma puede tomar varias formas. Una de las más usuales es la de un gráfico de barras combinado con un gráfico de líneas. Las barras representan las unidades de tiempo (ej. meses) y su longitud el valor de una de las variables (ej. precipitación), mientras que la línea muestra los valores de la otra variable (ej. temperatura).\n",
"\n",
"En los siguientes bloques de código se genera un climograma con [datos de temperatura y precipitación de ERA5](https://cds.climate.copernicus.eu/datasets/reanalysis-era5-land-monthly-means?tab=overview). [ERA5](https://www.ecmwf.int/en/forecasts/dataset/ecmwf-reanalysis-v5) es el quinto conjunto de datos de reanálisis atmosférico global producido por el [Centro Europeo de Predicción a Medio Plazo (ECMWF)](https://www.ecmwf.int/). Es una reconstrucción del clima de la Tierra, desde 1950 hasta la actualidad.\n",
"\n",
"Los datos que se utilizan en el climograma corresponden a los promedios mensuales de temperatura a 2 m de la superficie y de precipitación total para todo el territorio de Costa Rica, entre los años 2004 y 2023. Se utilizó una cuadrícula de 0.1 x 0.1 grados decimales.\n",
"\n",
"En este gráfico se utilizará el módulo `plotly.graph_objects`."
]
},
{
"cell_type": "code",
"execution_count": 228,
"id": "d313401f",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" mes | \n",
" temperatura | \n",
" precipitacion | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Enero | \n",
" 22.15 | \n",
" 3.98 | \n",
"
\n",
" \n",
" 1 | \n",
" Febrero | \n",
" 22.60 | \n",
" 2.92 | \n",
"
\n",
" \n",
" 2 | \n",
" Marzo | \n",
" 23.16 | \n",
" 2.73 | \n",
"
\n",
" \n",
" 3 | \n",
" Abril | \n",
" 23.67 | \n",
" 4.72 | \n",
"
\n",
" \n",
" 4 | \n",
" Mayo | \n",
" 23.46 | \n",
" 11.91 | \n",
"
\n",
" \n",
" 5 | \n",
" Junio | \n",
" 23.12 | \n",
" 12.44 | \n",
"
\n",
" \n",
" 6 | \n",
" Julio | \n",
" 22.87 | \n",
" 11.75 | \n",
"
\n",
" \n",
" 7 | \n",
" Agosto | \n",
" 22.95 | \n",
" 12.03 | \n",
"
\n",
" \n",
" 8 | \n",
" Septiembre | \n",
" 22.91 | \n",
" 11.77 | \n",
"
\n",
" \n",
" 9 | \n",
" Octubre | \n",
" 22.59 | \n",
" 14.74 | \n",
"
\n",
" \n",
" 10 | \n",
" Noviembre | \n",
" 22.24 | \n",
" 12.65 | \n",
"
\n",
" \n",
" 11 | \n",
" Diciembre | \n",
" 22.13 | \n",
" 6.32 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" mes temperatura precipitacion\n",
"0 Enero 22.15 3.98\n",
"1 Febrero 22.60 2.92\n",
"2 Marzo 23.16 2.73\n",
"3 Abril 23.67 4.72\n",
"4 Mayo 23.46 11.91\n",
"5 Junio 23.12 12.44\n",
"6 Julio 22.87 11.75\n",
"7 Agosto 22.95 12.03\n",
"8 Septiembre 22.91 11.77\n",
"9 Octubre 22.59 14.74\n",
"10 Noviembre 22.24 12.65\n",
"11 Diciembre 22.13 6.32"
]
},
"execution_count": 228,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Carga de datos de clima\n",
"clima = pd.read_csv(\n",
" \"https://raw.githubusercontent.com/gf0657-programacionsig/2024-ii/refs/heads/main/datos/era5/temperatura-precipitacion-cri-2004-2023.csv\"\n",
")\n",
"\n",
"# Despliegue de los datos\n",
"clima"
]
},
{
"cell_type": "code",
"execution_count": 229,
"id": "04e87437",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"marker": {
"color": "blue"
},
"name": "Precipitación (mm)",
"type": "bar",
"x": [
"Enero",
"Febrero",
"Marzo",
"Abril",
"Mayo",
"Junio",
"Julio",
"Agosto",
"Septiembre",
"Octubre",
"Noviembre",
"Diciembre"
],
"y": [
3.9765713,
2.9162953,
2.7286315,
4.7178497,
11.906303,
12.439727,
11.74687,
12.027591,
11.770472,
14.742642,
12.647323,
6.321157
],
"yaxis": "y"
},
{
"line": {
"color": "red"
},
"marker": {
"color": "red"
},
"mode": "lines+markers",
"name": "Temperatura (°C)",
"type": "scatter",
"x": [
"Enero",
"Febrero",
"Marzo",
"Abril",
"Mayo",
"Junio",
"Julio",
"Agosto",
"Septiembre",
"Octubre",
"Noviembre",
"Diciembre"
],
"y": [
22.145555,
22.595753,
23.161158,
23.668037,
23.456148,
23.122997,
22.873407,
22.95208,
22.91109,
22.591791,
22.240808,
22.13014
],
"yaxis": "y2"
}
],
"layout": {
"height": 600,
"legend": {
"bgcolor": "rgba(255,255,255,0)",
"bordercolor": "Black",
"borderwidth": 1,
"x": 0.01,
"y": 0.99
},
"template": {
"data": {
"bar": [
{
"error_x": {
"color": "#2a3f5f"
},
"error_y": {
"color": "#2a3f5f"
},
"marker": {
"line": {
"color": "white",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "bar"
}
],
"barpolar": [
{
"marker": {
"line": {
"color": "white",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "barpolar"
}
],
"carpet": [
{
"aaxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "#C8D4E3",
"linecolor": "#C8D4E3",
"minorgridcolor": "#C8D4E3",
"startlinecolor": "#2a3f5f"
},
"baxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "#C8D4E3",
"linecolor": "#C8D4E3",
"minorgridcolor": "#C8D4E3",
"startlinecolor": "#2a3f5f"
},
"type": "carpet"
}
],
"choropleth": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "choropleth"
}
],
"contour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "contour"
}
],
"contourcarpet": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "contourcarpet"
}
],
"heatmap": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmap"
}
],
"heatmapgl": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmapgl"
}
],
"histogram": [
{
"marker": {
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "histogram"
}
],
"histogram2d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2d"
}
],
"histogram2dcontour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2dcontour"
}
],
"mesh3d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "mesh3d"
}
],
"parcoords": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "parcoords"
}
],
"pie": [
{
"automargin": true,
"type": "pie"
}
],
"scatter": [
{
"fillpattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
},
"type": "scatter"
}
],
"scatter3d": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter3d"
}
],
"scattercarpet": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattercarpet"
}
],
"scattergeo": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergeo"
}
],
"scattergl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergl"
}
],
"scattermapbox": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattermapbox"
}
],
"scatterpolar": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolar"
}
],
"scatterpolargl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolargl"
}
],
"scatterternary": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterternary"
}
],
"surface": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "surface"
}
],
"table": [
{
"cells": {
"fill": {
"color": "#EBF0F8"
},
"line": {
"color": "white"
}
},
"header": {
"fill": {
"color": "#C8D4E3"
},
"line": {
"color": "white"
}
},
"type": "table"
}
]
},
"layout": {
"annotationdefaults": {
"arrowcolor": "#2a3f5f",
"arrowhead": 0,
"arrowwidth": 1
},
"autotypenumbers": "strict",
"coloraxis": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"colorscale": {
"diverging": [
[
0,
"#8e0152"
],
[
0.1,
"#c51b7d"
],
[
0.2,
"#de77ae"
],
[
0.3,
"#f1b6da"
],
[
0.4,
"#fde0ef"
],
[
0.5,
"#f7f7f7"
],
[
0.6,
"#e6f5d0"
],
[
0.7,
"#b8e186"
],
[
0.8,
"#7fbc41"
],
[
0.9,
"#4d9221"
],
[
1,
"#276419"
]
],
"sequential": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"sequentialminus": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
]
},
"colorway": [
"#636efa",
"#EF553B",
"#00cc96",
"#ab63fa",
"#FFA15A",
"#19d3f3",
"#FF6692",
"#B6E880",
"#FF97FF",
"#FECB52"
],
"font": {
"color": "#2a3f5f"
},
"geo": {
"bgcolor": "white",
"lakecolor": "white",
"landcolor": "white",
"showlakes": true,
"showland": true,
"subunitcolor": "#C8D4E3"
},
"hoverlabel": {
"align": "left"
},
"hovermode": "closest",
"mapbox": {
"style": "light"
},
"paper_bgcolor": "white",
"plot_bgcolor": "white",
"polar": {
"angularaxis": {
"gridcolor": "#EBF0F8",
"linecolor": "#EBF0F8",
"ticks": ""
},
"bgcolor": "white",
"radialaxis": {
"gridcolor": "#EBF0F8",
"linecolor": "#EBF0F8",
"ticks": ""
}
},
"scene": {
"xaxis": {
"backgroundcolor": "white",
"gridcolor": "#DFE8F3",
"gridwidth": 2,
"linecolor": "#EBF0F8",
"showbackground": true,
"ticks": "",
"zerolinecolor": "#EBF0F8"
},
"yaxis": {
"backgroundcolor": "white",
"gridcolor": "#DFE8F3",
"gridwidth": 2,
"linecolor": "#EBF0F8",
"showbackground": true,
"ticks": "",
"zerolinecolor": "#EBF0F8"
},
"zaxis": {
"backgroundcolor": "white",
"gridcolor": "#DFE8F3",
"gridwidth": 2,
"linecolor": "#EBF0F8",
"showbackground": true,
"ticks": "",
"zerolinecolor": "#EBF0F8"
}
},
"shapedefaults": {
"line": {
"color": "#2a3f5f"
}
},
"ternary": {
"aaxis": {
"gridcolor": "#DFE8F3",
"linecolor": "#A2B1C6",
"ticks": ""
},
"baxis": {
"gridcolor": "#DFE8F3",
"linecolor": "#A2B1C6",
"ticks": ""
},
"bgcolor": "white",
"caxis": {
"gridcolor": "#DFE8F3",
"linecolor": "#A2B1C6",
"ticks": ""
}
},
"title": {
"x": 0.05
},
"xaxis": {
"automargin": true,
"gridcolor": "#EBF0F8",
"linecolor": "#EBF0F8",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "#EBF0F8",
"zerolinewidth": 2
},
"yaxis": {
"automargin": true,
"gridcolor": "#EBF0F8",
"linecolor": "#EBF0F8",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "#EBF0F8",
"zerolinewidth": 2
}
}
},
"title": {
"text": "Precipitación y Temperatura Promedio Mensual en Costa Rica (2004-2023)"
},
"width": 900,
"xaxis": {
"tickmode": "linear",
"title": {
"text": "Mes"
}
},
"yaxis": {
"tickfont": {
"color": "blue"
},
"title": {
"font": {
"color": "blue"
},
"text": "Precipitación (mm)"
}
},
"yaxis2": {
"overlaying": "y",
"side": "right",
"tickfont": {
"color": "red"
},
"title": {
"font": {
"color": "red"
},
"text": "Temperatura (°C)"
}
}
}
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Creación de una figura\n",
"fig = go.Figure()\n",
"\n",
"# Añadir las barras de precipitación\n",
"fig.add_trace(\n",
" go.Bar(\n",
" x=clima['mes'],\n",
" y=clima['precipitacion'],\n",
" name='Precipitación (mm)',\n",
" marker_color='blue',\n",
" yaxis='y1'\n",
" )\n",
")\n",
"\n",
"# Añadir la línea de temperatura\n",
"fig.add_trace(\n",
" go.Scatter(\n",
" x=clima['mes'],\n",
" y=clima['temperatura'],\n",
" name='Temperatura (°C)',\n",
" mode='lines+markers',\n",
" marker=dict(color='red'),\n",
" line=dict(color='red'),\n",
" yaxis='y2'\n",
" )\n",
")\n",
"\n",
"# Propiedades globales de la figura\n",
"fig.update_layout(\n",
" title='Precipitación y Temperatura Promedio Mensual en Costa Rica (2004-2023)',\n",
" xaxis=dict(\n",
" title='Mes',\n",
" tickmode='linear'\n",
" ),\n",
" yaxis=dict(\n",
" title='Precipitación (mm)',\n",
" titlefont=dict(color='blue'),\n",
" tickfont=dict(color='blue')\n",
" ),\n",
" yaxis2=dict(\n",
" title='Temperatura (°C)',\n",
" titlefont=dict(color='red'),\n",
" tickfont=dict(color='red'),\n",
" overlaying='y',\n",
" side='right'\n",
" ),\n",
" legend=dict(\n",
" x=0.01,\n",
" y=0.99,\n",
" bgcolor='rgba(255,255,255,0)',\n",
" bordercolor=\"Black\",\n",
" borderwidth=1\n",
" ),\n",
" template='plotly_white',\n",
" width=900,\n",
" height=600\n",
")\n",
"\n",
"# Despliegue del gráfico\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"id": "934dbb48",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}