-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2019-07-03-graph-objects.html
More file actions
222 lines (169 loc) · 37.2 KB
/
Copy path2019-07-03-graph-objects.html
File metadata and controls
222 lines (169 loc) · 37.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
---
description: Python classes that represent parts of a figure.
display_as: file_settings
language: python
layout: base
name: Graph Objects
order: 36
page_type: u-guide
permalink: python/graph-objects/
thumbnail: thumbnail/horizontal-bar.jpg
---
{% raw %}
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="What-Are-Graph-Objects?">What Are Graph Objects?<a class="anchor-link" href="#What-Are-Graph-Objects?">¶</a></h3><p>The figures created, manipulated and rendered by the plotly Python library are <a href="/python/figure-structure/">represented by tree-like data structures</a> which are automatically serialized to JSON for rendering by the Plotly.js JavaScript library. These trees are composed of named nodes called "attributes", with their structure defined by the Plotly.js figure schema, which is available in <a href="https://raw.githubusercontent.com/plotly/plotly.js/master/dist/plot-schema.json">machine-readable form</a>. <strong>The <code>plotly.graph_objects</code> module (typically imported as <code>go</code>) contains an <a href="https://plotly.com/python-api-reference/plotly.graph_objects.html#graph-objects">automatically-generated hierarchy of Python classes</a> which represent non-leaf nodes in this figure schema. The term "graph objects" refers to instances of these classes. </strong></p>
<p>The primary classes defined in the <code>plotly.graph_objects</code> module are <a href="https://plotly.com/python-api-reference/generated/plotly.graph_objects.Figure.html"><code>Figure</code></a> and an <a href="/python/figurewidget/"><code>ipywidgets</code>-compatible variant called <code>FigureWidget</code></a>, which both represent entire figures. Instances of these classes have many convenience methods for Pythonically <a href="/python/creating-and-updating-figures/">manipulating their attributes</a> (e.g. <code>.update_layout()</code> or <code>.add_trace()</code>, which all accept <a href="/python/creating-and-updating-figures/#magic-underscore-notation">"magic underscore" notation</a>) as well as <a href="/python/renderers/">rendering them</a> (e.g. <code>.show()</code>) and <a href="/python/static-image-export/">exporting them to various formats</a> (e.g. <code>.to_json()</code> or <code>.write_image()</code> or <code>.write_html()</code>).</p>
<blockquote><p>Note: the functions in <a href="/python/plotly-express/">Plotly Express</a>, which is the recommended entry-point into the <code>plotly</code> library, are all built on top of graph objects, and all return instances of <code>plotly.graph_objects.Figure</code>.</p>
</blockquote>
<p>Every non-leaf attribute of a figure is represented by an instance of a class in the <code>plotly.graph_objects</code> hierarchy. For example, a figure <code>fig</code> can have an attribute <code>layout.margin</code>, which contains attributes <code>t</code>, <code>l</code>, <code>b</code> and <code>r</code> which are leaves of the tree: they have no children. The field at <code>fig.layout</code> is an object of class <a href="https://plotly.com/python-api-reference/generated/plotly.graph_objects.Layout.html"><code>plotly.graph_objects.Layout</code></a> and <code>fig.layout.margin</code> is an object of class <code>plotly.graph_objects.layout.Margin</code> which represents the <code>margin</code> node, and it has fields <code>t</code>, <code>l</code>, <code>b</code> and <code>r</code>, containing the values of the respective leaf-nodes. Note that specifying all of these values can be done without creating intermediate objects using <a href="/python/creating-and-updating-figures/#magic-underscore-notation">"magic underscore" notation</a>: <code>go.Figure(layout_margin=dict(t=10, b=10, r=10, l=10))</code>.</p>
<p>The objects contained in the list which is the <a href="/python/figure-structure/">value of the attribute <code>data</code> are called "traces"</a>, and can be of one of more than 40 possible types, each of which has a corresponding class in <code>plotly.graph_objects</code>. For example, traces of type <code>scatter</code> are represented by instances of the class <code>plotly.graph_objects.Scatter</code>. This means that a figure constructed as <code>go.Figure(data=[go.Scatter(x=[1,2], y=[3,4)])</code> will have the JSON representation <code>{"data": [{"type": "scatter", "x": [1,2], "y": [3,4]}]}</code>.</p>
<h3 id="Graph-Objects-Compared-to-Dictionaries">Graph Objects Compared to Dictionaries<a class="anchor-link" href="#Graph-Objects-Compared-to-Dictionaries">¶</a></h3><p>Graph objects have several benefits compared to plain Python dictionaries:</p>
<ol>
<li>Graph objects provide precise data validation. If you provide an invalid property name or an invalid property value as the key to a graph object, an exception will be raised with a helpful error message describing the problem. This is not the case if you use plain Python dictionaries and lists to build your figures.</li>
<li>Graph objects contain descriptions of each valid property as Python docstrings, with a <a href="https://plotly.com/python-api-reference/">full API reference available</a>. You can use these docstrings in the development environment of your choice to learn about the available properties as an alternative to consulting the online <a href="/python/reference/index/">Full Reference</a>.</li>
<li>Properties of graph objects can be accessed using both dictionary-style key lookup (e.g. <code>fig["layout"]</code>) or class-style property access (e.g. <code>fig.layout</code>).</li>
<li>Graph objects support higher-level convenience functions for making updates to already constructed figures (<code>.update_layout()</code>, <code>.add_trace()</code> etc).</li>
<li>Graph object constructors and update methods accept "magic underscores" (e.g. <code>go.Figure(layout_title_text="The Title")</code> rather than <code>dict(layout=dict(title=dict(text="The Title")))</code>) for more compact code.</li>
<li>Graph objects support attached rendering (<code>.show()</code>) and exporting functions (<code>.write_image()</code>) that automatically invoke the appropriate functions from <a href="https://plotly.com/python-api-reference/plotly.io.html">the <code>plotly.io</code> module</a>.</li>
</ol>
<h3 id="When-to-use-Graph-Objects-vs-Plotly-Express">When to use Graph Objects vs Plotly Express<a class="anchor-link" href="#When-to-use-Graph-Objects-vs-Plotly-Express">¶</a></h3><p>The recommended way to create figures is using the <a href="https://plotly.com/python-api-reference/">functions in the plotly.express module</a>, <a href="/python/plotly-express/">collectively known as Plotly Express</a>, which all return instances of <code>plotly.graph_objects.Figure</code>, so every figure produced with the <code>plotly</code> library actually uses graph objects under the hood, unless manually constructed out of dictionaries.</p>
<p>That said, certain kinds of figures are not yet possible to create with Plotly Express, such as figures that use certain 3D trace-types like <a href="/python/3d-mesh/"><code>mesh</code></a> or <a href="/python/3d-isosurface-plots/"><code>isosurface</code></a>. In addition, certain figures are cumbersome to create by starting from a figure created with Plotly Express, for example figures with <a href="/python/mixed-subplots/">subplots of different types</a>, <a href="/python/multiple-axes/">dual-axis plots</a>, or <a href="/python/facet-plots/">faceted plots</a> with multiple different types of traces. To construct such figures, it can be easier to start from an empty <code>plotly.graph_objects.Figure</code> object (or one configured with subplots via the <a href="/python/subplots/">make_subplots() function</a>) and progressively add traces and update attributes as above. Every <code>plotly</code> documentation page lists the Plotly Express option at the top if a Plotly Express function exists to make the kind of chart in question, and then the graph objects version below.</p>
<p>Note that the figures produced by Plotly Express <strong>in a single function-call</strong> are <a href="/python/styling-plotly-express/">easy to customize at creation-time</a>, and to <a href="/python/creating-and-updating-figures/">manipulate after creation</a> using the <code>update_*</code> and <code>add_*</code> methods.</p>
<h3 id="Comparing-Graph-Objects-and-Plotly-Express">Comparing Graph Objects and Plotly Express<a class="anchor-link" href="#Comparing-Graph-Objects-and-Plotly-Express">¶</a></h3><p>The figures produced by Plotly Express can always be built from the ground up using graph objects, but this approach typically takes <strong>5-100 lines of code rather than 1</strong>.</p>
<p>Here is a simple example of how to produce the same figure object from the same data, once with Plotly Express and once without. Note that <a href="/python-api-reference/plotly.express.html">Plotly Express functions</a> like <a href="/python/bar-charts/"><code>px.bar()</code></a> can accept a DataFrame as their first argument with column names passed to the <code>x</code> and <code>y</code> arguments, while <a href="/python-api-reference/plotly.graph_objects.html">Graph Objects functions</a> like <a href="/python/bar-charts/#basic-bar-charts-with-plotlygraphobjects"><code>go.Bar()</code></a> require the data values to be passed directly to the <code>x</code> and <code>y</code> arguments as a tuple, list, NumPy array, or Pandas Series.</p>
<p>The data in this example is in "long form" but <a href="/python/wide-form/">Plotly Express also accepts data in "wide form"</a> and the line-count savings from Plotly Express over graph objects are comparable. More complex figures such as <a href="/python/sunburst-charts/">sunbursts</a>, <a href="/python/parallel-coordinates-plot/">parallel coordinates</a>, <a href="/python/facet-plots/">facet plots</a> or <a href="/python/animations/">animations</a> require many more lines of figure-specific graph objects code, whereas switching from one representation to another with Plotly Express usually involves changing just a few characters.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">pandas</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">pd</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span>
<span class="s2">"Fruit"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"Apples"</span><span class="p">,</span> <span class="s2">"Oranges"</span><span class="p">,</span> <span class="s2">"Bananas"</span><span class="p">,</span> <span class="s2">"Apples"</span><span class="p">,</span> <span class="s2">"Oranges"</span><span class="p">,</span> <span class="s2">"Bananas"</span><span class="p">],</span>
<span class="s2">"Contestant"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"Alex"</span><span class="p">,</span> <span class="s2">"Alex"</span><span class="p">,</span> <span class="s2">"Alex"</span><span class="p">,</span> <span class="s2">"Jordan"</span><span class="p">,</span> <span class="s2">"Jordan"</span><span class="p">,</span> <span class="s2">"Jordan"</span><span class="p">],</span>
<span class="s2">"Number Eaten"</span><span class="p">:</span> <span class="p">[</span><span class="mi">2</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">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span>
<span class="p">})</span>
<span class="c1"># Plotly Express</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">plotly.express</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">px</span>
<span class="n">fig</span> <span class="o">=</span> <span class="n">px</span><span class="o">.</span><span class="n">bar</span><span class="p">(</span><span class="n">df</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="s2">"Fruit"</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="s2">"Number Eaten"</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s2">"Contestant"</span><span class="p">,</span> <span class="n">barmode</span><span class="o">=</span><span class="s2">"group"</span><span class="p">)</span>
<span class="n">fig</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
<span class="c1"># Graph Objects</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">plotly.graph_objects</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">go</span>
<span class="n">fig</span> <span class="o">=</span> <span class="n">go</span><span class="o">.</span><span class="n">Figure</span><span class="p">()</span>
<span class="k">for</span> <span class="n">contestant</span><span class="p">,</span> <span class="n">group</span> <span class="ow">in</span> <span class="n">df</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="s2">"Contestant"</span><span class="p">):</span>
<span class="n">fig</span><span class="o">.</span><span class="n">add_trace</span><span class="p">(</span><span class="n">go</span><span class="o">.</span><span class="n">Bar</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="n">group</span><span class="p">[</span><span class="s2">"Fruit"</span><span class="p">],</span> <span class="n">y</span><span class="o">=</span><span class="n">group</span><span class="p">[</span><span class="s2">"Number Eaten"</span><span class="p">],</span> <span class="n">name</span><span class="o">=</span><span class="n">contestant</span><span class="p">,</span>
<span class="n">hovertemplate</span><span class="o">=</span><span class="s2">"Contestant=</span><span class="si">%s</span><span class="s2"><br>Fruit=</span><span class="si">%%{x}</span><span class="s2"><br>Number Eaten=</span><span class="si">%%{y}</span><span class="s2"><extra></extra>"</span><span class="o">%</span> <span class="n">contestant</span><span class="p">))</span>
<span class="n">fig</span><span class="o">.</span><span class="n">update_layout</span><span class="p">(</span><span class="n">legend_title_text</span> <span class="o">=</span> <span class="s2">"Contestant"</span><span class="p">)</span>
<span class="n">fig</span><span class="o">.</span><span class="n">update_xaxes</span><span class="p">(</span><span class="n">title_text</span><span class="o">=</span><span class="s2">"Fruit"</span><span class="p">)</span>
<span class="n">fig</span><span class="o">.</span><span class="n">update_yaxes</span><span class="p">(</span><span class="n">title_text</span><span class="o">=</span><span class="s2">"Number Eaten"</span><span class="p">)</span>
<span class="n">fig</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt"></div>
<div class="output_html rendered_html output_subarea ">
<script>
window.PlotlyConfig = {MathJaxConfig: 'local'};
if (window.MathJax && window.MathJax.Hub && window.MathJax.Hub.Config) {window.MathJax.Hub.Config({SVG: {font: "STIX-Web"}});}
</script>
<script type="module">import "https://cdn.plot.ly/plotly-4.0.0.min"</script>
</div>
</div>
<div class="output_area">
<div class="prompt"></div>
<div class="output_html rendered_html output_subarea ">
<div style="height:525px; width:100%;"> <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-svg.min.js"></script><script>if (window.MathJax && window.MathJax.Hub && window.MathJax.Hub.Config) {window.MathJax.Hub.Config({SVG: {font: "STIX-Web"}});}</script> <script>window.PlotlyConfig = {MathJaxConfig: 'local'};</script>
<script charset="utf-8" src="https://cdn.plot.ly/plotly-4.0.0.min.js" integrity="sha256-FEYfO0yRyLtZCpnW0Dw/0DHKQO7Afrq3ml4+rBB818o=" crossorigin="anonymous"></script> <div id="525fc4c3-b102-4b04-b5af-e1f73a6181ae" class="plotly-graph-div" style="height:100%; width:100%;"></div> <script> window.PLOTLYENV=window.PLOTLYENV || {}; if (document.getElementById("525fc4c3-b102-4b04-b5af-e1f73a6181ae")) { Plotly.newPlot( "525fc4c3-b102-4b04-b5af-e1f73a6181ae", [{"alignmentgroup":"True","hovertemplate":"Contestant=Alex\u003cbr\u003eFruit=%{x}\u003cbr\u003eNumber Eaten=%{y}\u003cextra\u003e\u003c\u002fextra\u003e","legendgroup":"Alex","marker":{"color":"#636efa","pattern":{"shape":""}},"name":"Alex","offsetgroup":"Alex","orientation":"v","showlegend":true,"textposition":"auto","x":["Apples","Oranges","Bananas"],"xaxis":"x","y":{"dtype":"i1","bdata":"AgED"},"yaxis":"y","type":"bar"},{"alignmentgroup":"True","hovertemplate":"Contestant=Jordan\u003cbr\u003eFruit=%{x}\u003cbr\u003eNumber Eaten=%{y}\u003cextra\u003e\u003c\u002fextra\u003e","legendgroup":"Jordan","marker":{"color":"#EF553B","pattern":{"shape":""}},"name":"Jordan","offsetgroup":"Jordan","orientation":"v","showlegend":true,"textposition":"auto","x":["Apples","Oranges","Bananas"],"xaxis":"x","y":{"dtype":"i1","bdata":"AQMC"},"yaxis":"y","type":"bar"}], {"template":{"data":{"histogram2dcontour":[{"type":"histogram2dcontour","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"choropleth":[{"type":"choropleth","colorbar":{"outlinewidth":0,"ticks":""}}],"histogram2d":[{"type":"histogram2d","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"heatmap":[{"type":"heatmap","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"contourcarpet":[{"type":"contourcarpet","colorbar":{"outlinewidth":0,"ticks":""}}],"contour":[{"type":"contour","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"surface":[{"type":"surface","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"mesh3d":[{"type":"mesh3d","colorbar":{"outlinewidth":0,"ticks":""}}],"scatter":[{"fillpattern":{"fillmode":"overlay","size":10,"solidity":0.2},"type":"scatter"}],"parcoords":[{"type":"parcoords","line":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scatterpolargl":[{"type":"scatterpolargl","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"bar":[{"error_x":{"color":"#2a3f5f"},"error_y":{"color":"#2a3f5f"},"marker":{"line":{"color":"#E5ECF6","width":0.5},"pattern":{"fillmode":"overlay","size":10,"solidity":0.2}},"type":"bar"}],"scattergeo":[{"type":"scattergeo","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scatterpolar":[{"type":"scatterpolar","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"histogram":[{"marker":{"pattern":{"fillmode":"overlay","size":10,"solidity":0.2}},"type":"histogram"}],"scattergl":[{"type":"scattergl","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scatter3d":[{"type":"scatter3d","line":{"colorbar":{"outlinewidth":0,"ticks":""}},"marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scattermap":[{"type":"scattermap","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scatterternary":[{"type":"scatterternary","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scattercarpet":[{"type":"scattercarpet","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"carpet":[{"aaxis":{"endlinecolor":"#2a3f5f","gridcolor":"white","linecolor":"white","minorgridcolor":"white","startlinecolor":"#2a3f5f"},"baxis":{"endlinecolor":"#2a3f5f","gridcolor":"white","linecolor":"white","minorgridcolor":"white","startlinecolor":"#2a3f5f"},"type":"carpet"}],"table":[{"cells":{"fill":{"color":"#EBF0F8"},"line":{"color":"white"}},"header":{"fill":{"color":"#C8D4E3"},"line":{"color":"white"}},"type":"table"}],"barpolar":[{"marker":{"line":{"color":"#E5ECF6","width":0.5},"pattern":{"fillmode":"overlay","size":10,"solidity":0.2}},"type":"barpolar"}],"pie":[{"automargin":true,"type":"pie"}]},"layout":{"autotypenumbers":"strict","colorway":["#636efa","#EF553B","#00cc96","#ab63fa","#FFA15A","#19d3f3","#FF6692","#B6E880","#FF97FF","#FECB52"],"font":{"color":"#2a3f5f"},"hovermode":"closest","hoverlabel":{"align":"left"},"paper_bgcolor":"white","plot_bgcolor":"#E5ECF6","polar":{"bgcolor":"#E5ECF6","angularaxis":{"gridcolor":"white","linecolor":"white","ticks":""},"radialaxis":{"gridcolor":"white","linecolor":"white","ticks":""}},"ternary":{"bgcolor":"#E5ECF6","aaxis":{"gridcolor":"white","linecolor":"white","ticks":""},"baxis":{"gridcolor":"white","linecolor":"white","ticks":""},"caxis":{"gridcolor":"white","linecolor":"white","ticks":""}},"coloraxis":{"colorbar":{"outlinewidth":0,"ticks":""}},"colorscale":{"sequential":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]],"sequentialminus":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]],"diverging":[[0,"#8e0152"],[0.1,"#c51b7d"],[0.2,"#de77ae"],[0.3,"#f1b6da"],[0.4,"#fde0ef"],[0.5,"#f7f7f7"],[0.6,"#e6f5d0"],[0.7,"#b8e186"],[0.8,"#7fbc41"],[0.9,"#4d9221"],[1,"#276419"]]},"xaxis":{"gridcolor":"white","linecolor":"white","ticks":"","title":{"standoff":15},"zerolinecolor":"white","automargin":true,"zerolinewidth":2},"yaxis":{"gridcolor":"white","linecolor":"white","ticks":"","title":{"standoff":15},"zerolinecolor":"white","automargin":true,"zerolinewidth":2},"scene":{"xaxis":{"backgroundcolor":"#E5ECF6","gridcolor":"white","linecolor":"white","showbackground":true,"ticks":"","zerolinecolor":"white","gridwidth":2},"yaxis":{"backgroundcolor":"#E5ECF6","gridcolor":"white","linecolor":"white","showbackground":true,"ticks":"","zerolinecolor":"white","gridwidth":2},"zaxis":{"backgroundcolor":"#E5ECF6","gridcolor":"white","linecolor":"white","showbackground":true,"ticks":"","zerolinecolor":"white","gridwidth":2}},"shapedefaults":{"line":{"color":"#2a3f5f"}},"annotationdefaults":{"arrowcolor":"#2a3f5f","arrowhead":0,"arrowwidth":1},"geo":{"bgcolor":"white","landcolor":"#E5ECF6","subunitcolor":"white","showland":true,"showlakes":true,"lakecolor":"white"},"title":{"x":0.05}}},"xaxis":{"anchor":"y","domain":[0.0,1.0],"title":{"text":"Fruit"}},"yaxis":{"anchor":"x","domain":[0.0,1.0],"title":{"text":"Number Eaten"}},"legend":{"title":{"text":"Contestant"},"tracegroupgap":0},"margin":{"t":60},"barmode":"group"}, {"responsive": true} ).then(function(){
var gd = document.getElementById('525fc4c3-b102-4b04-b5af-e1f73a6181ae');
var x = new MutationObserver(function (mutations, observer) {{
var display = window.getComputedStyle(gd).display;
if (!display || display === 'none') {{
console.log([gd, 'removed!']);
Plotly.purge(gd);
observer.disconnect();
}}
}});
// Listen for the removal of the full notebook cells
var notebookContainer = gd.closest('#notebook-container');
if (notebookContainer) {{
x.observe(notebookContainer, {childList: true});
}}
// Listen for the clearing of the current output cell
var outputEl = gd.closest('.output');
if (outputEl) {{
x.observe(outputEl, {childList: true});
}}
}) }; </script> </div>
</div>
</div>
<div class="output_area">
<div class="prompt"></div>
<div class="output_html rendered_html output_subarea ">
<div style="height:525px; width:100%;"> <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-svg.min.js"></script><script>if (window.MathJax && window.MathJax.Hub && window.MathJax.Hub.Config) {window.MathJax.Hub.Config({SVG: {font: "STIX-Web"}});}</script> <script>window.PlotlyConfig = {MathJaxConfig: 'local'};</script>
<script charset="utf-8" src="https://cdn.plot.ly/plotly-4.0.0.min.js" integrity="sha256-FEYfO0yRyLtZCpnW0Dw/0DHKQO7Afrq3ml4+rBB818o=" crossorigin="anonymous"></script> <div id="df1e4e70-c9a9-449d-b8d1-47843b2a3dfc" class="plotly-graph-div" style="height:100%; width:100%;"></div> <script> window.PLOTLYENV=window.PLOTLYENV || {}; if (document.getElementById("df1e4e70-c9a9-449d-b8d1-47843b2a3dfc")) { Plotly.newPlot( "df1e4e70-c9a9-449d-b8d1-47843b2a3dfc", [{"hovertemplate":"Contestant=Alex\u003cbr\u003eFruit=%{x}\u003cbr\u003eNumber Eaten=%{y}\u003cextra\u003e\u003c\u002fextra\u003e","name":"Alex","x":["Apples","Oranges","Bananas"],"y":{"dtype":"i1","bdata":"AgED"},"type":"bar"},{"hovertemplate":"Contestant=Jordan\u003cbr\u003eFruit=%{x}\u003cbr\u003eNumber Eaten=%{y}\u003cextra\u003e\u003c\u002fextra\u003e","name":"Jordan","x":["Apples","Oranges","Bananas"],"y":{"dtype":"i1","bdata":"AQMC"},"type":"bar"}], {"template":{"data":{"histogram2dcontour":[{"type":"histogram2dcontour","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"choropleth":[{"type":"choropleth","colorbar":{"outlinewidth":0,"ticks":""}}],"histogram2d":[{"type":"histogram2d","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"heatmap":[{"type":"heatmap","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"contourcarpet":[{"type":"contourcarpet","colorbar":{"outlinewidth":0,"ticks":""}}],"contour":[{"type":"contour","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"surface":[{"type":"surface","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"mesh3d":[{"type":"mesh3d","colorbar":{"outlinewidth":0,"ticks":""}}],"scatter":[{"fillpattern":{"fillmode":"overlay","size":10,"solidity":0.2},"type":"scatter"}],"parcoords":[{"type":"parcoords","line":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scatterpolargl":[{"type":"scatterpolargl","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"bar":[{"error_x":{"color":"#2a3f5f"},"error_y":{"color":"#2a3f5f"},"marker":{"line":{"color":"#E5ECF6","width":0.5},"pattern":{"fillmode":"overlay","size":10,"solidity":0.2}},"type":"bar"}],"scattergeo":[{"type":"scattergeo","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scatterpolar":[{"type":"scatterpolar","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"histogram":[{"marker":{"pattern":{"fillmode":"overlay","size":10,"solidity":0.2}},"type":"histogram"}],"scattergl":[{"type":"scattergl","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scatter3d":[{"type":"scatter3d","line":{"colorbar":{"outlinewidth":0,"ticks":""}},"marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scattermap":[{"type":"scattermap","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scatterternary":[{"type":"scatterternary","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scattercarpet":[{"type":"scattercarpet","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"carpet":[{"aaxis":{"endlinecolor":"#2a3f5f","gridcolor":"white","linecolor":"white","minorgridcolor":"white","startlinecolor":"#2a3f5f"},"baxis":{"endlinecolor":"#2a3f5f","gridcolor":"white","linecolor":"white","minorgridcolor":"white","startlinecolor":"#2a3f5f"},"type":"carpet"}],"table":[{"cells":{"fill":{"color":"#EBF0F8"},"line":{"color":"white"}},"header":{"fill":{"color":"#C8D4E3"},"line":{"color":"white"}},"type":"table"}],"barpolar":[{"marker":{"line":{"color":"#E5ECF6","width":0.5},"pattern":{"fillmode":"overlay","size":10,"solidity":0.2}},"type":"barpolar"}],"pie":[{"automargin":true,"type":"pie"}]},"layout":{"autotypenumbers":"strict","colorway":["#636efa","#EF553B","#00cc96","#ab63fa","#FFA15A","#19d3f3","#FF6692","#B6E880","#FF97FF","#FECB52"],"font":{"color":"#2a3f5f"},"hovermode":"closest","hoverlabel":{"align":"left"},"paper_bgcolor":"white","plot_bgcolor":"#E5ECF6","polar":{"bgcolor":"#E5ECF6","angularaxis":{"gridcolor":"white","linecolor":"white","ticks":""},"radialaxis":{"gridcolor":"white","linecolor":"white","ticks":""}},"ternary":{"bgcolor":"#E5ECF6","aaxis":{"gridcolor":"white","linecolor":"white","ticks":""},"baxis":{"gridcolor":"white","linecolor":"white","ticks":""},"caxis":{"gridcolor":"white","linecolor":"white","ticks":""}},"coloraxis":{"colorbar":{"outlinewidth":0,"ticks":""}},"colorscale":{"sequential":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]],"sequentialminus":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]],"diverging":[[0,"#8e0152"],[0.1,"#c51b7d"],[0.2,"#de77ae"],[0.3,"#f1b6da"],[0.4,"#fde0ef"],[0.5,"#f7f7f7"],[0.6,"#e6f5d0"],[0.7,"#b8e186"],[0.8,"#7fbc41"],[0.9,"#4d9221"],[1,"#276419"]]},"xaxis":{"gridcolor":"white","linecolor":"white","ticks":"","title":{"standoff":15},"zerolinecolor":"white","automargin":true,"zerolinewidth":2},"yaxis":{"gridcolor":"white","linecolor":"white","ticks":"","title":{"standoff":15},"zerolinecolor":"white","automargin":true,"zerolinewidth":2},"scene":{"xaxis":{"backgroundcolor":"#E5ECF6","gridcolor":"white","linecolor":"white","showbackground":true,"ticks":"","zerolinecolor":"white","gridwidth":2},"yaxis":{"backgroundcolor":"#E5ECF6","gridcolor":"white","linecolor":"white","showbackground":true,"ticks":"","zerolinecolor":"white","gridwidth":2},"zaxis":{"backgroundcolor":"#E5ECF6","gridcolor":"white","linecolor":"white","showbackground":true,"ticks":"","zerolinecolor":"white","gridwidth":2}},"shapedefaults":{"line":{"color":"#2a3f5f"}},"annotationdefaults":{"arrowcolor":"#2a3f5f","arrowhead":0,"arrowwidth":1},"geo":{"bgcolor":"white","landcolor":"#E5ECF6","subunitcolor":"white","showland":true,"showlakes":true,"lakecolor":"white"},"title":{"x":0.05}}},"legend":{"title":{"text":"Contestant"}},"xaxis":{"title":{"text":"Fruit"}},"yaxis":{"title":{"text":"Number Eaten"}}}, {"responsive": true} ).then(function(){
var gd = document.getElementById('df1e4e70-c9a9-449d-b8d1-47843b2a3dfc');
var x = new MutationObserver(function (mutations, observer) {{
var display = window.getComputedStyle(gd).display;
if (!display || display === 'none') {{
console.log([gd, 'removed!']);
Plotly.purge(gd);
observer.disconnect();
}}
}});
// Listen for the removal of the full notebook cells
var notebookContainer = gd.closest('#notebook-container');
if (notebookContainer) {{
x.observe(notebookContainer, {childList: true});
}}
// Listen for the clearing of the current output cell
var outputEl = gd.closest('.output');
if (outputEl) {{
x.observe(outputEl, {childList: true});
}}
}) }; </script> </div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="What-About-Dash?">What About Dash?<a class="anchor-link" href="#What-About-Dash?">¶</a></h3><p><a href="https://dash.plot.ly/">Dash</a> is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.</p>
<p>Learn about how to install Dash at <a href="https://dash.plot.ly/installation">https://dash.plot.ly/installation</a>.</p>
<p>Everywhere in this page that you see <code>fig.show()</code>, you can display the same figure in a Dash application by passing it to the <code>figure</code> argument of the <a href="https://dash.plot.ly/dash-core-components/graph"><code>Graph</code> component</a> from the built-in <code>dash_core_components</code> package like this:</p>
<div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">plotly.graph_objects</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">go</span> <span class="c1"># or plotly.express as px</span>
<span class="n">fig</span> <span class="o">=</span> <span class="n">go</span><span class="o">.</span><span class="n">Figure</span><span class="p">()</span> <span class="c1"># or any Plotly Express function e.g. px.bar(...)</span>
<span class="c1"># fig.add_trace( ... )</span>
<span class="c1"># fig.update_layout( ... )</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">dash</span><span class="w"> </span><span class="kn">import</span> <span class="n">Dash</span><span class="p">,</span> <span class="n">dcc</span><span class="p">,</span> <span class="n">html</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">Dash</span><span class="p">()</span>
<span class="n">app</span><span class="o">.</span><span class="n">layout</span> <span class="o">=</span> <span class="n">html</span><span class="o">.</span><span class="n">Div</span><span class="p">([</span>
<span class="n">dcc</span><span class="o">.</span><span class="n">Graph</span><span class="p">(</span><span class="n">figure</span><span class="o">=</span><span class="n">fig</span><span class="p">)</span>
<span class="p">])</span>
<span class="n">app</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">debug</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">use_reloader</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> <span class="c1"># Turn off reloader if inside Jupyter</span>
</pre></div>
</div>
</div>
</div>
{% endraw %}