{
"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": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'2.4.1'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly\n",
"plotly.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Add Marker Border"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In order to make markers distinct, you can add a border to the markers. This can be achieved by adding the line dict to the marker dict. For example, `marker:{..., line: {...}}`."
]
},
{
"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",
"import numpy as np\n",
"\n",
"x = np.random.uniform(low=3, high=6, size=(500,))\n",
"y = np.random.uniform(low=3, high=6, size=(500,))\n",
" \n",
"data = [\n",
" go.Scatter(\n",
" mode = 'markers',\n",
" x = x,\n",
" y = y,\n",
" marker = dict(\n",
" color = 'rgb(17, 157, 255)',\n",
" size = 20,\n",
" line = dict(\n",
" color = 'rgb(231, 99, 250)',\n",
" width = 2\n",
" )\n",
" ),\n",
" showlegend = False\n",
" ),\n",
" go.Scatter(\n",
" mode = 'markers',\n",
" x = [2],\n",
" y = [4.5],\n",
" marker = dict(\n",
" color = 'rgb(17, 157, 255)',\n",
" size = 120,\n",
" line = dict(\n",
" color = 'rgb(231, 99, 250)',\n",
" width = 12\n",
" )\n",
" ),\n",
" showlegend = False\n",
" )]\n",
"\n",
"py.iplot(data, filename = \"style-add-border\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Fully Opaque"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Fully opaque, the default setting, is useful for non-overlapping markers. When many points overlap it can be hard to observe density. "
]
},
{
"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",
"import numpy as np\n",
"\n",
"x = np.random.uniform(low=3, high=6, size=(500,))\n",
"y = np.random.uniform(low=3, high=6, size=(500,))\n",
" \n",
"data = [\n",
" go.Scatter(\n",
" mode = 'markers',\n",
" x = x,\n",
" y = y,\n",
" marker = dict(\n",
" color = 'rgb(17, 157, 255)',\n",
" size = 20,\n",
" line = dict(\n",
" color = 'rgb(231, 99, 250)',\n",
" width = 2\n",
" )\n",
" ),\n",
" showlegend = False\n",
" ),\n",
" go.Scatter(\n",
" mode = 'markers',\n",
" x = [2,2],\n",
" y = [4.25,4.75],\n",
" marker = dict(\n",
" color = 'rgb(17, 157, 255)',\n",
" size = 80,\n",
" line = dict(\n",
" color = 'rgb(231, 99, 250)',\n",
" width = 8\n",
" )\n",
" ),\n",
" showlegend = False\n",
" )]\n",
"\n",
"py.iplot(data, filename = \"style-full-opaque\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Opacity"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Setting opacity outside the marker will set the opacity of the trace. Thus, it will allow greater visbility of additional traces but like fully opaque it is hard to distinguish density. "
]
},
{
"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",
"import numpy as np\n",
"\n",
"x = np.random.uniform(low=3, high=6, size=(500,))\n",
"y = np.random.uniform(low=3, high=4.5, size=(500,))\n",
"x2 = np.random.uniform(low=3, high=6, size=(500,))\n",
"y2 = np.random.uniform(low=4.5, high=6, size=(500,))\n",
" \n",
"data = [\n",
" go.Scatter(\n",
" mode = 'markers',\n",
" x = x,\n",
" y = y,\n",
" opacity = 0.5,\n",
" marker = dict(\n",
" color = 'rgb(17, 157, 255)',\n",
" size = 20,\n",
" line = dict(\n",
" color = 'rgb(231, 99, 250)',\n",
" width = 2\n",
" )\n",
" ),\n",
" name = 'Opacity 0.5'\n",
" ),\n",
" go.Scatter(\n",
" mode = 'markers',\n",
" x = x2,\n",
" y = y2,\n",
" marker = dict(\n",
" color = 'rgb(17, 157, 255)',\n",
" size = 20,\n",
" line = dict(\n",
" color = 'rgb(231, 99, 250)',\n",
" width = 2\n",
" )\n",
" ),\n",
" name = 'Opacity 1.0'\n",
" ),\n",
" go.Scatter(\n",
" mode = 'markers',\n",
" x = [2,2],\n",
" y = [4.25,4.75],\n",
" opacity = 0.5,\n",
" marker = dict(\n",
" color = 'rgb(17, 157, 255)',\n",
" size = 80,\n",
" line = dict(\n",
" color = 'rgb(231, 99, 250)',\n",
" width = 8\n",
" )\n",
" ),\n",
" showlegend = False\n",
" )]\n",
"\n",
"py.iplot(data, filename = \"style-opacity\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Marker Opacity"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To maximise visibility of density, it is recommended to set the opacity inside the marker `marker:{opacity:0.5}`. If mulitple traces exist with high density, consider using marker opacity in conjunction with trace opacity. "
]
},
{
"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 numpy as np\n",
"\n",
"x = np.random.uniform(low=3, high=6, size=(500,))\n",
"y = np.random.uniform(low=3, high=6, size=(500,))\n",
" \n",
"data = [\n",
" go.Scatter(\n",
" mode = 'markers',\n",
" x = x,\n",
" y = y,\n",
" marker = dict(\n",
" color = 'rgb(17, 157, 255)',\n",
" size = 20,\n",
" opacity = 0.5,\n",
" line = dict(\n",
" color = 'rgb(231, 99, 250)',\n",
" width = 2\n",
" )\n",
" ),\n",
" showlegend = False\n",
" ),\n",
" go.Scatter(\n",
" mode = 'markers',\n",
" x = [2,2],\n",
" y = [4.25,4.75],\n",
" marker = dict(\n",
" color = 'rgb(17, 157, 255)',\n",
" size = 80,\n",
" opacity = 0.5,\n",
" line = dict(\n",
" color = 'rgb(231, 99, 250)',\n",
" width = 8\n",
" )\n",
" ),\n",
" showlegend = False\n",
" )]\n",
"\n",
"py.iplot(data, filename = \"style-marker-opacity\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Color Opacity"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To maximise visibility of each point, set the color opacity by using alpha: `marker:{color: 'rgba(0,0,0,0.5)'}`. Here, the marker line will remain opaque. "
]
},
{
"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",
"import numpy as np\n",
"\n",
"x = np.random.uniform(low=3, high=6, size=(500,))\n",
"y = np.random.uniform(low=3, high=6, size=(500,))\n",
" \n",
"data = [\n",
" go.Scatter(\n",
" mode = 'markers',\n",
" x = x,\n",
" y = y,\n",
" marker = dict(\n",
" color = 'rgba(17, 157, 255, 0.5)',\n",
" size = 20,\n",
" line = dict(\n",
" color = 'rgb(231, 99, 250)',\n",
" width = 2\n",
" )\n",
" ),\n",
" showlegend = False\n",
" ),\n",
" go.Scatter(\n",
" mode = 'markers',\n",
" x = [2,2],\n",
" y = [4.25,4.75],\n",
" marker = dict(\n",
" color = 'rgba(17, 157, 255, 0.5)',\n",
" size = 80,\n",
" line = dict(\n",
" color = 'rgb(231, 99, 250)',\n",
" width = 8\n",
" )\n",
" ),\n",
" showlegend = False\n",
" )]\n",
"\n",
"py.iplot(data, filename = \"style-color-opacity\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Reference\n",
"See https://plotly.com/python/reference/ for more information and chart attribute options!"
]
},
{
"cell_type": "code",
"execution_count": 9,
"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 c:\\users\\brand\\appdata\\local\\temp\\pip-c2tssn0g-build\n",
"Installing collected packages: publisher\n",
" Found existing installation: publisher 0.11\n",
" Uninstalling publisher-0.11:\n",
" Successfully uninstalled publisher-0.11\n",
" Running setup.py install for publisher: started\n",
" Running setup.py install for publisher: finished with status 'done'\n",
"Successfully installed publisher-0.11\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Python27\\lib\\site-packages\\IPython\\nbconvert.py:13: ShimWarning:\n",
"\n",
"The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n",
"\n",
"C:\\Python27\\lib\\site-packages\\publisher\\publisher.py:53: UserWarning:\n",
"\n",
"Did you \"Save\" this notebook before running this command? Remember to save, always save.\n",
"\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",
" 'marker-style.ipynb', 'python/marker-style/', 'Styling Markers',\n",
" 'How to style markers in Python with Plotly.',\n",
" title = 'Styling Markers | Plotly',\n",
" has_thumbnail='true', thumbnail='thumbnail/marker-style.gif',\n",
" language='python',\n",
" display_as='file_settings', order=21, ipynb='~notebook_demo/203')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"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.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}