Skip to content

Commit 1e62a00

Browse files
committed
added python colorway example
1 parent 9a82dfb commit 1e62a00

File tree

2 files changed

+348
-0
lines changed

2 files changed

+348
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
---
2+
permalink: python/colorway/
3+
description: How to set default trace colors with colorway.
4+
name: Colorway
5+
has_thumbnail: true
6+
thumbnail: thumbnail/colorway.jpg
7+
layout: user-guide
8+
language: python
9+
title: Colorway in Python | Plotly
10+
display_as: style_opt
11+
has_thumbnail: true
12+
page_type: example_index
13+
order: 7.9
14+
ipynb: ~notebook_demo/256
15+
---
16+
{% raw %}
17+
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
18+
</div>
19+
<div class="inner_cell">
20+
<div class="text_cell_render border-box-sizing rendered_html">
21+
<h4 id="New-to-Plotly?">New to Plotly?<a class="anchor-link" href="#New-to-Plotly?">&#182;</a></h4><p>Plotly's Python library is free and open source! <a href="https://plot.ly/python/getting-started/">Get started</a> by downloading the client and <a href="https://plot.ly/python/getting-started/">reading the primer</a>.
22+
<br>You can set up Plotly to work in <a href="https://plot.ly/python/getting-started/#initialization-for-online-plotting">online</a> or <a href="https://plot.ly/python/getting-started/#initialization-for-offline-plotting">offline</a> mode, or in <a href="https://plot.ly/python/getting-started/#start-plotting-online">jupyter notebooks</a>.
23+
<br>We also have a quick-reference <a href="https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf">cheatsheet</a> (new!) to help you get started!</p>
24+
25+
</div>
26+
</div>
27+
</div>
28+
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
29+
</div>
30+
<div class="inner_cell">
31+
<div class="text_cell_render border-box-sizing rendered_html">
32+
<h4 id="Version-Check">Version Check<a class="anchor-link" href="#Version-Check">&#182;</a></h4><p>Plotly's python package is updated frequently. Run <code>pip install plotly --upgrade</code> to use the latest version.</p>
33+
34+
</div>
35+
</div>
36+
</div>
37+
<div class="cell border-box-sizing code_cell rendered">
38+
<div class="input">
39+
<div class="prompt input_prompt">In&nbsp;[1]:</div>
40+
<div class="inner_cell">
41+
<div class="input_area">
42+
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">plotly</span>
43+
<span class="n">plotly</span><span class="o">.</span><span class="n">__version__</span>
44+
</pre></div>
45+
46+
</div>
47+
</div>
48+
</div>
49+
50+
<div class="output_wrapper">
51+
<div class="output">
52+
53+
54+
<div class="output_area">
55+
56+
<div class="prompt output_prompt">Out[1]:</div>
57+
58+
59+
60+
61+
<div class="output_text output_subarea output_execute_result">
62+
<pre>&#39;3.1.1&#39;</pre>
63+
</div>
64+
65+
</div>
66+
67+
</div>
68+
</div>
69+
70+
</div>
71+
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
72+
</div>
73+
<div class="inner_cell">
74+
<div class="text_cell_render border-box-sizing rendered_html">
75+
<h4 id="Set-Default-Trace-Colors-with-colorway">Set Default Trace Colors with <code>colorway</code><a class="anchor-link" href="#Set-Default-Trace-Colors-with-colorway">&#182;</a></h4>
76+
</div>
77+
</div>
78+
</div>
79+
<div class="cell border-box-sizing code_cell rendered">
80+
<div class="input">
81+
<div class="prompt input_prompt">In&nbsp;[2]:</div>
82+
<div class="inner_cell">
83+
<div class="input_area">
84+
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">plotly.plotly</span> <span class="kn">as</span> <span class="nn">py</span>
85+
<span class="kn">import</span> <span class="nn">plotly.graph_objs</span> <span class="kn">as</span> <span class="nn">go</span>
86+
87+
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
88+
89+
90+
<span class="k">def</span> <span class="nf">parabola_gen</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
91+
<span class="k">return</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">a</span> <span class="o">*</span> <span class="n">x</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">b</span>
92+
93+
94+
<span class="n">parabolas</span> <span class="o">=</span> <span class="p">[</span><span class="n">parabola_gen</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="k">for</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">7</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">7</span><span class="p">))]</span>
95+
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
96+
<span class="n">data</span> <span class="o">=</span> <span class="p">[</span><span class="n">go</span><span class="o">.</span><span class="n">Scatter</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="n">p</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;lines&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">parabolas</span><span class="p">]</span>
97+
98+
<span class="n">colorway</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;#f3cec9&#39;</span><span class="p">,</span> <span class="s1">&#39;#e7a4b6&#39;</span><span class="p">,</span> <span class="s1">&#39;#cd7eaf&#39;</span><span class="p">,</span> <span class="s1">&#39;#a262a9&#39;</span><span class="p">,</span> <span class="s1">&#39;#6f4d96&#39;</span><span class="p">,</span> <span class="s1">&#39;#3d3b72&#39;</span><span class="p">,</span> <span class="s1">&#39;#182844&#39;</span><span class="p">]</span>
99+
<span class="n">layout</span> <span class="o">=</span> <span class="n">go</span><span class="o">.</span><span class="n">Layout</span><span class="p">(</span><span class="n">colorway</span><span class="o">=</span><span class="n">colorway</span><span class="p">)</span>
100+
101+
<span class="n">py</span><span class="o">.</span><span class="n">iplot</span><span class="p">(</span><span class="n">go</span><span class="o">.</span><span class="n">Figure</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">layout</span><span class="p">))</span>
102+
</pre></div>
103+
104+
</div>
105+
</div>
106+
</div>
107+
108+
<div class="output_wrapper">
109+
<div class="output">
110+
111+
112+
<div class="output_area">
113+
114+
<div class="prompt output_prompt">Out[2]:</div>
115+
116+
117+
118+
<div class="output_html rendered_html output_subarea output_execute_result">
119+
<iframe id="igraph" scrolling="no" style="border:none;" seamless="seamless" src="https://plot.ly/~PythonPlotBot/2807.embed" height="525px" width="100%"></iframe>
120+
</div>
121+
122+
</div>
123+
124+
</div>
125+
</div>
126+
127+
</div>
128+
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
129+
</div>
130+
<div class="inner_cell">
131+
<div class="text_cell_render border-box-sizing rendered_html">
132+
<h3 id="Reference">Reference<a class="anchor-link" href="#Reference">&#182;</a></h3>
133+
</div>
134+
</div>
135+
</div>
136+
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
137+
</div>
138+
<div class="inner_cell">
139+
<div class="text_cell_render border-box-sizing rendered_html">
140+
<p>See <a href="https://plot.ly/python/reference/">https://plot.ly/python/reference/</a> for more information and chart attribute options!</p>
141+
142+
</div>
143+
</div>
144+
</div>
145+
146+
147+
{% endraw %}
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### New to Plotly?\n",
8+
"Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).\n",
9+
"<br>You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/#start-plotting-online).\n",
10+
"<br>We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!"
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"metadata": {},
16+
"source": [
17+
"#### Version Check\n",
18+
"Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version."
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 1,
24+
"metadata": {},
25+
"outputs": [
26+
{
27+
"data": {
28+
"text/plain": [
29+
"'3.1.1'"
30+
]
31+
},
32+
"execution_count": 1,
33+
"metadata": {},
34+
"output_type": "execute_result"
35+
}
36+
],
37+
"source": [
38+
"import plotly\n",
39+
"plotly.__version__"
40+
]
41+
},
42+
{
43+
"cell_type": "markdown",
44+
"metadata": {},
45+
"source": [
46+
"#### Set Default Trace Colors with `colorway`"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 2,
52+
"metadata": {},
53+
"outputs": [
54+
{
55+
"data": {
56+
"text/html": [
57+
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~PythonPlotBot/2807.embed\" height=\"525px\" width=\"100%\"></iframe>"
58+
],
59+
"text/plain": [
60+
"<plotly.tools.PlotlyDisplay object>"
61+
]
62+
},
63+
"execution_count": 2,
64+
"metadata": {},
65+
"output_type": "execute_result"
66+
}
67+
],
68+
"source": [
69+
"import plotly.plotly as py\n",
70+
"import plotly.graph_objs as go\n",
71+
"\n",
72+
"import numpy as np\n",
73+
"\n",
74+
"\n",
75+
"def parabola_gen(a, b):\n",
76+
" return lambda x: a * x ** 2 + b\n",
77+
"\n",
78+
"\n",
79+
"parabolas = [parabola_gen(a, b) for a, b in zip(np.linspace(1, 3, 7), np.linspace(2, 14, 7))]\n",
80+
"x = np.linspace(-1, 3)\n",
81+
"data = [go.Scatter(x=x, y=p(x), mode='lines') for p in parabolas]\n",
82+
"\n",
83+
"colorway = ['#f3cec9', '#e7a4b6', '#cd7eaf', '#a262a9', '#6f4d96', '#3d3b72', '#182844']\n",
84+
"layout = go.Layout(colorway=colorway)\n",
85+
"\n",
86+
"py.iplot(go.Figure(data=data, layout=layout))\n"
87+
]
88+
},
89+
{
90+
"cell_type": "markdown",
91+
"metadata": {},
92+
"source": [
93+
"### Reference"
94+
]
95+
},
96+
{
97+
"cell_type": "markdown",
98+
"metadata": {},
99+
"source": [
100+
"See https://plot.ly/python/reference/ for more information and chart attribute options!"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": 3,
106+
"metadata": {},
107+
"outputs": [
108+
{
109+
"data": {
110+
"text/html": [
111+
"<link href=\"//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700\" rel=\"stylesheet\" type=\"text/css\" />"
112+
],
113+
"text/plain": [
114+
"<IPython.core.display.HTML object>"
115+
]
116+
},
117+
"metadata": {},
118+
"output_type": "display_data"
119+
},
120+
{
121+
"data": {
122+
"text/html": [
123+
"<link rel=\"stylesheet\" type=\"text/css\" href=\"http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css\">"
124+
],
125+
"text/plain": [
126+
"<IPython.core.display.HTML object>"
127+
]
128+
},
129+
"metadata": {},
130+
"output_type": "display_data"
131+
},
132+
{
133+
"name": "stdout",
134+
"output_type": "stream",
135+
"text": [
136+
"Collecting git+https://github.com/plotly/publisher.git\n",
137+
" Cloning https://github.com/plotly/publisher.git to /tmp/pip-req-build-sMJsCC\n",
138+
"Building wheels for collected packages: publisher\n",
139+
" Running setup.py bdist_wheel for publisher ... \u001b[?25ldone\n",
140+
"\u001b[?25h Stored in directory: /tmp/pip-ephem-wheel-cache-msTCdJ/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n",
141+
"Successfully built publisher\n",
142+
"Installing collected packages: publisher\n",
143+
" Found existing installation: publisher 0.11\n",
144+
" Uninstalling publisher-0.11:\n",
145+
" Successfully uninstalled publisher-0.11\n",
146+
"Successfully installed publisher-0.11\n",
147+
"\u001b[33mYou are using pip version 10.0.1, however version 18.0 is available.\n",
148+
"You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n"
149+
]
150+
}
151+
],
152+
"source": [
153+
"from IPython.display import display, HTML\n",
154+
"\n",
155+
"display(HTML('<link href=\"//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700\" rel=\"stylesheet\" type=\"text/css\" />'))\n",
156+
"display(HTML('<link rel=\"stylesheet\" type=\"text/css\" href=\"http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css\">'))\n",
157+
"\n",
158+
"! pip install git+https://github.com/plotly/publisher.git --upgrade\n",
159+
"import publisher\n",
160+
"publisher.publish(\n",
161+
" 'colorway.ipynb', 'python/colorway/', 'Colorway',\n",
162+
" 'How to set default trace colors with colorway.',\n",
163+
" title = 'Colorway in Python | Plotly',\n",
164+
" has_thumbnail='true', thumbnail='thumbnail/colorway.jpg', \n",
165+
" language='python', \n",
166+
" page_type='example_index',\n",
167+
" display_as='style_opt', \n",
168+
" order=7.9,\n",
169+
" ipynb= '~notebook_demo/256')"
170+
]
171+
},
172+
{
173+
"cell_type": "code",
174+
"execution_count": null,
175+
"metadata": {},
176+
"outputs": [],
177+
"source": []
178+
}
179+
],
180+
"metadata": {
181+
"kernelspec": {
182+
"display_name": "Python 2",
183+
"language": "python",
184+
"name": "python2"
185+
},
186+
"language_info": {
187+
"codemirror_mode": {
188+
"name": "ipython",
189+
"version": 2
190+
},
191+
"file_extension": ".py",
192+
"mimetype": "text/x-python",
193+
"name": "python",
194+
"nbconvert_exporter": "python",
195+
"pygments_lexer": "ipython2",
196+
"version": "2.7.15rc1"
197+
}
198+
},
199+
"nbformat": 4,
200+
"nbformat_minor": 2
201+
}

0 commit comments

Comments
 (0)