{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### New to Plotly?\n",
"Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by downloading the client and [reading the primer](https://plotly.com/python/getting-started/).\n",
"
You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).\n",
"
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!\n",
"#### Version Check\n",
"Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'3.2.0'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly\n",
"plotly.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Simple Bubble Chart"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"trace0 = go.Scatter(\n",
" x=[1, 2, 3, 4],\n",
" y=[10, 11, 12, 13],\n",
" mode='markers',\n",
" marker=dict(\n",
" size=[40, 60, 80, 100],\n",
" )\n",
")\n",
"\n",
"data = [trace0]\n",
"py.iplot(data, filename='bubblechart-size')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Setting Marker Size and Color"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"trace0 = go.Scatter(\n",
" x=[1, 2, 3, 4],\n",
" y=[10, 11, 12, 13],\n",
" mode='markers',\n",
" marker=dict(\n",
" color=['rgb(93, 164, 214)', 'rgb(255, 144, 14)',\n",
" 'rgb(44, 160, 101)', 'rgb(255, 65, 54)'],\n",
" opacity=[1, 0.8, 0.6, 0.4],\n",
" size=[40, 60, 80, 100],\n",
" )\n",
")\n",
"\n",
"data = [trace0]\n",
"py.iplot(data, filename='bubblechart-color')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Scaling the Size of Bubble Charts\n",
"To scale the bubble size, use the attribute `sizeref`. We recommend using the following formula to calculate a `sizeref` value:
\n",
"`sizeref = 2. * max(array of size values) / (desired maximum marker size ** 2)`
\n",
"Note that setting 'sizeref' to a value greater than 1, decreases the rendered marker sizes, while setting 'sizeref' to less than 1, increases the rendered marker sizes. See https://plotly.com/python/reference/#scatter-marker-sizeref for more information.\n",
"Additionally, we recommend setting the sizemode attribute: https://plotly.com/python/reference/#scatter-marker-sizemode to area."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"size = [20, 40, 60, 80, 100, 80, 60, 40, 20, 40]\n",
"trace0 = go.Scatter(\n",
" x=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],\n",
" y=[11, 12, 10, 11, 12, 11, 12, 13, 12, 11],\n",
" mode='markers',\n",
" marker=dict(\n",
" size=size,\n",
" sizemode='area',\n",
" sizeref=2.*max(size)/(40.**2),\n",
" sizemin=4\n",
" )\n",
")\n",
"\n",
"data = [trace0]\n",
"py.iplot(data, filename='bubblechart-size-ref')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Hover Text with Bubble Charts"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"trace0 = go.Scatter(\n",
" x=[1, 2, 3, 4],\n",
" y=[10, 11, 12, 13],\n",
" text=['A
size: 40', 'B
size: 60', 'C
size: 80', 'D
size: 100'],\n",
" mode='markers',\n",
" marker=dict(\n",
" color=['rgb(93, 164, 214)', 'rgb(255, 144, 14)', 'rgb(44, 160, 101)', 'rgb(255, 65, 54)'],\n",
" size=[40, 60, 80, 100],\n",
" )\n",
")\n",
"\n",
"data = [trace0]\n",
"py.iplot(data, filename='bubblechart-text')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Bubble Charts with Colorscale"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"data = [\n",
" {\n",
" 'x': [1, 3.2, 5.4, 7.6, 9.8, 12.5],\n",
" 'y': [1, 3.2, 5.4, 7.6, 9.8, 12.5],\n",
" 'mode': 'markers',\n",
" 'marker': {\n",
" 'color': [120, 125, 130, 135, 140, 145],\n",
" 'size': [15, 30, 55, 70, 90, 110],\n",
" 'showscale': True\n",
" }\n",
" }\n",
"]\n",
"\n",
"py.iplot(data, filename='scatter-colorscale')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Categorical Bubble Charts"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"import pandas as pd\n",
"import math\n",
"\n",
"data = pd.read_csv(\"https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv\")\n",
"df_2007 = data[data['year']==2007]\n",
"df_2007 = df_2007.sort_values(['continent', 'country'])\n",
"slope = 2.666051223553066e-05\n",
"hover_text = []\n",
"bubble_size = []\n",
"\n",
"for index, row in df_2007.iterrows():\n",
" hover_text.append(('Country: {country}
'+\n",
" 'Life Expectancy: {lifeExp}
'+\n",
" 'GDP per capita: {gdp}
'+\n",
" 'Population: {pop}
'+\n",
" 'Year: {year}').format(country=row['country'],\n",
" lifeExp=row['lifeExp'],\n",
" gdp=row['gdpPercap'],\n",
" pop=row['pop'],\n",
" year=row['year']))\n",
" bubble_size.append(math.sqrt(row['pop']*slope))\n",
"\n",
"df_2007['text'] = hover_text\n",
"df_2007['size'] = bubble_size\n",
"sizeref = 2.*max(df_2007['size'])/(100**2)\n",
"\n",
"trace0 = go.Scatter(\n",
" x=df_2007['gdpPercap'][df_2007['continent'] == 'Africa'],\n",
" y=df_2007['lifeExp'][df_2007['continent'] == 'Africa'],\n",
" mode='markers',\n",
" name='Africa',\n",
" text=df_2007['text'][df_2007['continent'] == 'Africa'],\n",
" marker=dict(\n",
" symbol='circle',\n",
" sizemode='area',\n",
" sizeref=sizeref,\n",
" size=df_2007['size'][df_2007['continent'] == 'Africa'],\n",
" line=dict(\n",
" width=2\n",
" ),\n",
" )\n",
")\n",
"trace1 = go.Scatter(\n",
" x=df_2007['gdpPercap'][df_2007['continent'] == 'Americas'],\n",
" y=df_2007['lifeExp'][df_2007['continent'] == 'Americas'],\n",
" mode='markers',\n",
" name='Americas',\n",
" text=df_2007['text'][df_2007['continent'] == 'Americas'],\n",
" marker=dict(\n",
" sizemode='area',\n",
" sizeref=sizeref,\n",
" size=df_2007['size'][df_2007['continent'] == 'Americas'],\n",
" line=dict(\n",
" width=2\n",
" ),\n",
" )\n",
")\n",
"trace2 = go.Scatter(\n",
" x=df_2007['gdpPercap'][df_2007['continent'] == 'Asia'],\n",
" y=df_2007['lifeExp'][df_2007['continent'] == 'Asia'],\n",
" mode='markers',\n",
" name='Asia',\n",
" text=df_2007['text'][df_2007['continent'] == 'Asia'],\n",
" marker=dict(\n",
" sizemode='area',\n",
" sizeref=sizeref,\n",
" size=df_2007['size'][df_2007['continent'] == 'Asia'],\n",
" line=dict(\n",
" width=2\n",
" ),\n",
" )\n",
")\n",
"trace3 = go.Scatter(\n",
" x=df_2007['gdpPercap'][df_2007['continent'] == 'Europe'],\n",
" y=df_2007['lifeExp'][df_2007['continent'] == 'Europe'],\n",
" mode='markers',\n",
" name='Europe',\n",
" text=df_2007['text'][df_2007['continent'] == 'Europe'],\n",
" marker=dict(\n",
" sizemode='area',\n",
" sizeref=sizeref,\n",
" size=df_2007['size'][df_2007['continent'] == 'Europe'],\n",
" line=dict(\n",
" width=2\n",
" ),\n",
" )\n",
")\n",
"trace4 = go.Scatter(\n",
" x=df_2007['gdpPercap'][df_2007['continent'] == 'Oceania'],\n",
" y=df_2007['lifeExp'][df_2007['continent'] == 'Oceania'],\n",
" mode='markers',\n",
" name='Oceania',\n",
" text=df_2007['text'][df_2007['continent'] == 'Oceania'],\n",
" marker=dict(\n",
" sizemode='area',\n",
" sizeref=sizeref,\n",
" size=df_2007['size'][df_2007['continent'] == 'Oceania'],\n",
" line=dict(\n",
" width=2\n",
" ),\n",
" )\n",
")\n",
"\n",
"data = [trace0, trace1, trace2, trace3, trace4]\n",
"layout = go.Layout(\n",
" title='Life Expectancy v. Per Capita GDP, 2007',\n",
" xaxis=dict(\n",
" title='GDP per capita (2000 dollars)',\n",
" gridcolor='rgb(255, 255, 255)',\n",
" range=[2.003297660701705, 5.191505530708712],\n",
" type='log',\n",
" zerolinewidth=1,\n",
" ticklen=5,\n",
" gridwidth=2,\n",
" ),\n",
" yaxis=dict(\n",
" title='Life Expectancy (years)',\n",
" gridcolor='rgb(255, 255, 255)',\n",
" range=[36.12621671352166, 91.72921793264332],\n",
" zerolinewidth=1,\n",
" ticklen=5,\n",
" gridwidth=2,\n",
" ),\n",
" paper_bgcolor='rgb(243, 243, 243)',\n",
" plot_bgcolor='rgb(243, 243, 243)',\n",
")\n",
"\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='life-expectancy-per-GDP-2007')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Reference\n",
"See https://plotly.com/python/reference/#scatter for more information and chart attribute options!"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting git+https://github.com/plotly/publisher.git\n",
" Cloning https://github.com/plotly/publisher.git to /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-req-build-IqekGg\n",
"Building wheels for collected packages: publisher\n",
" Running setup.py bdist_wheel for publisher ... \u001b[?25ldone\n",
"\u001b[?25h Stored in directory: /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-ephem-wheel-cache-nsvXuo/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n",
"Successfully built publisher\n",
"Installing collected packages: publisher\n",
" Found existing installation: publisher 0.11\n",
" Uninstalling publisher-0.11:\n",
" Successfully uninstalled publisher-0.11\n",
"Successfully installed publisher-0.11\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n",
" \"You should import from nbconvert instead.\", ShimWarning)\n",
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning: Did you \"Save\" this notebook before running this command? Remember to save, always save.\n",
" warnings.warn('Did you \"Save\" this notebook before running this command? '\n"
]
}
],
"source": [
"from IPython.display import display, HTML\n",
"\n",
"display(HTML(''))\n",
"display(HTML(''))\n",
"\n",
"! pip install git+https://github.com/plotly/publisher.git --upgrade\n",
"import publisher\n",
"publisher.publish(\n",
" 'bubble.ipynb', 'python/bubble-charts/', 'Python Bubble Charts | plotly',\n",
" 'How to make bubble charts in Python with Plotly.',\n",
" title = 'Bubble Charts | plotly',\n",
" name = 'Bubble Charts', language='python',\n",
" has_thumbnail='true', thumbnail='thumbnail/bubble.jpg',\n",
" display_as='basic', order=3,\n",
" ipynb= '~notebook_demo/1/new-to-plotly-plotlys-python-library-i',\n",
" redirect_from='python/bubble-charts-tutorial/',\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 1
}