{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plotly Library Basics"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A look under the hood of the Plotly library"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import plotly.graph_objs as go\n",
"import plotly.express as px"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A simple scatter plot"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv('Gapminder-mini.csv', sep=',')\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig = px.scatter(\n",
" df, \n",
" x='Income', \n",
" y='Life expectancy',\n",
" color='Region'\n",
")\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plotly's internal representation of a static scatter plot"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(fig)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A simple dictionary with two parts:\n",
"* data (contains all data-related information)\n",
"* layout (contains all other information)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f = {\n",
" 'data': [{'hovertemplate': 'Region=Europe
Income=%{x}
Life expectancy=%{y}',\n",
" 'legendgroup': 'Europe',\n",
" 'marker': {'color': '#636efa', 'symbol': 'circle'},\n",
" 'mode': 'markers',\n",
" 'name': 'Europe',\n",
" 'orientation': 'v',\n",
" 'showlegend': True,\n",
" 'type': 'scatter',\n",
" 'x': [34735., 35373., 35739., 35816., 36084.],\n",
" 'xaxis': 'x',\n",
" 'y': [82.97, 83.17, 83.33, 83.49, 83.64],\n",
" 'yaxis': 'y'},\n",
" {'hovertemplate': 'Region=Africa
Income=%{x}
Life expectancy=%{y}',\n",
" 'legendgroup': 'Africa',\n",
" 'marker': {'color': '#EF553B', 'symbol': 'circle'},\n",
" 'mode': 'markers',\n",
" 'name': 'Africa',\n",
" 'orientation': 'v',\n",
" 'showlegend': True,\n",
" 'type': 'scatter',\n",
" 'x': [12246., 12233., 12143., 12039., 11986.],\n",
" 'xaxis': 'x',\n",
" 'y': [64.44, 66.31, 66.64, 66.93, 67.19],\n",
" 'yaxis': 'y'}],\n",
" 'layout': {'legend': {'title': {'text': 'Region'}, 'tracegroupgap': 0},\n",
" 'margin': {'t': 60},\n",
" 'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title': {'text': 'Income'}},\n",
" 'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'Life expectancy'}}}\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig = go.Figure(f)\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plotly's internal representation of an animated scatter plot"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig = px.scatter(\n",
" df, \n",
" x='Income', \n",
" y='Life expectancy',\n",
" color='Region',\n",
" animation_frame='Year'\n",
")\n",
"fig.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"print(fig)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A more complex dictionary with three parts:\n",
"* data (contains all data-related information for the initial frame)\n",
"* frames (contains all data-related information for each frame (including the first one))\n",
"* layout (contains all other information, including slider and buttons)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Difference between `plotly.graph_obj` and `plotly.express`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `plotly.graph_obj` \n",
"\n",
"* Low-level syntax\n",
"* Complex interface, data in the form of a dictionary\n",
"* Complete control of all the details\n",
"* Analogous to the `matplotlib` library\n",
"\n",
"### `plotly.express`\n",
"\n",
"* High-level syntax\n",
"* Easy interface, data should be in a Pandas DataFrame\n",
"* No control over some details\n",
"* Analogous to the `seaborn` library"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"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.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}