{
"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!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 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": [
"'2.4.1'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly\n",
"plotly.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Adding Text to Data in Line and Scatter Plots"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"trace1 = go.Scatter(\n",
" x=[0, 1, 2],\n",
" y=[1, 1, 1],\n",
" mode='lines+markers+text',\n",
" name='Lines, Markers and Text',\n",
" text=['Text A', 'Text B', 'Text C'],\n",
" textposition='top center'\n",
")\n",
"\n",
"trace2 = go.Scatter(\n",
" x=[0, 1, 2],\n",
" y=[2, 2, 2],\n",
" mode='markers+text',\n",
" name='Markers and Text',\n",
" text=['Text D', 'Text E', 'Text F'],\n",
" textposition='bottom center'\n",
")\n",
"\n",
"trace3 = go.Scatter(\n",
" x=[0, 1, 2],\n",
" y=[3, 3, 3],\n",
" mode='lines+text',\n",
" name='Lines and Text',\n",
" text=['Text G', 'Text H', 'Text I'],\n",
" textposition='bottom center'\n",
")\n",
"\n",
"data = [trace1, trace2, trace3]\n",
"\n",
"layout = go.Layout(\n",
" showlegend=False\n",
")\n",
"\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='text-chart-basic')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Adding Hover Text to Data in Line and Scatter Plots"
]
},
{
"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",
"data = [\n",
" go.Scatter(\n",
" x=[0, 1, 2],\n",
" y=[1, 3, 2],\n",
" mode='markers',\n",
" text=['Text A', 'Text B', 'Text C']\n",
" )\n",
"]\n",
"\n",
"layout = go.Layout(\n",
" title='Hover over the points to see the text'\n",
")\n",
"\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='hover-chart-basic')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Simple Annotation"
]
},
{
"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",
"trace1 = go.Scatter(\n",
" x=[0, 1, 2, 3, 4, 5, 6, 7, 8],\n",
" y=[0, 1, 3, 2, 4, 3, 4, 6, 5]\n",
")\n",
"\n",
"trace2 = go.Scatter(\n",
" x=[0, 1, 2, 3, 4, 5, 6, 7, 8],\n",
" y=[0, 4, 5, 1, 2, 2, 3, 4, 2]\n",
")\n",
"\n",
"data = [trace1, trace2]\n",
"\n",
"layout = go.Layout(\n",
" showlegend=False,\n",
" annotations=[\n",
" dict(\n",
" x=2,\n",
" y=5,\n",
" xref='x',\n",
" yref='y',\n",
" text='dict Text',\n",
" showarrow=True,\n",
" arrowhead=7,\n",
" ax=0,\n",
" ay=-40\n",
" )\n",
" ]\n",
")\n",
"\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='simple-annotation')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Multiple Annotations"
]
},
{
"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",
"trace1 = go.Scatter(\n",
" x=[0, 1, 2, 3, 4, 5, 6, 7, 8],\n",
" y=[0, 1, 3, 2, 4, 3, 4, 6, 5]\n",
")\n",
"\n",
"trace2 = go.Scatter(\n",
" x=[0, 1, 2, 3, 4, 5, 6, 7, 8],\n",
" y=[0, 4, 5, 1, 2, 2, 3, 4, 2]\n",
")\n",
"\n",
"data = [trace1, trace2]\n",
"\n",
"layout = go.Layout(\n",
" showlegend=False,\n",
" annotations=[\n",
" dict(\n",
" x=2,\n",
" y=5,\n",
" xref='x',\n",
" yref='y',\n",
" text='dict Text',\n",
" showarrow=True,\n",
" arrowhead=7,\n",
" ax=0,\n",
" ay=-40\n",
" ),\n",
" dict(\n",
" x=4,\n",
" y=4,\n",
" xref='x',\n",
" yref='y',\n",
" text='dict Text 2',\n",
" showarrow=True,\n",
" arrowhead=7,\n",
" ax=0,\n",
" ay=-40\n",
" )\n",
" ]\n",
")\n",
"\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='multiple-annotation')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3D Annotations"
]
},
{
"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",
"data = [go.Scatter3d(\n",
" x = [\"2017-01-01\", \"2017-02-10\", \"2017-03-20\"],\n",
" y = [\"A\", \"B\", \"C\"],\n",
" z = [1, 1000, 100000],\n",
" name = \"z\",\n",
")]\n",
"\n",
"layout = go.Layout(\n",
" scene = dict(\n",
" aspectratio = dict(\n",
" x = 1,\n",
" y = 1,\n",
" z = 1\n",
" ),\n",
" camera = dict(\n",
" center = dict(\n",
" x = 0,\n",
" y = 0,\n",
" z = 0\n",
" ),\n",
" eye = dict(\n",
" x = 1.96903462608,\n",
" y = -1.09022831971,\n",
" z = 0.405345349304\n",
" ),\n",
" up = dict(\n",
" x = 0,\n",
" y = 0,\n",
" z = 1\n",
" )\n",
" ),\n",
" dragmode = \"turntable\",\n",
" xaxis = dict(\n",
" title = \"\",\n",
" type = \"date\"\n",
" ),\n",
" yaxis = dict(\n",
" title = \"\",\n",
" type = \"category\"\n",
" ),\n",
" zaxis = dict(\n",
" title = \"\",\n",
" type = \"log\"\n",
" ),\n",
" annotations = [dict(\n",
" showarrow = False,\n",
" x = \"2017-01-01\",\n",
" y = \"A\",\n",
" z = 0,\n",
" text = \"Point 1\",\n",
" xanchor = \"left\",\n",
" xshift = 10,\n",
" opacity = 0.7\n",
" ), dict(\n",
" x = \"2017-02-10\",\n",
" y = \"B\",\n",
" z = 4,\n",
" text = \"Point 2\",\n",
" textangle = 0,\n",
" ax = 0,\n",
" ay = -75,\n",
" font = dict(\n",
" color = \"black\",\n",
" size = 12\n",
" ),\n",
" arrowcolor = \"black\",\n",
" arrowsize = 3,\n",
" arrowwidth = 1,\n",
" arrowhead = 1\n",
" ), dict(\n",
" x = \"2017-03-20\",\n",
" y = \"C\",\n",
" z = 5,\n",
" ax = 50,\n",
" ay = 0,\n",
" text = \"Point 3\",\n",
" arrowhead = 1,\n",
" xanchor = \"left\",\n",
" yanchor = \"bottom\"\n",
" )]\n",
" ),\n",
" xaxis = dict(title = \"x\"),\n",
" yaxis = dict(title = \"y\")\n",
")\n",
"\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename = \"3d annotations\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Custom Text Color and Styling"
]
},
{
"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",
"trace1 = go.Scatter(\n",
" x=[0, 1, 2],\n",
" y=[1, 1, 1],\n",
" mode='lines+markers+text',\n",
" name='Lines, Markers and Text',\n",
" text=['Text A', 'Text B', 'Text C'],\n",
" textposition='top right',\n",
" textfont=dict(\n",
" family='sans serif',\n",
" size=18,\n",
" color='#1f77b4'\n",
" )\n",
")\n",
"\n",
"trace2 = go.Scatter(\n",
" x=[0, 1, 2],\n",
" y=[2, 2, 2],\n",
" mode='lines+markers+text',\n",
" name='Lines and Text',\n",
" text=['Text G', 'Text H', 'Text I'],\n",
" textposition='bottom center',\n",
" textfont=dict(\n",
" family='sans serif',\n",
" size=18,\n",
" color='#ff7f0e'\n",
" )\n",
")\n",
"\n",
"data = [trace1, trace2]\n",
"\n",
"layout = go.Layout(\n",
" showlegend=False\n",
")\n",
"\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='text-chart-styling')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Styling and Coloring Annotations"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"trace1 = go.Scatter(\n",
" x=[0, 1, 2, 3, 4, 5, 6, 7, 8],\n",
" y=[0, 1, 3, 2, 4, 3, 4, 6, 5]\n",
")\n",
"\n",
"trace2 = go.Scatter(\n",
" x=[0, 1, 2, 3, 4, 5, 6, 7, 8],\n",
" y=[0, 4, 5, 1, 2, 2, 3, 4, 2]\n",
")\n",
"\n",
"data = [trace1, trace2]\n",
"\n",
"layout = go.Layout(\n",
" showlegend=False,\n",
" annotations=[\n",
" dict(\n",
" x=2,\n",
" y=5,\n",
" xref='x',\n",
" yref='y',\n",
" text='max=5',\n",
" showarrow=True,\n",
" font=dict(\n",
" family='Courier New, monospace',\n",
" size=16,\n",
" color='#ffffff'\n",
" ),\n",
" align='center',\n",
" arrowhead=2,\n",
" arrowsize=1,\n",
" arrowwidth=2,\n",
" arrowcolor='#636363',\n",
" ax=20,\n",
" ay=-30,\n",
" bordercolor='#c7c7c7',\n",
" borderwidth=2,\n",
" borderpad=4,\n",
" bgcolor='#ff7f0e',\n",
" opacity=0.8\n",
" )\n",
" ]\n",
")\n",
"\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='style-annotation')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Disabling Hover Text"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"\n",
"trace = dict(\n",
" x=[1, 2, 3,],\n",
" y=[10, 30, 15],\n",
" type='scatter',\n",
" name='first trace',\n",
" hoverinfo='none'\n",
")\n",
"\n",
"py.iplot([trace], filename='hoverinfo=none')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Text Font as an Array - Styling Each Text Element"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"fig = go.Figure(\n",
" data=[\n",
" go.Scattergeo(\n",
" lat=[45.5,43.4,49.13,51.1,53.34,45.24,44.64,48.25,49.89,50.45],\n",
" lon=[-73.57,-79.24,-123.06,-114.1,-113.28,-75.43,-63.57,-123.21,-97.13,-104.6],\n",
" marker={\n",
" \"color\": [\"#bebada\",\"#fdb462\",\"#fb8072\",\"#d9d9d9\",\"#bc80bd\",\"#b3de69\",\"#8dd3c7\",\"#80b1d3\",\"#fccde5\",\"#ffffb3\"],\n",
" \"line\": {\n",
" \"width\": 1\n",
" },\n",
" \"size\": 10\n",
" },\n",
" mode=\"markers+text\",\n",
" name=\"\",\n",
" text=[\"Montreal\",\"Toronto\",\"Vancouver\",\"Calgary\",\"Edmonton\",\"Ottawa\",\"Halifax\",\"Victoria\",\"Winnepeg\",\"Regina\"],\n",
" textfont={\n",
" \"color\": [\"#bebada\",\"#fdb462\",\"#fb8072\",\"#d9d9d9\",\"#bc80bd\",\"#b3de69\",\"#8dd3c7\",\"#80b1d3\",\"#fccde5\",\"#ffffb3\"],\n",
" \"family\": [\"Arial, sans-serif\",\"Balto, sans-serif\",\"Courier New, monospace\",\"Droid Sans, sans-serif\",\"Droid Serif, serif\",\"Droid Sans Mono, sans-serif\",\"Gravitas One, cursive\",\"Old Standard TT, serif\",\"Open Sans, sans-serif\",\"PT Sans Narrow, sans-serif\",\"Raleway, sans-serif\",\"Times New Roman, Times, serif\"],\n",
" \"size\": [22,21,20,19,18,17,16,15,14,13]\n",
" },\n",
" textposition=[\"top center\",\"middle left\",\"top center\",\"bottom center\",\"top right\",\"middle left\",\"bottom right\",\"bottom left\",\"top right\",\"top right\"]\n",
" )\n",
" ],\n",
" layout={\n",
" \"title\": \"Canadian cities\",\n",
" \"geo\": {\n",
" \"lataxis\": {\n",
" \"range\": [40, 70]\n",
" },\n",
" \"lonaxis\": {\n",
" \"range\": [-130, -55]\n",
" },\n",
" \"scope\": \"north america\"\n",
" }\n",
" }\n",
")\n",
"\n",
"py.iplot(fig, filename='Canadian Cities')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Adding Annotations with xref and yref as Paper"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"data = [\n",
" go.Scatter(\n",
" x=[1, 2, 3],\n",
" y=[1, 2, 3],\n",
" name='y',\n",
" )\n",
"]\n",
"\n",
"layout = go.Layout(\n",
" annotations=[\n",
" dict(\n",
" x=0.5004254919715793,\n",
" y=-0.16191064079952971,\n",
" showarrow=False,\n",
" text='Custom x-axis title',\n",
" xref='paper',\n",
" yref='paper'\n",
" ),\n",
" dict(\n",
" x=-0.04944728761514841,\n",
" y=0.4714285714285711,\n",
" showarrow=False,\n",
" text='Custom y-axis title',\n",
" textangle=-90,\n",
" xref='paper',\n",
" yref='paper'\n",
" )\n",
" ],\n",
" autosize=True,\n",
" margin=dict(\n",
" b=100\n",
" ),\n",
" title='Plot Title',\n",
" xaxis=dict(\n",
" autorange=False,\n",
" range=[-0.05674507980728292, -0.0527310420933204],\n",
" type='linear'\n",
" ),\n",
" yaxis=dict(\n",
" autorange=False,\n",
" range=[1.2876210047544652, 1.2977732997811402],\n",
" type='linear'\n",
" ),\n",
" height=550,\n",
" width=1137\n",
")\n",
"\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dash Example"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Dash](https://plotly.com/products/dash/) is an Open Source Python library which can help you convert plotly figures into a reactive, web-based application. Below is a simple example of a dashboard created using Dash. Its [source code](https://github.com/plotly/simple-example-chart-apps/tree/master/dash-text-annotationsplot) can easily be deployed to a PaaS."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import IFrame\n",
"IFrame(src= \"https://dash-simple-apps.plotly.host/dash-text-annotationsplot/\", width=\"100%\", height=\"750px\", frameBorder=\"0\")"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import IFrame\n",
"IFrame(src= \"https://dash-simple-apps.plotly.host/dash-text-annotationsplot/code\", width=\"100%\", height=500, frameBorder=\"0\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Reference\n",
"See https://plotly.com/python/reference/#layout-annotations for more information and chart attribute options!"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-req-build-8j23umwg\n",
"Building wheels for collected packages: publisher\n",
" Building wheel for publisher (setup.py) ... \u001b[?25ldone\n",
"\u001b[?25h Stored in directory: /private/var/folders/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-ephem-wheel-cache-vjzses9j/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n",
"Successfully built publisher\n",
"Installing collected packages: publisher\n",
" Found existing installation: publisher 0.13\n",
" Uninstalling publisher-0.13:\n",
" Successfully uninstalled publisher-0.13\n",
"Successfully installed publisher-0.13\n",
"\u001b[33mYou are using pip version 19.0.3, however version 19.1.1 is available.\n",
"You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\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",
" 'text-and-annotations.ipynb', 'python/text-and-annotations/', 'Text and Annotations',\n",
" 'How to add text labels and annotations to plots in python.',\n",
" title = 'Text and Annotations | plotly',\n",
" thumbnail='thumbnail/text-and-annotations.jpg', language='python',\n",
" has_thumbnail='true', display_as='file_settings', order=30, \n",
" ipynb='~notebook_demo/204', uses_plotly_offline=False)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}