├── README.md
└── notebooks
├── 01- Line Plots.ipynb
├── 02- Scatter Plots.ipynb
├── 03- Bar Charts.ipynb
├── 04- Pie Charts.ipynb
├── 05- Sunburst Charts.ipynb
├── 06- Bubble Charts.ipynb
├── 07- Gantt Chart.ipynb
├── 08- Error Bars.ipynb
├── 09- Box Plots.ipynb
├── 10- Scatterplot Matrix.ipynb
├── 11- Histograms.ipynb
├── 12- Distribution Plots .ipynb
├── 13- Violin Plots .ipynb
├── 14- Contour Plot.ipynb
├── 15- Table.ipynb
├── Hover Text.ipynb
└── Static Image Export.ipynb
/README.md:
--------------------------------------------------------------------------------
1 | # Interactive Plotting with Python's Plotly
2 |
3 | Please see the notebooks for detailed code!
--------------------------------------------------------------------------------
/notebooks/02- Scatter Plots.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Scatter Plots\n",
8 | "A scatter plot (also called a scatterplot, scatter graph, scatter chart, scattergram, or scatter diagram) is a type of plot or mathematical diagram using Cartesian coordinates to display values for typically two variables for a set of data. If the points are coded (color/shape/size), one additional variable can be displayed. The data are displayed as a collection of points, each having the value of one variable determining the position on the horizontal axis and the value of the other variable determining the position on the vertical axis. [Scatter Plot on Wikipedia](https://en.wikipedia.org/wiki/Scatter_plot)\n",
9 | "\n",
10 | "* Scatter plots, X-Y plots are used to determine relationships between the two different things.\n",
11 | "* The x-axis is used to measure one event (or variable) and the y-axis is used to measure the other.\n",
12 | "* If both variables increase at the same time, they have a positive relationship. If one variable decreases while the other increases, they have a negative relationship. Sometimes the variables don't follow any pattern and have no relationship. [Source](https://nces.ed.gov/nceskids/help/user_guide/graph/scatter.asp)\n",
13 | "\n",
14 | "\n",
15 | "* Scatter plots are used to show the relationship between pairs of quantitative measurements made for the same object or individual.\n",
16 | "* For example, a scatter plot could be used to present information about the examination and coursework marks for each of the students in a class. \n",
17 | "* In a scatter plot a dot represents each individual or object (child in this case) and is located with reference to the x-axis and y-axis, each of which represent one of the two measurements.\n",
18 | "* By analysing the pattern of dots that make up a scatter plot it is possible to identify whether there is any systematic or causal relationship between the two measurements.\n",
19 | "* Regression lines can also be added to the graph and used to decide whether the relationship between the two sets of measurements can be explained or if it is due to chance. [Source](https://www.le.ac.uk/oerresources/ssds/numeracyskills/page_35.htm)"
20 | ]
21 | },
22 | {
23 | "cell_type": "markdown",
24 | "metadata": {},
25 | "source": [
26 | "## Scatter plot with Plotly Express"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": null,
32 | "metadata": {},
33 | "outputs": [],
34 | "source": [
35 | "# x and y given as array_like objects\n",
36 | "import plotly.express as px\n",
37 | "fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])\n",
38 | "fig.show()"
39 | ]
40 | },
41 | {
42 | "cell_type": "code",
43 | "execution_count": null,
44 | "metadata": {},
45 | "outputs": [],
46 | "source": [
47 | "# x and y given as DataFrame columns\n",
48 | "import plotly.express as px\n",
49 | "df = px.data.iris() # iris is a pandas DataFrame\n",
50 | "fig = px.scatter(df, x=\"sepal_width\", y=\"sepal_length\")\n",
51 | "fig.show()\n",
52 | "#df.head()"
53 | ]
54 | },
55 | {
56 | "cell_type": "code",
57 | "execution_count": null,
58 | "metadata": {},
59 | "outputs": [],
60 | "source": [
61 | "df['species'].unique()"
62 | ]
63 | },
64 | {
65 | "cell_type": "markdown",
66 | "metadata": {},
67 | "source": [
68 | "## Set size and color with column names\n",
69 | "Note that *color* and *size* data are added to hover information. You can add other columns to hover data with the *hover_data* argument of *px.scatter*."
70 | ]
71 | },
72 | {
73 | "cell_type": "code",
74 | "execution_count": null,
75 | "metadata": {},
76 | "outputs": [],
77 | "source": [
78 | "import plotly.express as px\n",
79 | "df = px.data.iris()\n",
80 | "fig = px.scatter(df, x=\"sepal_width\", y=\"sepal_length\", color=\"species\",\n",
81 | " size='petal_length', hover_data=['petal_width'])\n",
82 | "fig.show()"
83 | ]
84 | },
85 | {
86 | "cell_type": "markdown",
87 | "metadata": {},
88 | "source": [
89 | "## Scatter plot with go.Scatter (Graphic Object)\n",
90 | "We have seen how to generate Scatter and Line plots together in the Line Plots notebook!\n",
91 | "\n",
92 | "We can customize the Scatter object by setting the *mode* option .. possible values are '*lines*', '*markers*' and '*lines+marker*'"
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": null,
98 | "metadata": {},
99 | "outputs": [],
100 | "source": [
101 | "import plotly.graph_objects as go\n",
102 | "import numpy as np\n",
103 | "\n",
104 | "N = 1000\n",
105 | "t = np.linspace(0, 10, 100)\n",
106 | "y = np.sin(t)\n",
107 | "\n",
108 | "fig = go.Figure(data=go.Scatter(x=t, y=y, mode='markers'))\n",
109 | "\n",
110 | "fig.show()"
111 | ]
112 | },
113 | {
114 | "cell_type": "markdown",
115 | "metadata": {},
116 | "source": [
117 | "## Bubble Scatter Plots\n",
118 | "\n",
119 | "In bubble charts, a third dimension of the data is shown through the size of markers (more in the Bubble Charts notebook)."
120 | ]
121 | },
122 | {
123 | "cell_type": "code",
124 | "execution_count": null,
125 | "metadata": {},
126 | "outputs": [],
127 | "source": [
128 | "import plotly.graph_objects as go\n",
129 | "\n",
130 | "fig = go.Figure(data=go.Scatter(\n",
131 | " x=[1, 2, 3, 4],\n",
132 | " y=[10, 11, 12, 13],\n",
133 | " mode='markers',\n",
134 | " marker=dict(size=[40, 60, 80, 100],\n",
135 | " color=[0, 1, 2, 3])\n",
136 | "))\n",
137 | "\n",
138 | "fig.show()"
139 | ]
140 | },
141 | {
142 | "cell_type": "markdown",
143 | "metadata": {},
144 | "source": [
145 | "## Style Scatter Plots\n",
146 | "\n",
147 | "You will see that customizing Plotly plots is fun and easy!"
148 | ]
149 | },
150 | {
151 | "cell_type": "code",
152 | "execution_count": null,
153 | "metadata": {},
154 | "outputs": [],
155 | "source": [
156 | "import plotly.graph_objects as go\n",
157 | "import numpy as np\n",
158 | "\n",
159 | "\n",
160 | "t = np.linspace(0, 10, 100)\n",
161 | "\n",
162 | "fig = go.Figure()\n",
163 | "\n",
164 | "fig.add_trace(go.Scatter(\n",
165 | " x=t, y=np.sin(t),\n",
166 | " name='sin',\n",
167 | " mode='markers',\n",
168 | " marker_color='rgba(152, 0, 0, .8)'\n",
169 | "))\n",
170 | "\n",
171 | "fig.add_trace(go.Scatter(\n",
172 | " x=t, y=np.cos(t),\n",
173 | " name='cos',\n",
174 | " marker_color='rgba(255, 182, 193, .9)'\n",
175 | "))\n",
176 | "\n",
177 | "# Set options common to all traces with fig.update_traces\n",
178 | "fig.update_traces(mode='markers', marker_line_width=2, marker_size=10)\n",
179 | "fig.update_layout(title='Styled Scatter',\n",
180 | " yaxis_zeroline=False, xaxis_zeroline=False)\n",
181 | "\n",
182 | "\n",
183 | "fig.show()"
184 | ]
185 | },
186 | {
187 | "cell_type": "markdown",
188 | "metadata": {},
189 | "source": [
190 | "## Data Labels on Hover\n",
191 | "What do you want to"
192 | ]
193 | },
194 | {
195 | "cell_type": "code",
196 | "execution_count": null,
197 | "metadata": {},
198 | "outputs": [],
199 | "source": [
200 | "import plotly.graph_objects as go\n",
201 | "import pandas as pd\n",
202 | "\n",
203 | "data= pd.read_csv(\"https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv\")\n",
204 | "\n",
205 | "fig = go.Figure(data=go.Scatter(x=data['Postal'],\n",
206 | " y=data['Population'],\n",
207 | " mode='markers',\n",
208 | " marker_color=data['Population'],\n",
209 | " text=data['State'])) # hover text goes here\n",
210 | "\n",
211 | "fig.update_layout(title='Population of USA States')\n",
212 | "fig.show()"
213 | ]
214 | },
215 | {
216 | "cell_type": "markdown",
217 | "metadata": {},
218 | "source": [
219 | "## Scatter with a Color Dimension"
220 | ]
221 | },
222 | {
223 | "cell_type": "code",
224 | "execution_count": null,
225 | "metadata": {},
226 | "outputs": [],
227 | "source": [
228 | "import plotly.graph_objects as go\n",
229 | "import numpy as np\n",
230 | "\n",
231 | "fig = go.Figure(data=go.Scatter(\n",
232 | " y = np.random.randn(500),\n",
233 | " mode='markers',\n",
234 | " marker=dict(\n",
235 | " size=16,\n",
236 | " color=np.random.randn(500), #set color equal to a variable\n",
237 | " colorscale='Peach', # one of plotly colorscales\n",
238 | " showscale=True\n",
239 | " )\n",
240 | "))\n",
241 | "\n",
242 | "fig.show()"
243 | ]
244 | },
245 | {
246 | "cell_type": "markdown",
247 | "metadata": {},
248 | "source": [
249 | "## Large Data Sets"
250 | ]
251 | },
252 | {
253 | "cell_type": "code",
254 | "execution_count": null,
255 | "metadata": {},
256 | "outputs": [],
257 | "source": [
258 | "%%time\n",
259 | "import plotly.graph_objects as go\n",
260 | "import numpy as np\n",
261 | "\n",
262 | "N = 100000\n",
263 | "# notice Scattergl not Scatter\n",
264 | "fig = go.Figure(data=go.Scattergl(\n",
265 | " x = np.random.randn(N),\n",
266 | " y = np.random.randn(N),\n",
267 | " mode='markers',\n",
268 | " marker=dict(\n",
269 | " color=np.random.randn(N),\n",
270 | " colorscale='Viridis',\n",
271 | " line_width=1\n",
272 | " )\n",
273 | "))\n",
274 | "\n",
275 | "fig.show()"
276 | ]
277 | },
278 | {
279 | "cell_type": "markdown",
280 | "metadata": {},
281 | "source": [
282 | "## 3D Scatter Plot"
283 | ]
284 | },
285 | {
286 | "cell_type": "code",
287 | "execution_count": null,
288 | "metadata": {},
289 | "outputs": [],
290 | "source": [
291 | "import plotly.graph_objs as go\n",
292 | "import numpy as np\n",
293 | "z = np.linspace(0, 10, 50)\n",
294 | "x = np.cos(z)\n",
295 | "y = np.sin(z)\n",
296 | "\n",
297 | "#N = 1000\n",
298 | "#t = np.linspace(0, 10, 100)\n",
299 | "#y = np.sin(t)\n",
300 | "\n",
301 | "trace=go.Scatter3d(x=x, y=y, z=z,mode='markers',\n",
302 | " marker=dict(\n",
303 | " size=12,\n",
304 | " color=z, # set color to an array/list of desired values\n",
305 | " colorscale='Viridis')\n",
306 | ")\n",
307 | "fig = go.Figure(trace)\n",
308 | "fig.show()\n"
309 | ]
310 | },
311 | {
312 | "cell_type": "code",
313 | "execution_count": null,
314 | "metadata": {},
315 | "outputs": [],
316 | "source": []
317 | }
318 | ],
319 | "metadata": {
320 | "kernelspec": {
321 | "display_name": "Python 3",
322 | "language": "python",
323 | "name": "python3"
324 | },
325 | "language_info": {
326 | "codemirror_mode": {
327 | "name": "ipython",
328 | "version": 3
329 | },
330 | "file_extension": ".py",
331 | "mimetype": "text/x-python",
332 | "name": "python",
333 | "nbconvert_exporter": "python",
334 | "pygments_lexer": "ipython3",
335 | "version": "3.7.6"
336 | }
337 | },
338 | "nbformat": 4,
339 | "nbformat_minor": 2
340 | }
341 |
--------------------------------------------------------------------------------
/notebooks/03- Bar Charts.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Bar Charts\n",
8 | "\n",
9 | "A bar chart or bar graph is a chart or graph that presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent. The bars can be plotted vertically or horizontally. A vertical bar chart is sometimes called a column chart. [Bar Chart on Wikipedia](https://en.wikipedia.org/wiki/Bar_chart)\n",
10 | "\n",
11 | "* Bar graphs are used to compare things between different groups or to track changes over time. However, when trying to measure change over time, bar graphs are best when the changes are larger.[Source](https://nces.ed.gov/nceskids/help/user_guide/graph/whentouse.asp)\n",
12 | "\n",
13 | "* Bar charts are one of the most commonly used types of graph and are used to display and compare the number, frequency or other measure (e.g. mean) for different discrete categories or groups.\n",
14 | "* The graph is constructed such that the heights or lengths of the different bars are proportional to the size of the category they represent.\n",
15 | "* Since the x-axis (the horizontal axis) represents the different categories it has no scale.\n",
16 | "* The y-axis (the vertical axis) does have a scale and this indicates the units of measurement.\n",
17 | "* The bars can be drawn either vertically or horizontally depending upon the number of categories and length or complexity of the category labels. \n",
18 | "* There are various ways in which bar charts can be constructed and this makes them a very flexible chart type.\n",
19 | "* For example, if there is more than one set of values for each category then grouped or component bar charts can be used to display the data. \n",
20 | "[Source](https://www.le.ac.uk/oerresources/ssds/numeracyskills/page_31.htm)"
21 | ]
22 | },
23 | {
24 | "cell_type": "markdown",
25 | "metadata": {},
26 | "source": [
27 | "## Bar chart with Plotly Express"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": null,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": [
36 | "# With px.bar, each row of the DataFrame is represented as a rectangular mark.\n",
37 | "import plotly.express as px\n",
38 | "data_italy = px.data.gapminder().query(\"country == 'Italy'\")\n",
39 | "fig = px.bar(data_italy, x='year', y='pop')\n",
40 | "fig.show()"
41 | ]
42 | },
43 | {
44 | "cell_type": "code",
45 | "execution_count": null,
46 | "metadata": {},
47 | "outputs": [],
48 | "source": [
49 | "#type(data_libya)"
50 | ]
51 | },
52 | {
53 | "cell_type": "markdown",
54 | "metadata": {},
55 | "source": [
56 | "## Customize bar chart with Plotly Express"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": null,
62 | "metadata": {},
63 | "outputs": [],
64 | "source": [
65 | "# We can use a certain column in the data to color the bars\n",
66 | "import plotly.express as px\n",
67 | "data = px.data.gapminder()\n",
68 | "\n",
69 | "data_canada = data[data.country == 'Canada']\n",
70 | "fig = px.bar(data_canada, x='year', y='pop',\n",
71 | " hover_data=['lifeExp', 'gdpPercap'], color='lifeExp',\n",
72 | " labels={'pop':'population of Canada'}, height=400)\n",
73 | "fig.show()"
74 | ]
75 | },
76 | {
77 | "cell_type": "code",
78 | "execution_count": null,
79 | "metadata": {},
80 | "outputs": [],
81 | "source": [
82 | "# When several rows share the same value of x (here Female or Male), \n",
83 | "# the rectangles are stacked on top of one another by default.\n",
84 | "import plotly.express as px\n",
85 | "df = px.data.tips()\n",
86 | "fig = px.bar(df, x=\"sex\", y=\"total_bill\", color='time')\n",
87 | "fig.show()"
88 | ]
89 | },
90 | {
91 | "cell_type": "code",
92 | "execution_count": null,
93 | "metadata": {},
94 | "outputs": [],
95 | "source": [
96 | "df.head()"
97 | ]
98 | },
99 | {
100 | "cell_type": "code",
101 | "execution_count": null,
102 | "metadata": {},
103 | "outputs": [],
104 | "source": [
105 | "# Change the default stacking\n",
106 | "import plotly.express as px\n",
107 | "fig = px.bar(df, x=\"sex\", y=\"total_bill\", color='smoker', barmode='group',\n",
108 | " height=400)\n",
109 | "fig.show()"
110 | ]
111 | },
112 | {
113 | "cell_type": "markdown",
114 | "metadata": {},
115 | "source": [
116 | "## Facetted subplots\n",
117 | "Use the keyword arguments facet_row (resp. facet_col) to create facetted subplots, where different rows (resp. columns) correspond to different values of the dataframe column specified in facet_row."
118 | ]
119 | },
120 | {
121 | "cell_type": "code",
122 | "execution_count": null,
123 | "metadata": {},
124 | "outputs": [],
125 | "source": [
126 | "import plotly.express as px\n",
127 | "fig = px.bar(df, x=\"sex\", y=\"total_bill\", color=\"smoker\", barmode=\"group\",\n",
128 | " facet_row=\"time\", facet_col=\"day\",\n",
129 | " category_orders={\"day\": [\"Thur\", \"Fri\", \"Sat\", \"Sun\"],\n",
130 | " \"time\": [\"Lunch\", \"Dinner\"]})\n",
131 | "fig.show()"
132 | ]
133 | },
134 | {
135 | "cell_type": "markdown",
136 | "metadata": {},
137 | "source": [
138 | "# And here are examples on Bar Charts with Plotly's Graph Objects"
139 | ]
140 | },
141 | {
142 | "cell_type": "markdown",
143 | "metadata": {},
144 | "source": [
145 | "## A Simple Bar Chart"
146 | ]
147 | },
148 | {
149 | "cell_type": "code",
150 | "execution_count": null,
151 | "metadata": {},
152 | "outputs": [],
153 | "source": [
154 | "import plotly.graph_objects as go\n",
155 | "animals=['giraffes', 'orangutans', 'monkeys']\n",
156 | "\n",
157 | "fig = go.Figure([go.Bar(x=animals, y=[20, 14, 23])])\n",
158 | "fig.show()"
159 | ]
160 | },
161 | {
162 | "cell_type": "markdown",
163 | "metadata": {},
164 | "source": [
165 | "## Grouped Bar Chart\n",
166 | "Notice how we can customize the figure using *fig.update*."
167 | ]
168 | },
169 | {
170 | "cell_type": "code",
171 | "execution_count": null,
172 | "metadata": {},
173 | "outputs": [],
174 | "source": [
175 | "import plotly.graph_objects as go\n",
176 | "animals=['giraffes', 'orangutans', 'monkeys']\n",
177 | "\n",
178 | "fig = go.Figure(data=[\n",
179 | " go.Bar(name='SF Zoo', x=animals, y=[20, 14, 23]),\n",
180 | " go.Bar(name='LA Zoo', x=animals, y=[12, 18, 29])\n",
181 | "])\n",
182 | "# Change the bar mode .. default mode is group\n",
183 | "fig.update_layout(barmode='group')\n",
184 | "fig.show()"
185 | ]
186 | },
187 | {
188 | "cell_type": "markdown",
189 | "metadata": {},
190 | "source": [
191 | "## Stacked Bar Chart"
192 | ]
193 | },
194 | {
195 | "cell_type": "code",
196 | "execution_count": null,
197 | "metadata": {},
198 | "outputs": [],
199 | "source": [
200 | "import plotly.graph_objects as go\n",
201 | "animals=['giraffes', 'orangutans', 'monkeys']\n",
202 | "\n",
203 | "fig = go.Figure(data=[\n",
204 | " go.Bar(name='SF Zoo', x=animals, y=[20, 14, 23]),\n",
205 | " go.Bar(name='LA Zoo', x=animals, y=[12, 18, 29])\n",
206 | "])\n",
207 | "# Change the bar mode to 'stack'\n",
208 | "fig.update_layout(barmode='stack')\n",
209 | "fig.show()"
210 | ]
211 | },
212 | {
213 | "cell_type": "markdown",
214 | "metadata": {},
215 | "source": [
216 | "## Bar Chart with Hover Text"
217 | ]
218 | },
219 | {
220 | "cell_type": "code",
221 | "execution_count": null,
222 | "metadata": {},
223 | "outputs": [],
224 | "source": [
225 | "import plotly.graph_objects as go\n",
226 | "\n",
227 | "x = ['Product A', 'Product B', 'Product C']\n",
228 | "y = [20, 14, 23]\n",
229 | "\n",
230 | "# Use the hovertext kw argument for hover text\n",
231 | "fig = go.Figure(data=[go.Bar(x=x, y=y,\n",
232 | " hovertext=['27% market share', '24% market share', '19% market share'])])\n",
233 | "# Customize aspect\n",
234 | "fig.update_traces(marker_color='rgb(158,202,225)', marker_line_color='rgb(8,48,107)',\n",
235 | " marker_line_width=1.5, opacity=0.6)\n",
236 | "fig.update_layout(title_text='January 2013 Sales Report')\n",
237 | "fig.show()"
238 | ]
239 | },
240 | {
241 | "cell_type": "markdown",
242 | "metadata": {},
243 | "source": [
244 | "## Bar Chart with Direct Labels\n",
245 | "It is possible to change text fontsize and position"
246 | ]
247 | },
248 | {
249 | "cell_type": "code",
250 | "execution_count": null,
251 | "metadata": {},
252 | "outputs": [],
253 | "source": [
254 | "import plotly.graph_objects as go\n",
255 | "\n",
256 | "x = ['Product A', 'Product B', 'Product C']\n",
257 | "y = [20, 14, 23]\n",
258 | "\n",
259 | "# Use textposition='auto' for direct text\n",
260 | "fig = go.Figure(data=[go.Bar(\n",
261 | " x=x, y=y,\n",
262 | " text=y,\n",
263 | " textposition='auto',\n",
264 | " )])\n",
265 | "\n",
266 | "fig.show()"
267 | ]
268 | },
269 | {
270 | "cell_type": "markdown",
271 | "metadata": {},
272 | "source": [
273 | "## Rotated Bar Chart Labels\n",
274 | "This is useful when we do not have much display space"
275 | ]
276 | },
277 | {
278 | "cell_type": "code",
279 | "execution_count": null,
280 | "metadata": {},
281 | "outputs": [],
282 | "source": [
283 | "import plotly.graph_objects as go\n",
284 | "\n",
285 | "months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',\n",
286 | " 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']\n",
287 | "\n",
288 | "fig = go.Figure()\n",
289 | "fig.add_trace(go.Bar(\n",
290 | " x=months,\n",
291 | " y=[20, 14, 25, 16, 18, 22, 19, 15, 12, 16, 14, 17],\n",
292 | " name='Primary Product',\n",
293 | " marker_color='indianred'\n",
294 | "))\n",
295 | "fig.add_trace(go.Bar(\n",
296 | " x=months,\n",
297 | " y=[19, 14, 22, 14, 16, 19, 15, 14, 10, 12, 12, 16],\n",
298 | " name='Secondary Product',\n",
299 | " marker_color='lightsalmon'\n",
300 | "))\n",
301 | "\n",
302 | "# Here we modify the tickangle of the xaxis, resulting in rotated labels.\n",
303 | "fig.update_layout(barmode='group', xaxis_tickangle=-45)\n",
304 | "fig.show()"
305 | ]
306 | },
307 | {
308 | "cell_type": "markdown",
309 | "metadata": {},
310 | "source": [
311 | "## Customizing Individual Bar Colors and Widths\n",
312 | "In case we want to highligh a certain bar"
313 | ]
314 | },
315 | {
316 | "cell_type": "code",
317 | "execution_count": null,
318 | "metadata": {},
319 | "outputs": [],
320 | "source": [
321 | "import plotly.graph_objects as go\n",
322 | "\n",
323 | "colors = ['lightslategray',] * 5\n",
324 | "colors[1] = 'crimson'\n",
325 | "\n",
326 | "fig = go.Figure(data=[go.Bar(\n",
327 | " x=['Feature A', 'Feature B', 'Feature C',\n",
328 | " 'Feature D', 'Feature E'],\n",
329 | " y=[20, 14, 23, 25, 22],\n",
330 | " marker_color=colors, # marker color can be a single color value or an iterable\n",
331 | " width=[0.5, 0.5, 0.5, 1, 0.5] # customize width here\n",
332 | ")])\n",
333 | "fig.update_layout(title_text='Least Used Feature')\n",
334 | "fig.show()"
335 | ]
336 | },
337 | {
338 | "cell_type": "code",
339 | "execution_count": null,
340 | "metadata": {},
341 | "outputs": [],
342 | "source": []
343 | },
344 | {
345 | "cell_type": "markdown",
346 | "metadata": {},
347 | "source": [
348 | "## Horizontal Bar Chart with Plotly Express\n",
349 | "## https://plot.ly/python/horizontal-bar-charts/"
350 | ]
351 | },
352 | {
353 | "cell_type": "code",
354 | "execution_count": null,
355 | "metadata": {},
356 | "outputs": [],
357 | "source": [
358 | "import plotly.express as px\n",
359 | "df = px.data.tips()\n",
360 | "fig = px.bar(df, x=\"total_bill\", y=\"day\", orientation='h')\n",
361 | "fig.show()"
362 | ]
363 | },
364 | {
365 | "cell_type": "markdown",
366 | "metadata": {},
367 | "source": [
368 | "## Configure horizontal bar chart"
369 | ]
370 | },
371 | {
372 | "cell_type": "code",
373 | "execution_count": null,
374 | "metadata": {},
375 | "outputs": [],
376 | "source": [
377 | "import plotly.express as px\n",
378 | "df = px.data.tips()\n",
379 | "fig = px.bar(df, x=\"total_bill\", y=\"sex\", color='day', orientation='h',\n",
380 | " hover_data=['total_bill'], \n",
381 | " height=400,\n",
382 | " title='Restaurant bills')\n",
383 | "fig.show()"
384 | ]
385 | },
386 | {
387 | "cell_type": "code",
388 | "execution_count": null,
389 | "metadata": {},
390 | "outputs": [],
391 | "source": [
392 | "df.head()"
393 | ]
394 | },
395 | {
396 | "cell_type": "markdown",
397 | "metadata": {},
398 | "source": [
399 | "## Horizontal Bar Chart with go.Bar (Graphic Object rather then plotly express)\n",
400 | "Notice the axes are swapped and the orientation is 'h'"
401 | ]
402 | },
403 | {
404 | "cell_type": "code",
405 | "execution_count": null,
406 | "metadata": {},
407 | "outputs": [],
408 | "source": [
409 | "import plotly.graph_objects as go\n",
410 | "\n",
411 | "fig = go.Figure(go.Bar(\n",
412 | " x=[20, 14, 23],\n",
413 | " y=['giraffes', 'orangutans', 'monkeys'],\n",
414 | " orientation='h'))\n",
415 | "\n",
416 | "fig.show()"
417 | ]
418 | },
419 | {
420 | "cell_type": "markdown",
421 | "metadata": {},
422 | "source": [
423 | "## Colored Horizontal Bar Chart\n",
424 | "Coloring is easy peasy!"
425 | ]
426 | },
427 | {
428 | "cell_type": "code",
429 | "execution_count": null,
430 | "metadata": {},
431 | "outputs": [],
432 | "source": [
433 | "import plotly.graph_objects as go\n",
434 | "\n",
435 | "fig = go.Figure()\n",
436 | "fig.add_trace(go.Bar(\n",
437 | " y=['giraffes', 'orangutans', 'monkeys'],\n",
438 | " x=[20, 14, 23],\n",
439 | " name='SF Zoo',\n",
440 | " orientation='h',\n",
441 | " marker=dict(\n",
442 | " color='rgba(246, 78, 139, 0.6)',\n",
443 | " line=dict(color='rgba(246, 78, 139, 1.0)', width=3)\n",
444 | " )\n",
445 | "))\n",
446 | "fig.add_trace(go.Bar(\n",
447 | " y=['giraffes', 'orangutans', 'monkeys'],\n",
448 | " x=[12, 18, 29],\n",
449 | " name='LA Zoo',\n",
450 | " orientation='h',\n",
451 | " marker=dict(\n",
452 | " color='rgba(58, 71, 80, 0.6)',\n",
453 | " line=dict(color='rgba(58, 71, 80, 1.0)', width=3)\n",
454 | " )\n",
455 | "))\n",
456 | "\n",
457 | "fig.update_layout(barmode='stack')\n",
458 | "fig.show()"
459 | ]
460 | },
461 | {
462 | "cell_type": "markdown",
463 | "metadata": {},
464 | "source": [
465 | "## Bar Chart with Sorted or Ordered Categories\n",
466 | "Set *categoryorder* to \"*category ascending\" or \"category descending\" for the alphanumerical order of the category names or \"total ascending\" or \"total descending\" for numerical order of values. categoryorder for more information. Note that sorting the bars by a particular trace isn't possible right now - it's only possible to sort by the total values. Of course, you can always sort your data before plotting it if you need more customization.\n",
467 | "\n",
468 | "This example orders the bar chart alphabetically with categoryorder: 'category ascending'"
469 | ]
470 | },
471 | {
472 | "cell_type": "code",
473 | "execution_count": null,
474 | "metadata": {},
475 | "outputs": [],
476 | "source": [
477 | "import plotly.graph_objects as go\n",
478 | "\n",
479 | "x=['b', 'a', 'c', 'd']\n",
480 | "fig = go.Figure(go.Bar(x=x, y=[2,5,1,9], name='Montreal'))\n",
481 | "fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16], name='Ottawa'))\n",
482 | "fig.add_trace(go.Bar(x=x, y=[6, 8, 4.5, 8], name='Toronto'))\n",
483 | "\n",
484 | "fig.update_layout(barmode='stack', xaxis={'categoryorder':'category ascending'})\n",
485 | "fig.show()"
486 | ]
487 | },
488 | {
489 | "cell_type": "code",
490 | "execution_count": null,
491 | "metadata": {},
492 | "outputs": [],
493 | "source": [
494 | "import plotly.graph_objects as go\n",
495 | "\n",
496 | "y=['b', 'a', 'c', 'd']\n",
497 | "fig = go.Figure(go.Bar(y=y, x=[2,5,1,9], name='Montreal', orientation='h',))\n",
498 | "fig.add_trace(go.Bar(y=y, x=[1, 4, 9, 16], name='Ottawa', orientation='h',))\n",
499 | "fig.add_trace(go.Bar(y=y, x=[6, 8, 4.5, 8], name='Toronto', orientation='h',))\n",
500 | "\n",
501 | "fig.update_layout(barmode='stack')\n",
502 | "fig.show()"
503 | ]
504 | },
505 | {
506 | "cell_type": "code",
507 | "execution_count": null,
508 | "metadata": {},
509 | "outputs": [],
510 | "source": []
511 | }
512 | ],
513 | "metadata": {
514 | "kernelspec": {
515 | "display_name": "Python 3",
516 | "language": "python",
517 | "name": "python3"
518 | },
519 | "language_info": {
520 | "codemirror_mode": {
521 | "name": "ipython",
522 | "version": 3
523 | },
524 | "file_extension": ".py",
525 | "mimetype": "text/x-python",
526 | "name": "python",
527 | "nbconvert_exporter": "python",
528 | "pygments_lexer": "ipython3",
529 | "version": "3.7.6"
530 | }
531 | },
532 | "nbformat": 4,
533 | "nbformat_minor": 2
534 | }
535 |
--------------------------------------------------------------------------------
/notebooks/04- Pie Charts.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Pie Chart:\n",
8 | "A pie chart (or a circle chart) is a circular statistical graphic, which is divided into slices to illustrate numerical proportion. In a pie chart, the arc length of each slice (and consequently its central angle and area), is proportional to the quantity it represents. While it is named for its resemblance to a pie which has been sliced, there are variations on the way it can be presented. [Pie Chart on Wikipedia](https://en.wikipedia.org/wiki/Pie_chart)\n",
9 | "\n",
10 | "Pie charts can be used to show percentages of a whole, and represents percentages at a set point in time. Unlike bar graphs and line graphs, pie charts do not show changes over time. \n",
11 | "\n",
12 | "*The Title*: The title offers a short explanation of what is in your graph. This helps the reader identify what they are about to look at. It can be creative or simple as long as it tells what is in the chart. \n",
13 | "\n",
14 | "*The Legend*: The legend tells what each slice represents. Just like on a map, the legend helps the reader understand what they are looking at.\n",
15 | "\n",
16 | "*The Source*: The source explains where you found the information that is in your graph. It is important to give credit to those who collected your data! \n",
17 | "\n",
18 | "*The Data*: The most important part of your chart is the information, or data, it contains. Pie charts represent data as part of 100 (a percentage). Each slice represents a different piece of data. [Source](https://nces.ed.gov/nceskids/help/user_guide/graph/pie.asp)\n",
19 | "\n",
20 | "\n",
21 | "* Pie charts are a visual way of displaying how the total data are distributed between different categories. \n",
22 | "* Pie charts should only be used for displaying nominal data (i.e. data that are classed into different categories). \n",
23 | "* They are generally best for showing information grouped into a small number of categories and are a graphical way of displaying data that might otherwise be presented as a simple table. [Source](https://www.le.ac.uk/oerresources/ssds/numeracyskills/page_33.htm)"
24 | ]
25 | },
26 | {
27 | "cell_type": "markdown",
28 | "metadata": {},
29 | "source": [
30 | "## Pie chart with plotly express"
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "execution_count": null,
36 | "metadata": {},
37 | "outputs": [],
38 | "source": [
39 | "import plotly.express as px\n",
40 | "df = px.data.gapminder().query(\"year == 2007\").query(\"continent == 'Europe'\")\n",
41 | "df.loc[df['pop'] < 2.e6, 'country'] = 'Other countries' # Represent only large countries\n",
42 | "fig = px.pie(df, values='pop', names='country', title='Population of European continent')\n",
43 | "fig.show()"
44 | ]
45 | },
46 | {
47 | "cell_type": "markdown",
48 | "metadata": {},
49 | "source": [
50 | "## Pie chart with repeated labels"
51 | ]
52 | },
53 | {
54 | "cell_type": "code",
55 | "execution_count": null,
56 | "metadata": {},
57 | "outputs": [],
58 | "source": [
59 | "# Lines of the dataframe with the same value for names are grouped together in the same sector.\n",
60 | "import plotly.express as px\n",
61 | "# This dataframe has 244 lines, but 4 distinct values for `day`\n",
62 | "df = px.data.tips()\n",
63 | "fig = px.pie(df, values='tip', names='day')\n",
64 | "fig.show()"
65 | ]
66 | },
67 | {
68 | "cell_type": "code",
69 | "execution_count": null,
70 | "metadata": {},
71 | "outputs": [],
72 | "source": [
73 | "df"
74 | ]
75 | },
76 | {
77 | "cell_type": "markdown",
78 | "metadata": {},
79 | "source": [
80 | "## Setting the color of pie sectors with px.pie"
81 | ]
82 | },
83 | {
84 | "cell_type": "code",
85 | "execution_count": null,
86 | "metadata": {},
87 | "outputs": [],
88 | "source": [
89 | "import plotly.express as px\n",
90 | "df = px.data.tips()\n",
91 | "fig = px.pie(df, values='tip', names='day', color_discrete_sequence=px.colors.sequential.RdBu)\n",
92 | "fig.show()"
93 | ]
94 | },
95 | {
96 | "cell_type": "markdown",
97 | "metadata": {},
98 | "source": [
99 | "## Pie Charts with Graphic Object\n",
100 | "\n",
101 | "* In *go.Pie*, data visualized by the sectors of the pie is set in *values*. \n",
102 | "* The sector labels are set in *labels*. \n",
103 | "* The sector colors are set in *marker.colors*."
104 | ]
105 | },
106 | {
107 | "cell_type": "code",
108 | "execution_count": null,
109 | "metadata": {},
110 | "outputs": [],
111 | "source": [
112 | "import plotly.graph_objects as go\n",
113 | "\n",
114 | "labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']\n",
115 | "values = [4500, 2500, 1053, 500]\n",
116 | "\n",
117 | "fig = go.Figure(data=[go.Pie(labels=labels, values=values)])\n",
118 | "fig.show()"
119 | ]
120 | },
121 | {
122 | "cell_type": "markdown",
123 | "metadata": {},
124 | "source": [
125 | "## Styled Pie Chart"
126 | ]
127 | },
128 | {
129 | "cell_type": "code",
130 | "execution_count": null,
131 | "metadata": {},
132 | "outputs": [],
133 | "source": [
134 | "#Colors can be given as RGB triplets or hexadecimal strings, or with CSS color names as below.\n",
135 | "import plotly.graph_objects as go\n",
136 | "colors = ['gold', 'mediumturquoise', 'darkorange', 'lightgreen']\n",
137 | "\n",
138 | "fig = go.Figure(data=[go.Pie(labels=['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen'],\n",
139 | " values=[4500,2500,1053,500])])\n",
140 | "fig.update_traces(hoverinfo='label+percent', textinfo='value', textfont_size=20,\n",
141 | " marker=dict(colors=colors, line=dict(color='#000000', width=2)))\n",
142 | "fig.show()"
143 | ]
144 | },
145 | {
146 | "cell_type": "markdown",
147 | "metadata": {},
148 | "source": [
149 | "## Controlling text fontsize and orientation inside pie sectors\n",
150 | "\n",
151 | "If you want all the text labels to have the same size, you can use the *uniformtext* layout parameter\n",
152 | "\n",
153 | "The *insidetextorientation* attribute controls the orientation of text inside sectors. With \"auto\" the texts may automatically be rotated to fit with the maximum size inside the slice. Using \"horizontal\" (resp. \"radial\", \"tangential\") forces text to be horizontal (resp. radial or tangential)\n",
154 | "\n",
155 | "For a figure *fig* created with plotly express, use *fig.update_traces(insidetextorientation='...')* to change the text orientation."
156 | ]
157 | },
158 | {
159 | "cell_type": "code",
160 | "execution_count": null,
161 | "metadata": {},
162 | "outputs": [],
163 | "source": [
164 | "import plotly.graph_objects as go\n",
165 | "\n",
166 | "labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']\n",
167 | "values = [4500, 2500, 1053, 500]\n",
168 | "\n",
169 | "fig = go.Figure(data=[go.Pie(labels=labels, values=values, textinfo='label+percent',\n",
170 | " insidetextorientation='radial'\n",
171 | " )])\n",
172 | "fig.update_traces(textposition='inside')\n",
173 | "fig.update_layout(uniformtext_minsize=12, uniformtext_mode='hide')\n",
174 | "fig.show()"
175 | ]
176 | },
177 | {
178 | "cell_type": "markdown",
179 | "metadata": {},
180 | "source": [
181 | "## Donut Chart"
182 | ]
183 | },
184 | {
185 | "cell_type": "code",
186 | "execution_count": null,
187 | "metadata": {},
188 | "outputs": [],
189 | "source": [
190 | "import plotly.graph_objects as go\n",
191 | "\n",
192 | "labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']\n",
193 | "values = [4500, 2500, 1053, 500]\n",
194 | "\n",
195 | "# Use `hole` to create a donut-like pie chart\n",
196 | "fig = go.Figure(data=[go.Pie(labels=labels, values=values, hole=.3)])\n",
197 | "fig.show()"
198 | ]
199 | },
200 | {
201 | "cell_type": "markdown",
202 | "metadata": {},
203 | "source": [
204 | "## Pulling sectors out from the center\n",
205 | "For a \"pulled-out\" or \"exploded\" layout of the pie chart, use the *pull argument*. It can be a scalar for pulling all sectors or an array to pull only some of the sectors."
206 | ]
207 | },
208 | {
209 | "cell_type": "code",
210 | "execution_count": null,
211 | "metadata": {},
212 | "outputs": [],
213 | "source": [
214 | "import plotly.graph_objects as go\n",
215 | "\n",
216 | "labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']\n",
217 | "values = [4500, 2500, 1053, 500]\n",
218 | "\n",
219 | "# pull is given as a fraction of the pie radius\n",
220 | "fig = go.Figure(data=[go.Pie(labels=labels, values=values, pull=[0, 0, 0.2, 0], hole=0.25)])\n",
221 | "fig.show()"
222 | ]
223 | },
224 | {
225 | "cell_type": "markdown",
226 | "metadata": {},
227 | "source": [
228 | "## Sub Plots + Plot chart with area proportional to total count\n",
229 | "\n",
230 | "Plots in the same *scalegroup* are represented with an area proportional to their total size."
231 | ]
232 | },
233 | {
234 | "cell_type": "code",
235 | "execution_count": null,
236 | "metadata": {},
237 | "outputs": [],
238 | "source": [
239 | "# https://en.wikipedia.org/wiki/World_population\n",
240 | "import pandas as pd\n",
241 | "raw_data = {\n",
242 | " \"region\": [\"Africa\", \"Asia\", \"Europe\", \"Latin America\",\"North America\",\"Oceania\"], \n",
243 | " \"1900\": [133, 947, 408, 74, 82, 6],\n",
244 | " \"2010\": [1022, 4164, 738, 590, 345, 37]\n",
245 | " }\n",
246 | " \n",
247 | "df = pd.DataFrame(raw_data, columns = ['region', '1900','2010'])\n",
248 | "df"
249 | ]
250 | },
251 | {
252 | "cell_type": "code",
253 | "execution_count": null,
254 | "metadata": {},
255 | "outputs": [],
256 | "source": [
257 | "import plotly.graph_objects as go\n",
258 | "from plotly.subplots import make_subplots\n",
259 | "\n",
260 | "fig = make_subplots(1, 2, specs=[[{'type':'domain'}, {'type':'domain'}]],\n",
261 | " subplot_titles=['1900', '2010'])\n",
262 | "fig.add_trace(go.Pie(labels=df['region'], values=df['1900'], scalegroup='one',\n",
263 | " name=\"World Pop 1900\"), 1, 1)\n",
264 | "fig.add_trace(go.Pie(labels=df['region'], values=df['2010'], scalegroup='one',\n",
265 | " name=\"World Pop 2010\"), 1, 2)\n",
266 | "\n",
267 | "fig.update_layout(title_text='World GDP')\n",
268 | "fig.show()"
269 | ]
270 | },
271 | {
272 | "cell_type": "code",
273 | "execution_count": null,
274 | "metadata": {},
275 | "outputs": [],
276 | "source": []
277 | }
278 | ],
279 | "metadata": {
280 | "kernelspec": {
281 | "display_name": "Python 3",
282 | "language": "python",
283 | "name": "python3"
284 | },
285 | "language_info": {
286 | "codemirror_mode": {
287 | "name": "ipython",
288 | "version": 3
289 | },
290 | "file_extension": ".py",
291 | "mimetype": "text/x-python",
292 | "name": "python",
293 | "nbconvert_exporter": "python",
294 | "pygments_lexer": "ipython3",
295 | "version": "3.7.6"
296 | }
297 | },
298 | "nbformat": 4,
299 | "nbformat_minor": 2
300 | }
301 |
--------------------------------------------------------------------------------
/notebooks/05- Sunburst Charts.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Sunburst Chart:\n",
8 | "A ring chart, also known as a **sunburst** chart or a multilevel pie chart, is used to visualize hierarchical data, depicted by concentric circles. The circle in the center represents the root node, with the hierarchy moving outward from the center. A segment of the inner circle bears a hierarchical relationship to those segments of the outer circle which lie within the angular sweep of the parent segment.[Sunburst Char on Wikipedia](https://en.wikipedia.org/wiki/Pie_chart#Ring)\n",
9 | "\n",
10 | "\n",
11 | "Sunburst plots visualize hierarchical data spanning outwards radially from root to leaves. The sunburst sector hierarchy is determined by the entries in labels (names in px.sunburst) and in parents. The root starts from the center and children are added to the outer rings.\n",
12 | "\n",
13 | "Main arguments:\n",
14 | "\n",
15 | "* *labels* (*names* in *px.sunburst* since *labels* is reserved for overriding columns names): sets the labels of sunburst sectors.\n",
16 | "* *parents*: sets the parent sectors of sunburst sectors. An empty string *''* is used for the root node in the hierarchy. In this example, the root is \"Eve\".\n",
17 | "* *values*: sets the values associated with sunburst sectors, determining their width (See the *branchvalues* section below for different modes for setting the width).\n",
18 | "[Source](https://plot.ly/python/sunburst-charts/)"
19 | ]
20 | },
21 | {
22 | "cell_type": "markdown",
23 | "metadata": {},
24 | "source": [
25 | "## Basic Sunburst Plot with plotly.express\n",
26 | "With *px.sunburst*, each row of the DataFrame is represented as a sector of the sunburst."
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": null,
32 | "metadata": {},
33 | "outputs": [],
34 | "source": [
35 | "import plotly.express as px\n",
36 | "import pandas as pd\n",
37 | "data = dict(\n",
38 | " character=[\"Eve\", \"Cain\", \"Seth\", \"Enos\", \"Noam\", \"Abel\", \"Awan\", \"Enoch\", \"Azura\"],\n",
39 | " parent=[\"\", \"Eve\", \"Eve\", \"Seth\", \"Seth\", \"Eve\", \"Eve\", \"Awan\", \"Eve\" ],\n",
40 | " value=[10, 14, 12, 10, 2, 6, 6, 4, 4])\n",
41 | "\n",
42 | "\n",
43 | "fig = px.sunburst(\n",
44 | " data,\n",
45 | " names='character',\n",
46 | " parents='parent',\n",
47 | " values='value',\n",
48 | ")\n",
49 | "fig.show()\n",
50 | "\n"
51 | ]
52 | },
53 | {
54 | "cell_type": "code",
55 | "execution_count": null,
56 | "metadata": {},
57 | "outputs": [],
58 | "source": [
59 | "import plotly.io as pio\n",
60 | "pio.write_html(fig, 'sunburst.html')"
61 | ]
62 | },
63 | {
64 | "cell_type": "markdown",
65 | "metadata": {},
66 | "source": [
67 | "## Sunburst with discrete color argument\n",
68 | "Hierarchical data are often stored as a rectangular dataframe, with different columns corresponding to different levels of the hierarchy. *px.sunburst* can take a path parameter corresponding to a list of columns. Note that id and parent should not be provided if path is given."
69 | ]
70 | },
71 | {
72 | "cell_type": "code",
73 | "execution_count": null,
74 | "metadata": {},
75 | "outputs": [],
76 | "source": [
77 | "import plotly.express as px\n",
78 | "df = px.data.tips()\n",
79 | "#fig = px.sunburst(df, path=['day', 'sex'], values='total_bill')\n",
80 | "fig = px.sunburst(df, path=['sex', 'day', 'time'], values='total_bill', color='time')\n",
81 | "fig.show()"
82 | ]
83 | },
84 | {
85 | "cell_type": "markdown",
86 | "metadata": {},
87 | "source": [
88 | "## Basic Sunburst with Graphic Object"
89 | ]
90 | },
91 | {
92 | "cell_type": "code",
93 | "execution_count": null,
94 | "metadata": {},
95 | "outputs": [],
96 | "source": [
97 | "import plotly.graph_objects as go\n",
98 | "\n",
99 | "fig =go.Figure(go.Sunburst(\n",
100 | " labels=[\"Eve\", \"Cain\", \"Seth\", \"Enos\", \"Noam\", \"Abel\", \"Awan\", \"Enoch\", \"Azura\"],\n",
101 | " parents=[\"\", \"Eve\", \"Eve\", \"Seth\", \"Seth\", \"Eve\", \"Eve\", \"Awan\", \"Eve\" ],\n",
102 | " values=[10, 14, 12, 10, 2, 6, 6, 4, 4],\n",
103 | "))\n",
104 | "# Update layout for tight margin\n",
105 | "# See https://plot.ly/python/creating-and-updating-figures/\n",
106 | "fig.update_layout(margin = dict(t=0, l=0, r=0, b=0))\n",
107 | "\n",
108 | "fig.show()"
109 | ]
110 | },
111 | {
112 | "cell_type": "code",
113 | "execution_count": null,
114 | "metadata": {},
115 | "outputs": [],
116 | "source": []
117 | }
118 | ],
119 | "metadata": {
120 | "kernelspec": {
121 | "display_name": "Python 3",
122 | "language": "python",
123 | "name": "python3"
124 | },
125 | "language_info": {
126 | "codemirror_mode": {
127 | "name": "ipython",
128 | "version": 3
129 | },
130 | "file_extension": ".py",
131 | "mimetype": "text/x-python",
132 | "name": "python",
133 | "nbconvert_exporter": "python",
134 | "pygments_lexer": "ipython3",
135 | "version": "3.7.6"
136 | }
137 | },
138 | "nbformat": 4,
139 | "nbformat_minor": 2
140 | }
141 |
--------------------------------------------------------------------------------
/notebooks/06- Bubble Charts.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Bubble Chart:\n",
8 | "A bubble chart is a type of chart that displays three dimensions of data. Each entity with its triplet *(v1, v2, v3)* of associated data is plotted as a disk that expresses two of the *vi* values through the disk's *xy* location and the third through its size. Bubble charts can facilitate the understanding of social, economical, medical, and other scientific relationships. [Bubble Chart on Wikipedia](https://en.wikipedia.org/wiki/Bubble_chart)\n",
9 | "\n",
10 | "### A bubble chart is essentially a scatter plot with the size of markers customized"
11 | ]
12 | },
13 | {
14 | "cell_type": "markdown",
15 | "metadata": {},
16 | "source": [
17 | "## Bubble chart with plotly.express"
18 | ]
19 | },
20 | {
21 | "cell_type": "code",
22 | "execution_count": null,
23 | "metadata": {},
24 | "outputs": [],
25 | "source": [
26 | "import plotly.express as px\n",
27 | "df = px.data.gapminder()\n",
28 | "\n",
29 | "fig = px.scatter(df.query(\"year==2007\"), x=\"gdpPercap\", y=\"lifeExp\",\n",
30 | " size=\"pop\", color=\"continent\",\n",
31 | " hover_name=\"country\", log_x=True, size_max=60)\n",
32 | "fig.show()\n"
33 | ]
34 | },
35 | {
36 | "cell_type": "markdown",
37 | "metadata": {},
38 | "source": [
39 | "## Bubble Chart with Graph Object\n",
40 | "\n",
41 | "Take a look at various parameters in *go.Scatter*"
42 | ]
43 | },
44 | {
45 | "cell_type": "code",
46 | "execution_count": null,
47 | "metadata": {},
48 | "outputs": [],
49 | "source": [
50 | "import plotly.graph_objects as go\n",
51 | "\n",
52 | "fig = go.Figure(data=[go.Scatter(\n",
53 | " x=[1, 2, 3, 4], y=[10, 11, 12, 13],\n",
54 | " mode='markers',\n",
55 | " marker_size=[40, 60, 80, 100])\n",
56 | "])\n",
57 | "\n",
58 | "fig.show()"
59 | ]
60 | },
61 | {
62 | "cell_type": "markdown",
63 | "metadata": {},
64 | "source": [
65 | "## Setting Marker Size and Color .. and Hover Text"
66 | ]
67 | },
68 | {
69 | "cell_type": "code",
70 | "execution_count": null,
71 | "metadata": {},
72 | "outputs": [],
73 | "source": [
74 | "import plotly.graph_objects as go\n",
75 | "\n",
76 | "fig = go.Figure(data=[go.Scatter(\n",
77 | " x=[1, 2, 3, 4], y=[10, 11, 12, 13],\n",
78 | " #text=['A
size: 40', 'B
size: 60', 'C
size: 80', 'D
size: 100'],\n",
79 | " mode='markers',\n",
80 | " marker=dict(\n",
81 | " color=['rgb(93, 164, 214)', 'rgb(255, 144, 14)',\n",
82 | " 'rgb(44, 160, 101)', 'rgb(255, 65, 54)'],\n",
83 | " opacity=[1, 0.8, 0.6, 0.4],\n",
84 | " size=[40, 60, 80, 100],\n",
85 | " )\n",
86 | ")])\n",
87 | "\n",
88 | "fig.show()"
89 | ]
90 | },
91 | {
92 | "cell_type": "markdown",
93 | "metadata": {},
94 | "source": [
95 | "## Bubble Charts with Colorscale"
96 | ]
97 | },
98 | {
99 | "cell_type": "code",
100 | "execution_count": null,
101 | "metadata": {},
102 | "outputs": [],
103 | "source": [
104 | "import plotly.graph_objects as go\n",
105 | "\n",
106 | "fig = go.Figure(data=[go.Scatter(\n",
107 | " x=[1, 3.2, 5.4, 7.6, 9.8, 12.5],\n",
108 | " y=[1, 3.2, 5.4, 7.6, 9.8, 12.5],\n",
109 | " mode='markers',\n",
110 | " marker=dict(\n",
111 | " color=[120, 125, 130, 135, 140, 145],\n",
112 | " size=[15, 30, 55, 70, 90, 110],\n",
113 | " showscale=True\n",
114 | " )\n",
115 | ")])\n",
116 | "\n",
117 | "fig.show()"
118 | ]
119 | },
120 | {
121 | "cell_type": "code",
122 | "execution_count": null,
123 | "metadata": {},
124 | "outputs": [],
125 | "source": []
126 | }
127 | ],
128 | "metadata": {
129 | "kernelspec": {
130 | "display_name": "Python 3",
131 | "language": "python",
132 | "name": "python3"
133 | },
134 | "language_info": {
135 | "codemirror_mode": {
136 | "name": "ipython",
137 | "version": 3
138 | },
139 | "file_extension": ".py",
140 | "mimetype": "text/x-python",
141 | "name": "python",
142 | "nbconvert_exporter": "python",
143 | "pygments_lexer": "ipython3",
144 | "version": "3.7.6"
145 | }
146 | },
147 | "nbformat": 4,
148 | "nbformat_minor": 2
149 | }
150 |
--------------------------------------------------------------------------------
/notebooks/07- Gantt Chart.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Gantt Charts:\n",
8 | "A Gantt chart is a type of bar chart that illustrates a project schedule, named after its inventor, Henry Gantt (1861–1919), who designed such a chart around the years 1910–1915. Modern Gantt charts also show the dependency relationships between activities and current schedule status. [Gantt Chart on Wikipedia](https://en.wikipedia.org/wiki/Gantt_chart)\n",
9 | "\n",
10 | "* A Gantt chart, or harmonogram, is a type of bar chart that illustrates a project schedule.\n",
11 | "* This chart lists the tasks to be performed on the vertical axis, and time intervals on the horizontal axis.\n",
12 | "* The width of the horizontal bars in the graph shows the duration of each activity.\n",
13 | "* Gantt charts illustrate the start and finish dates of the terminal elements and summary elements of a project (Terminal elements and summary elements constitute the work breakdown structure of the project).\n",
14 | "\n",
15 | "\n"
16 | ]
17 | },
18 | {
19 | "cell_type": "markdown",
20 | "metadata": {},
21 | "source": [
22 | "## Simple Gantt Chart"
23 | ]
24 | },
25 | {
26 | "cell_type": "code",
27 | "execution_count": null,
28 | "metadata": {},
29 | "outputs": [],
30 | "source": [
31 | "import plotly.figure_factory as ff\n",
32 | "\n",
33 | "data = [dict(Task=\"Job A\", Start='2009-01-01', Finish='2009-02-28'),\n",
34 | " dict(Task=\"Job B\", Start='2009-03-05', Finish='2009-04-15'),\n",
35 | " dict(Task=\"Job C\", Start='2009-02-20', Finish='2009-05-30')]\n",
36 | "\n",
37 | "fig = ff.create_gantt(data)\n",
38 | "fig.show()"
39 | ]
40 | },
41 | {
42 | "cell_type": "markdown",
43 | "metadata": {},
44 | "source": [
45 | "## Index by Numeric Variable\n",
46 | "\n",
47 | "Use colors to show task completion percentage"
48 | ]
49 | },
50 | {
51 | "cell_type": "code",
52 | "execution_count": null,
53 | "metadata": {},
54 | "outputs": [],
55 | "source": [
56 | "import plotly.figure_factory as ff\n",
57 | "\n",
58 | "df = [dict(Task=\"Job A\", Start='2009-01-01', Finish='2009-02-28', Complete=10),\n",
59 | " dict(Task=\"Job B\", Start='2008-12-05', Finish='2009-04-15', Complete=60),\n",
60 | " dict(Task=\"Job C\", Start='2009-02-20', Finish='2009-05-30', Complete=95)]\n",
61 | "\n",
62 | "fig = ff.create_gantt(df, index_col='Complete', show_colorbar=True)\n",
63 | "fig.show()"
64 | ]
65 | },
66 | {
67 | "cell_type": "markdown",
68 | "metadata": {},
69 | "source": [
70 | "## Index by String Variable\n",
71 | "* Give each task a label\n",
72 | "* Specify specific colors\n",
73 | "\n",
74 | "*Resource* parameter is like a tag"
75 | ]
76 | },
77 | {
78 | "cell_type": "code",
79 | "execution_count": null,
80 | "metadata": {},
81 | "outputs": [],
82 | "source": [
83 | "import plotly.figure_factory as ff\n",
84 | "\n",
85 | "df = [dict(Task=\"Job A\", Start='2009-01-01', Finish='2009-02-01', Resource='Apple'),\n",
86 | " dict(Task=\"Job B\", Start='2009-03-05', Finish='2009-04-15', Resource='Grape'),\n",
87 | " dict(Task=\"Job C\", Start='2009-04-20', Finish='2009-09-30', Resource='Banana')]\n",
88 | "\n",
89 | "colors = ['#7a0504', (0.2, 0.7, 0.3), 'rgb(210, 60, 180)']\n",
90 | "#colors = dict(Apple='rgb(220, 0, 0)', Grape='rgb(170, 14, 200)', Banana=(1, 0.9, 0.16))\n",
91 | "fig = ff.create_gantt(df, colors=colors, index_col='Resource', show_colorbar=True)\n",
92 | "fig.show()"
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": null,
98 | "metadata": {},
99 | "outputs": [],
100 | "source": [
101 | "import plotly.figure_factory as ff\n",
102 | "\n",
103 | "import pandas as pd\n",
104 | "df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gantt_example.csv')\n",
105 | "fig = ff.create_gantt(df, colors=['#333F44', '#93e4c1'], index_col='Complete',\n",
106 | " show_colorbar=True, bar_width=0.2, showgrid_x=True, showgrid_y=True)\n",
107 | "fig.show()\n",
108 | "#df"
109 | ]
110 | },
111 | {
112 | "cell_type": "code",
113 | "execution_count": null,
114 | "metadata": {},
115 | "outputs": [],
116 | "source": [
117 | "df"
118 | ]
119 | },
120 | {
121 | "cell_type": "markdown",
122 | "metadata": {},
123 | "source": [
124 | "## Using Hours and Minutes in Times"
125 | ]
126 | },
127 | {
128 | "cell_type": "code",
129 | "execution_count": null,
130 | "metadata": {},
131 | "outputs": [],
132 | "source": []
133 | },
134 | {
135 | "cell_type": "code",
136 | "execution_count": null,
137 | "metadata": {},
138 | "outputs": [],
139 | "source": [
140 | "import plotly.figure_factory as ff\n",
141 | "\n",
142 | "df = [\n",
143 | " dict(Task='Morning Sleep', Start='2016-01-01', Finish='2016-01-01 6:00:00', Resource='Sleep'),\n",
144 | " dict(Task='Breakfast', Start='2016-01-01 7:00:00', Finish='2016-01-01 7:30:00', Resource='Food'),\n",
145 | " dict(Task='Work', Start='2016-01-01 9:00:00', Finish='2016-01-01 11:25:00', Resource='Brain'),\n",
146 | " dict(Task='Break', Start='2016-01-01 11:30:00', Finish='2016-01-01 12:00:00', Resource='Rest'),\n",
147 | " dict(Task='Lunch', Start='2016-01-01 12:00:00', Finish='2016-01-01 13:00:00', Resource='Food'),\n",
148 | " dict(Task='Work', Start='2016-01-01 13:00:00', Finish='2016-01-01 17:00:00', Resource='Brain'),\n",
149 | " dict(Task='Exercise', Start='2016-01-01 17:30:00', Finish='2016-01-01 18:30:00', Resource='Cardio'),\n",
150 | " dict(Task='Post Workout Rest', Start='2016-01-01 18:30:00', Finish='2016-01-01 19:00:00', Resource='Rest'),\n",
151 | " dict(Task='Dinner', Start='2016-01-01 19:00:00', Finish='2016-01-01 20:00:00', Resource='Food'),\n",
152 | " dict(Task='Evening Sleep', Start='2016-01-01 21:00:00', Finish='2016-01-01 23:59:00', Resource='Sleep')\n",
153 | "]\n",
154 | "\n",
155 | "colors = dict(Cardio = 'rgb(46, 137, 205)',\n",
156 | " Food = 'rgb(114, 44, 121)',\n",
157 | " Sleep = 'rgb(198, 47, 105)',\n",
158 | " Brain = 'rgb(58, 149, 136)',\n",
159 | " Rest = 'rgb(107, 127, 135)')\n",
160 | "\n",
161 | "fig = ff.create_gantt(df, colors=colors, index_col='Resource', title='Daily Schedule',\n",
162 | " show_colorbar=True, bar_width=0.8, showgrid_x=True, showgrid_y=True)\n",
163 | "fig.show()"
164 | ]
165 | },
166 | {
167 | "cell_type": "markdown",
168 | "metadata": {},
169 | "source": [
170 | "## Group Tasks Together\n",
171 | "\n",
172 | "* *Resource* parameter is like a tag\n",
173 | "* Grouping is done by *Task* parameter"
174 | ]
175 | },
176 | {
177 | "cell_type": "code",
178 | "execution_count": null,
179 | "metadata": {},
180 | "outputs": [],
181 | "source": [
182 | "# To put all tasks of the same type in the same row on the plot\n",
183 | "import plotly.figure_factory as ff\n",
184 | "\n",
185 | "df = [dict(Task=\"Job-1\", Start='2017-01-01', Finish='2017-02-02', Resource='Complete'),\n",
186 | " dict(Task=\"Job-1\", Start='2017-02-15', Finish='2017-03-15', Resource='Incomplete'),\n",
187 | " dict(Task=\"Job-2\", Start='2017-01-17', Finish='2017-02-17', Resource='Complete'),\n",
188 | " dict(Task=\"Job-2\", Start='2017-02-27', Finish='2017-04-17', Resource='Not Started'),\n",
189 | " dict(Task=\"Job-3\", Start='2017-03-10', Finish='2017-03-20', Resource='Not Started'),\n",
190 | " dict(Task=\"Job-3\", Start='2017-04-01', Finish='2017-04-20', Resource='Incomplete'),\n",
191 | " dict(Task=\"Job-3\", Start='2017-05-18', Finish='2017-06-18', Resource='Not Started'),\n",
192 | " dict(Task=\"Job-4\", Start='2017-01-14', Finish='2017-03-14', Resource='Complete')]\n",
193 | "\n",
194 | "colors = {'Not Started': 'rgb(220, 0, 0)',\n",
195 | " 'Incomplete': (1, 0.9, 0.16),\n",
196 | " 'Complete': 'rgb(0, 255, 100)'}\n",
197 | "\n",
198 | "fig = ff.create_gantt(df, colors=colors, index_col='Resource', show_colorbar=True,group_tasks=True)\n",
199 | "fig.show()"
200 | ]
201 | },
202 | {
203 | "cell_type": "code",
204 | "execution_count": null,
205 | "metadata": {},
206 | "outputs": [],
207 | "source": []
208 | }
209 | ],
210 | "metadata": {
211 | "kernelspec": {
212 | "display_name": "Python 3",
213 | "language": "python",
214 | "name": "python3"
215 | },
216 | "language_info": {
217 | "codemirror_mode": {
218 | "name": "ipython",
219 | "version": 3
220 | },
221 | "file_extension": ".py",
222 | "mimetype": "text/x-python",
223 | "name": "python",
224 | "nbconvert_exporter": "python",
225 | "pygments_lexer": "ipython3",
226 | "version": "3.7.6"
227 | }
228 | },
229 | "nbformat": 4,
230 | "nbformat_minor": 2
231 | }
232 |
--------------------------------------------------------------------------------
/notebooks/08- Error Bars.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Error Bars:\n",
8 | "Error bars are graphical representations of the variability of data and used on graphs to indicate the error or uncertainty in a reported measurement. They give a general idea of how precise a measurement is, or conversely, how far from the reported value the true (error free) value might be. \n",
9 | "\n",
10 | "Error bars often represent one standard deviation of uncertainty, one standard error, or a particular confidence interval (e.g., a 95% interval). These quantities are not the same and so the measure selected should be stated explicitly in the graph or supporting text. [Error Bar on Wikipedia](https://en.wikipedia.org/wiki/Error_bar)\n",
11 | "\n",
12 | "\n",
13 | "\n",
14 | " ### In Plotly we plot Error Bars using the Scatter Plot"
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | " ## Error Bars with Plotly Express"
22 | ]
23 | },
24 | {
25 | "cell_type": "code",
26 | "execution_count": null,
27 | "metadata": {},
28 | "outputs": [],
29 | "source": [
30 | "# The Iris dataset https://archive.ics.uci.edu/ml/datasets/Iris\n",
31 | "import plotly.express as px\n",
32 | "df = px.data.iris()\n",
33 | "df[\"e\"] = df[\"sepal_width\"]/100\n",
34 | "fig = px.scatter(df, x=\"sepal_width\", y=\"sepal_length\", color=\"species\",\n",
35 | " error_x=\"e\", error_y=\"e\")\n",
36 | "fig.show()\n",
37 | "#df"
38 | ]
39 | },
40 | {
41 | "cell_type": "markdown",
42 | "metadata": {},
43 | "source": [
44 | "## Asymmetric Error Bars with Plotly Express"
45 | ]
46 | },
47 | {
48 | "cell_type": "code",
49 | "execution_count": null,
50 | "metadata": {},
51 | "outputs": [],
52 | "source": [
53 | "import plotly.express as px\n",
54 | "df = px.data.iris()\n",
55 | "df[\"e_plus\"] = df[\"sepal_width\"]/100\n",
56 | "df[\"e_minus\"] = df[\"sepal_width\"]/40\n",
57 | "fig = px.scatter(df, x=\"sepal_width\", y=\"sepal_length\", color=\"species\",\n",
58 | " error_y=\"e_plus\", error_y_minus=\"e_minus\")\n",
59 | "fig.show()"
60 | ]
61 | },
62 | {
63 | "cell_type": "markdown",
64 | "metadata": {},
65 | "source": [
66 | "## Error Bars with Graph Object"
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "execution_count": null,
72 | "metadata": {},
73 | "outputs": [],
74 | "source": [
75 | "# Basic Symmetric Error Bars\n",
76 | "import plotly.graph_objects as go\n",
77 | "\n",
78 | "fig = go.Figure(data=go.Scatter(\n",
79 | " x=[0, 1, 2],\n",
80 | " y=[6, 10, 2],\n",
81 | " error_y=dict(\n",
82 | " type='data', # value of error bar given in data coordinates\n",
83 | " array=[1, 2, 3],\n",
84 | " visible=True)\n",
85 | " ))\n",
86 | "fig.show()"
87 | ]
88 | },
89 | {
90 | "cell_type": "markdown",
91 | "metadata": {},
92 | "source": [
93 | "## Asymmetric Error Bars"
94 | ]
95 | },
96 | {
97 | "cell_type": "code",
98 | "execution_count": null,
99 | "metadata": {},
100 | "outputs": [],
101 | "source": [
102 | "import plotly.graph_objects as go\n",
103 | "\n",
104 | "fig = go.Figure(data=go.Scatter(\n",
105 | " x=[1, 2, 3, 4],\n",
106 | " y=[2, 1, 3, 4],\n",
107 | " mode = \"markers\",\n",
108 | " error_x=dict(\n",
109 | " type='data',\n",
110 | " symmetric=False,\n",
111 | " array=[0.1, 0.2, 0.1, 0.1],\n",
112 | " arrayminus=[0.2, 0.4, 1, 0.2]),\n",
113 | " error_y=dict(\n",
114 | " type='data',\n",
115 | " symmetric=False,\n",
116 | " array=[0.1, 0.2, 0.1, 0.1],\n",
117 | " arrayminus=[0.2, 0.4, 1, 0.2])\n",
118 | " )\n",
119 | ")\n",
120 | "fig.show()"
121 | ]
122 | },
123 | {
124 | "cell_type": "markdown",
125 | "metadata": {},
126 | "source": [
127 | "## Error Bars as a Percentage of the y Value"
128 | ]
129 | },
130 | {
131 | "cell_type": "code",
132 | "execution_count": null,
133 | "metadata": {},
134 | "outputs": [],
135 | "source": [
136 | "import plotly.graph_objects as go\n",
137 | "\n",
138 | "fig = go.Figure(data=go.Scatter(\n",
139 | " x=[0, 1, 2],\n",
140 | " y=[6, 10, 2],\n",
141 | " error_y=dict(\n",
142 | " type='percent', # value of error bar given as percentage of y value\n",
143 | " value=50,\n",
144 | " visible=True),\n",
145 | " error_x=dict(\n",
146 | " type='percent', # value of error bar given as percentage of x value\n",
147 | " value=30,\n",
148 | " visible=True)\n",
149 | " ))\n",
150 | "fig.show()"
151 | ]
152 | },
153 | {
154 | "cell_type": "markdown",
155 | "metadata": {},
156 | "source": [
157 | "## Horizontal Error Bars"
158 | ]
159 | },
160 | {
161 | "cell_type": "code",
162 | "execution_count": null,
163 | "metadata": {},
164 | "outputs": [],
165 | "source": [
166 | "import plotly.graph_objects as go\n",
167 | "\n",
168 | "fig = go.Figure(data=go.Scatter(\n",
169 | " x=[1, 2, 3, 4],\n",
170 | " y=[2, 1, 3, 4],\n",
171 | " error_x=dict(\n",
172 | " type='percent',\n",
173 | " value=10),\n",
174 | " error_y=dict(\n",
175 | " type='percent',\n",
176 | " value=60)\n",
177 | " ))\n",
178 | "fig.show()\n"
179 | ]
180 | },
181 | {
182 | "cell_type": "markdown",
183 | "metadata": {},
184 | "source": [
185 | "## Bar Chart with Error Bars"
186 | ]
187 | },
188 | {
189 | "cell_type": "code",
190 | "execution_count": null,
191 | "metadata": {},
192 | "outputs": [],
193 | "source": [
194 | "import plotly.graph_objects as go\n",
195 | "\n",
196 | "fig = go.Figure()\n",
197 | "fig.add_trace(go.Bar(\n",
198 | " name='Control',\n",
199 | " x=['Trial 1', 'Trial 2', 'Trial 3'], y=[3, 6, 4],\n",
200 | " error_y=dict(type='data', array=[1, 0.5, 1.5])\n",
201 | "))\n",
202 | "fig.add_trace(go.Bar(\n",
203 | " name='Experimental',\n",
204 | " x=['Trial 1', 'Trial 2', 'Trial 3'], y=[4, 7, 3],\n",
205 | " error_y=dict(type='data', array=[0.5, 1, 2])\n",
206 | "))\n",
207 | "fig.update_layout(barmode='group')\n",
208 | "fig.show()"
209 | ]
210 | },
211 | {
212 | "cell_type": "markdown",
213 | "metadata": {},
214 | "source": [
215 | "## Colored and Styled Error Bars"
216 | ]
217 | },
218 | {
219 | "cell_type": "code",
220 | "execution_count": null,
221 | "metadata": {},
222 | "outputs": [],
223 | "source": [
224 | "import plotly.graph_objects as go\n",
225 | "import numpy as np\n",
226 | "\n",
227 | "x_theo = np.linspace(-4, 4, 100)\n",
228 | "sincx = np.sinc(x_theo)\n",
229 | "x = [-3.8, -3.03, -1.91, -1.46, -0.89, -0.24, -0.0, 0.41, 0.89, 1.01, 1.91, 2.28, 2.79, 3.56]\n",
230 | "y = [-0.02, 0.04, -0.01, -0.27, 0.36, 0.75, 1.03, 0.65, 0.28, 0.02, -0.11, 0.16, 0.04, -0.15]\n",
231 | "\n",
232 | "fig = go.Figure()\n",
233 | "fig.add_trace(go.Scatter(\n",
234 | " x=x_theo, y=sincx,\n",
235 | " name='sinc(x)'\n",
236 | "))\n",
237 | "fig.add_trace(go.Scatter(\n",
238 | " x=x, y=y,\n",
239 | " mode='markers',\n",
240 | " name='measured',\n",
241 | " error_y=dict(\n",
242 | " type='constant',\n",
243 | " value=0.1,\n",
244 | " color='purple',\n",
245 | " thickness=1.5,\n",
246 | " width=3,\n",
247 | " ),\n",
248 | " error_x=dict(\n",
249 | " type='constant',\n",
250 | " value=0.2,\n",
251 | " color='red',\n",
252 | " thickness=1.5,\n",
253 | " width=3,\n",
254 | " ),\n",
255 | " marker=dict(color='purple', size=8)\n",
256 | "))\n",
257 | "fig.show()"
258 | ]
259 | },
260 | {
261 | "cell_type": "code",
262 | "execution_count": null,
263 | "metadata": {},
264 | "outputs": [],
265 | "source": [
266 | "import plotly.io as pio\n",
267 | "pio.write_html(fig, file='error_bar.html', auto_open=True)"
268 | ]
269 | },
270 | {
271 | "cell_type": "code",
272 | "execution_count": null,
273 | "metadata": {},
274 | "outputs": [],
275 | "source": []
276 | },
277 | {
278 | "cell_type": "code",
279 | "execution_count": null,
280 | "metadata": {},
281 | "outputs": [],
282 | "source": []
283 | },
284 | {
285 | "cell_type": "code",
286 | "execution_count": null,
287 | "metadata": {},
288 | "outputs": [],
289 | "source": []
290 | }
291 | ],
292 | "metadata": {
293 | "kernelspec": {
294 | "display_name": "Python 3",
295 | "language": "python",
296 | "name": "python3"
297 | },
298 | "language_info": {
299 | "codemirror_mode": {
300 | "name": "ipython",
301 | "version": 3
302 | },
303 | "file_extension": ".py",
304 | "mimetype": "text/x-python",
305 | "name": "python",
306 | "nbconvert_exporter": "python",
307 | "pygments_lexer": "ipython3",
308 | "version": "3.7.6"
309 | }
310 | },
311 | "nbformat": 4,
312 | "nbformat_minor": 2
313 | }
314 |
--------------------------------------------------------------------------------
/notebooks/09- Box Plots.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Box Plot:\n",
8 | "* In descriptive statistics, a box plot or boxplot is a method for graphically depicting groups of numerical data through their quartiles. \n",
9 | "* Box plots may also have lines extending from the boxes (whiskers) indicating variability outside the upper and lower quartiles, hence the terms box-and-whisker plot and box-and-whisker diagram. \n",
10 | "* Outliers may be plotted as individual points. \n",
11 | "* Box plots are non-parametric: they display variation in samples of a statistical population without making any assumptions of the underlying statistical distribution (though Tukey's boxplot assumes symmetry for the whiskers and normality for their length). \n",
12 | "* The spacings between the different parts of the box indicate the degree of dispersion (spread) and skewness in the data, and show outliers.\n",
13 | "* In addition to the points themselves, they allow one to visually estimate various L-estimators, notably the interquartile range, midhinge, range, mid-range, and trimean. \n",
14 | "* Box plots can be drawn either horizontally or vertically. \n",
15 | "* Box plots received their name from the box in the middle. \n",
16 | "[Box Plot on Wikipedia](https://en.wikipedia.org/wiki/Box_plot)\n",
17 | "\n",
18 | "\n",
19 | "\n",
20 | "* A box plot is a statistical representation of numerical data through their quartiles. \n",
21 | "* The ends of the box represent the lower and upper quartiles, while the median (second quartile) is marked by a line inside the box."
22 | ]
23 | },
24 | {
25 | "cell_type": "markdown",
26 | "metadata": {},
27 | "source": [
28 | "## Box Plot with plotly.express"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": null,
34 | "metadata": {},
35 | "outputs": [],
36 | "source": [
37 | "import plotly.express as px\n",
38 | "df = px.data.tips()\n",
39 | "\n",
40 | "#fig = px.box(df, y=\"total_bill\")\n",
41 | "# If a column name is given as x argument, a box plot is drawn for each value of x.\n",
42 | "fig = px.box(df, x=\"time\", y=\"total_bill\")\n",
43 | "fig.show()"
44 | ]
45 | },
46 | {
47 | "cell_type": "code",
48 | "execution_count": null,
49 | "metadata": {},
50 | "outputs": [],
51 | "source": [
52 | "df = df.iloc[0:200, :]\n",
53 | "data = df['total_bill']\n",
54 | "x = df['time']\n",
55 | "#fig = px.box(df, y=data)\n",
56 | "# If a column name is given as x argument, a box plot is drawn for each value of x.\n",
57 | "fig = px.box(x=x, y=data, color=x, points=\"all\")\n",
58 | "fig.show()\n"
59 | ]
60 | },
61 | {
62 | "cell_type": "code",
63 | "execution_count": null,
64 | "metadata": {},
65 | "outputs": [],
66 | "source": []
67 | },
68 | {
69 | "cell_type": "markdown",
70 | "metadata": {},
71 | "source": [
72 | "## Display the underlying data\n",
73 | "\n",
74 | "With the points argument, display underlying data points with either all points (all), outliers only (outliers, default), or none of them (False)."
75 | ]
76 | },
77 | {
78 | "cell_type": "code",
79 | "execution_count": null,
80 | "metadata": {},
81 | "outputs": [],
82 | "source": [
83 | "import plotly.express as px\n",
84 | "df = px.data.tips()\n",
85 | "fig = px.box(df, x=\"time\", y=\"total_bill\", points=\"all\")\n",
86 | "fig.show()"
87 | ]
88 | },
89 | {
90 | "cell_type": "markdown",
91 | "metadata": {},
92 | "source": [
93 | "## Choosing The Algorithm For Computing Quartiles\n",
94 | "* By default, quartiles for box plots are computed using the linear method (for more about linear interpolation, see #10 listed on http://www.amstat.org/publications/jse/v14n3/langford.html and https://en.wikipedia.org/wiki/Quartile for more details).\n",
95 | "\n",
96 | "* However, you can also choose to use an exclusive or an inclusive algorithm to compute quartiles.\n",
97 | "\n",
98 | "* The *exclusive* algorithm uses the median to divide the ordered dataset into two halves. If the sample is odd, it does not include the median in either half. Q1 is then the median of the lower half and Q3 is the median of the upper half.\n",
99 | "\n",
100 | "* The *inclusive* algorithm also uses the median to divide the ordered dataset into two halves, but if the sample is odd, it includes the median in both halves. Q1 is then the median of the lower half and Q3 the median of the upper half."
101 | ]
102 | },
103 | {
104 | "cell_type": "code",
105 | "execution_count": null,
106 | "metadata": {},
107 | "outputs": [],
108 | "source": [
109 | "import plotly.express as px\n",
110 | "\n",
111 | "df = px.data.tips()\n",
112 | "\n",
113 | "fig = px.box(df, x=\"day\", y=\"total_bill\", color=\"smoker\", points=\"all\")\n",
114 | "fig.update_traces(quartilemethod=\"exclusive\") # or \"inclusive\", or \"linear\" by default\n",
115 | "fig.show()"
116 | ]
117 | },
118 | {
119 | "cell_type": "markdown",
120 | "metadata": {},
121 | "source": [
122 | "## Difference Between Quartile Algorithms\n",
123 | "It can sometimes be difficult to see the difference between the linear, inclusive, and exclusive algorithms for computing quartiles. In the following example, the same dataset is visualized using each of the three different quartile computation algorithms."
124 | ]
125 | },
126 | {
127 | "cell_type": "code",
128 | "execution_count": null,
129 | "metadata": {},
130 | "outputs": [],
131 | "source": [
132 | "import plotly.express as px\n",
133 | "import pandas as pd\n",
134 | "\n",
135 | "data = [1,2,3,4,5,6,7,8,9]\n",
136 | "df = pd.DataFrame(dict(\n",
137 | " linear=data,\n",
138 | " inclusive=data,\n",
139 | " exclusive=data\n",
140 | ")).melt(var_name=\"quartilemethod\") \n",
141 | "\n",
142 | "\n",
143 | "fig = px.box(df, y=\"value\", facet_col=\"quartilemethod\", color=\"quartilemethod\",\n",
144 | " boxmode=\"overlay\", points='all')\n",
145 | "\n",
146 | "fig.update_traces(quartilemethod=\"linear\", jitter=0, col=1)\n",
147 | "fig.update_traces(quartilemethod=\"inclusive\", jitter=0, col=2)\n",
148 | "fig.update_traces(quartilemethod=\"exclusive\", jitter=0, col=3)\n",
149 | "\n",
150 | "fig.show()"
151 | ]
152 | },
153 | {
154 | "cell_type": "markdown",
155 | "metadata": {},
156 | "source": [
157 | "## Styled box plot\n",
158 | "For the interpretation of the notches, see https://en.wikipedia.org/wiki/Box_plot#Variations."
159 | ]
160 | },
161 | {
162 | "cell_type": "code",
163 | "execution_count": null,
164 | "metadata": {},
165 | "outputs": [],
166 | "source": [
167 | "import plotly.express as px\n",
168 | "df = px.data.tips()\n",
169 | "fig = px.box(df, x=\"time\", y=\"total_bill\", color=\"smoker\",\n",
170 | " notched=True, # used notched shape\n",
171 | " title=\"Box plot of total bill\",\n",
172 | " hover_data=[\"day\"] # add day column to hover data\n",
173 | " )\n",
174 | "fig.show()"
175 | ]
176 | },
177 | {
178 | "cell_type": "markdown",
179 | "metadata": {},
180 | "source": [
181 | "## Box plot with Graphic Objects"
182 | ]
183 | },
184 | {
185 | "cell_type": "code",
186 | "execution_count": null,
187 | "metadata": {},
188 | "outputs": [],
189 | "source": [
190 | "# Basic Box Plot\n",
191 | "import plotly.graph_objects as go\n",
192 | "import numpy as np\n",
193 | "np.random.seed(1)\n",
194 | "\n",
195 | "y0 = np.random.randn(50) - 1\n",
196 | "y1 = np.random.randn(50) + 1\n",
197 | "\n",
198 | "fig = go.Figure()\n",
199 | "fig.add_trace(go.Box(y=y0))\n",
200 | "fig.add_trace(go.Box(y=y1))\n",
201 | "\n",
202 | "fig.show()"
203 | ]
204 | },
205 | {
206 | "cell_type": "markdown",
207 | "metadata": {},
208 | "source": [
209 | "## Basic Horizontal Box Plot"
210 | ]
211 | },
212 | {
213 | "cell_type": "code",
214 | "execution_count": null,
215 | "metadata": {},
216 | "outputs": [],
217 | "source": [
218 | "import plotly.graph_objects as go\n",
219 | "import numpy as np\n",
220 | "\n",
221 | "x0 = np.random.randn(50)\n",
222 | "x1 = np.random.randn(50) + 2 # shift mean\n",
223 | "\n",
224 | "fig = go.Figure()\n",
225 | "# Use x instead of y argument for horizontal plot\n",
226 | "fig.add_trace(go.Box(x=x0))\n",
227 | "fig.add_trace(go.Box(x=x1))\n",
228 | "\n",
229 | "fig.show()"
230 | ]
231 | },
232 | {
233 | "cell_type": "markdown",
234 | "metadata": {},
235 | "source": [
236 | "## Box Plot That Displays The Underlying Data"
237 | ]
238 | },
239 | {
240 | "cell_type": "code",
241 | "execution_count": null,
242 | "metadata": {},
243 | "outputs": [],
244 | "source": [
245 | "import plotly.graph_objects as go\n",
246 | "\n",
247 | "fig = go.Figure(data=[go.Box(y=[0, 1, 1, 2, 3, 5, 8, 13, 9],\n",
248 | " boxpoints='all', # can also be outliers, or suspectedoutliers, or False\n",
249 | " jitter=0.3, # add some jitter for a better separation between points\n",
250 | " pointpos=-1.8 # relative position of points wrt box\n",
251 | " )])\n",
252 | "\n",
253 | "fig.show()"
254 | ]
255 | },
256 | {
257 | "cell_type": "markdown",
258 | "metadata": {},
259 | "source": [
260 | "## Modifying The Algorithm For Computing Quartiles"
261 | ]
262 | },
263 | {
264 | "cell_type": "code",
265 | "execution_count": null,
266 | "metadata": {},
267 | "outputs": [],
268 | "source": [
269 | "## See above for more explanation\n",
270 | "import plotly.graph_objects as go\n",
271 | "\n",
272 | "data = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
273 | "\n",
274 | "fig = go.Figure()\n",
275 | "fig.add_trace(go.Box(y=data, quartilemethod=\"linear\", name=\"Linear Quartile Mode\"))\n",
276 | "fig.add_trace(go.Box(y=data, quartilemethod=\"inclusive\", name=\"Inclusive Quartile Mode\"))\n",
277 | "fig.add_trace(go.Box(y=data, quartilemethod=\"exclusive\", name=\"Exclusive Quartile Mode\"))\n",
278 | "fig.update_traces(boxpoints='all', jitter=0)\n",
279 | "fig.show()"
280 | ]
281 | },
282 | {
283 | "cell_type": "markdown",
284 | "metadata": {},
285 | "source": [
286 | "## Box Plot With Precomputed Quartiles\n",
287 | "This could be useful if you have already pre-computed those values or if you need to use a different algorithm than the ones provided."
288 | ]
289 | },
290 | {
291 | "cell_type": "code",
292 | "execution_count": null,
293 | "metadata": {},
294 | "outputs": [],
295 | "source": [
296 | "import plotly.graph_objects as go\n",
297 | "\n",
298 | "fig = go.Figure()\n",
299 | "\n",
300 | "fig.add_trace(go.Box(y=[\n",
301 | " [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],\n",
302 | " [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],\n",
303 | " [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]\n",
304 | " ], name=\"Precompiled Quartiles\"))\n",
305 | "\n",
306 | "fig.update_traces(q1=[ 1, 2, 3 ], median=[ 4, 5, 6 ], \n",
307 | " q3=[ 7, 8, 9 ], lowerfence=[-1, 0, 1], \n",
308 | " upperfence=[5, 6, 7], mean=[ 2.2, 2.8, 3.2 ], \n",
309 | " sd=[ 0.2, 0.4, 0.6 ], notchspan=[ 0.2, 0.4, 0.6 ] )\n",
310 | "\n",
311 | "fig.show()"
312 | ]
313 | },
314 | {
315 | "cell_type": "markdown",
316 | "metadata": {},
317 | "source": [
318 | "## Colored Box Plot"
319 | ]
320 | },
321 | {
322 | "cell_type": "code",
323 | "execution_count": null,
324 | "metadata": {},
325 | "outputs": [],
326 | "source": [
327 | "import plotly.graph_objects as go\n",
328 | "import numpy as np\n",
329 | "\n",
330 | "y0 = np.random.randn(50)\n",
331 | "y1 = np.random.randn(50) + 1 # shift mean\n",
332 | "\n",
333 | "fig = go.Figure()\n",
334 | "fig.add_trace(go.Box(y=y0, name='Sample A',\n",
335 | " marker_color = 'indianred'))\n",
336 | "fig.add_trace(go.Box(y=y1, name = 'Sample B',\n",
337 | " marker_color = 'lightseagreen'))\n",
338 | "\n",
339 | "fig.show()"
340 | ]
341 | },
342 | {
343 | "cell_type": "markdown",
344 | "metadata": {},
345 | "source": [
346 | "## Box Plot Styling Mean & Standard Deviation"
347 | ]
348 | },
349 | {
350 | "cell_type": "code",
351 | "execution_count": null,
352 | "metadata": {},
353 | "outputs": [],
354 | "source": [
355 | "import plotly.graph_objects as go\n",
356 | "\n",
357 | "fig = go.Figure()\n",
358 | "fig.add_trace(go.Box(\n",
359 | " y=[2.37, 2.16, 4.82, 1.73, 1.04, 0.23, 1.32, 2.91, 0.11, 4.51, 0.51, 3.75, 1.35, 2.98, 4.50, 0.18, 4.66, 1.30, 2.06, 1.19],\n",
360 | " name='Only Mean',\n",
361 | " marker_color='darkblue',\n",
362 | " boxmean=True # represent mean\n",
363 | "))\n",
364 | "fig.add_trace(go.Box(\n",
365 | " y=[2.37, 2.16, 4.82, 1.73, 1.04, 0.23, 1.32, 2.91, 0.11, 4.51, 0.51, 3.75, 1.35, 2.98, 4.50, 0.18, 4.66, 1.30, 2.06, 1.19],\n",
366 | " name='Mean & SD',\n",
367 | " marker_color='royalblue',\n",
368 | " boxmean='sd' # represent mean and standard deviation\n",
369 | "))\n",
370 | "\n",
371 | "fig.show()"
372 | ]
373 | },
374 | {
375 | "cell_type": "markdown",
376 | "metadata": {},
377 | "source": [
378 | "## Grouped Box Plots"
379 | ]
380 | },
381 | {
382 | "cell_type": "code",
383 | "execution_count": null,
384 | "metadata": {},
385 | "outputs": [],
386 | "source": [
387 | "import plotly.graph_objects as go\n",
388 | "\n",
389 | "x = ['day 1', 'day 1', 'day 1', 'day 1', 'day 1', 'day 1',\n",
390 | " 'day 2', 'day 2', 'day 2', 'day 2', 'day 2', 'day 2']\n",
391 | "\n",
392 | "fig = go.Figure()\n",
393 | "\n",
394 | "fig.add_trace(go.Box(\n",
395 | " y=[0.2, 0.2, 0.6, 1.0, 0.5, 0.4, 0.2, 0.7, 0.9, 0.1, 0.5, 0.3],\n",
396 | " x=x,\n",
397 | " name='kale',\n",
398 | " marker_color='#3D9970'\n",
399 | "))\n",
400 | "fig.add_trace(go.Box(\n",
401 | " y=[0.6, 0.7, 0.3, 0.6, 0.0, 0.5, 0.7, 0.9, 0.5, 0.8, 0.7, 0.2],\n",
402 | " x=x,\n",
403 | " name='radishes',\n",
404 | " marker_color='#FF4136'\n",
405 | "))\n",
406 | "fig.add_trace(go.Box(\n",
407 | " y=[0.1, 0.3, 0.1, 0.9, 0.6, 0.6, 0.9, 1.0, 0.3, 0.6, 0.8, 0.5],\n",
408 | " x=x,\n",
409 | " name='carrots',\n",
410 | " marker_color='#FF851B'\n",
411 | "))\n",
412 | "\n",
413 | "fig.update_layout(\n",
414 | " yaxis_title='normalized moisture',\n",
415 | " boxmode='group' # group together boxes of the different traces for each value of x\n",
416 | ")\n",
417 | "fig.show()"
418 | ]
419 | },
420 | {
421 | "cell_type": "code",
422 | "execution_count": null,
423 | "metadata": {},
424 | "outputs": [],
425 | "source": []
426 | }
427 | ],
428 | "metadata": {
429 | "kernelspec": {
430 | "display_name": "Python 3",
431 | "language": "python",
432 | "name": "python3"
433 | },
434 | "language_info": {
435 | "codemirror_mode": {
436 | "name": "ipython",
437 | "version": 3
438 | },
439 | "file_extension": ".py",
440 | "mimetype": "text/x-python",
441 | "name": "python",
442 | "nbconvert_exporter": "python",
443 | "pygments_lexer": "ipython3",
444 | "version": "3.7.6"
445 | }
446 | },
447 | "nbformat": 4,
448 | "nbformat_minor": 2
449 | }
450 |
--------------------------------------------------------------------------------
/notebooks/10- Scatterplot Matrix.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Scatter Matrix\n",
8 | "* A scatterplot matrix is a matrix associated to *n* numerical arrays (data variables), *X1,X2,...,Xn*, of the same length. \n",
9 | "* The cell *(i,j)* of such a matrix displays the scatter plot of the variable *Xi* versus *Xj*."
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "## Using Plotly Express"
17 | ]
18 | },
19 | {
20 | "cell_type": "code",
21 | "execution_count": null,
22 | "metadata": {},
23 | "outputs": [],
24 | "source": [
25 | "# Plot the scatter matrix for the columns of the dataframe. \n",
26 | "# By default, all columns are considered.\n",
27 | "import plotly.express as px\n",
28 | "df = px.data.iris()\n",
29 | "fig = px.scatter_matrix(df)\n",
30 | "fig.show()\n",
31 | "#df"
32 | ]
33 | },
34 | {
35 | "cell_type": "markdown",
36 | "metadata": {},
37 | "source": [
38 | "Specify the columns to be represented with the *dimensions* argument, and set colors using a column of the dataframe:"
39 | ]
40 | },
41 | {
42 | "cell_type": "code",
43 | "execution_count": null,
44 | "metadata": {},
45 | "outputs": [],
46 | "source": [
47 | "import plotly.express as px\n",
48 | "df = px.data.iris()\n",
49 | "fig = px.scatter_matrix(df,\n",
50 | " dimensions=[\"sepal_width\", \"sepal_length\", \"petal_width\", \"petal_length\"],\n",
51 | " color=\"species\")\n",
52 | "fig.show()"
53 | ]
54 | },
55 | {
56 | "cell_type": "markdown",
57 | "metadata": {},
58 | "source": [
59 | "## Styled Scatter Matrix with Plotly Express"
60 | ]
61 | },
62 | {
63 | "cell_type": "code",
64 | "execution_count": null,
65 | "metadata": {},
66 | "outputs": [],
67 | "source": [
68 | "import plotly.express as px\n",
69 | "df = px.data.iris()\n",
70 | "fig = px.scatter_matrix(df,\n",
71 | " dimensions=[\"sepal_width\", \"sepal_length\", \"petal_width\", \"petal_length\"],\n",
72 | " color=\"species\", symbol=\"species\",\n",
73 | " title=\"Scatter matrix of iris data set\",\n",
74 | " labels={col:col.replace('_', ' ') for col in df.columns}) # remove underscore\n",
75 | "fig.update_traces(diagonal_visible=False)\n",
76 | "fig.show()"
77 | ]
78 | },
79 | {
80 | "cell_type": "markdown",
81 | "metadata": {},
82 | "source": [
83 | "## Scatter Matrix with Graphic Object\n",
84 | "* It is possible to use the more generic go.Splom function.\n",
85 | "* The Plotly *splom* trace implementation for the scatterplot matrix does not require to set *x=Xi , and y=Xj*, for each scatter plot. \n",
86 | "* All arrays, *X1,X2,...,Xn* , are passed once, through a list of dicts called *dimensions*, i.e. each array/variable represents a dimension.\n",
87 | "* A trace of type splom is defined as follows:\n",
88 | "\n",
89 | "$\\qquad$trace=go.Splom(dimensions=[dict(label='string-1',
\n",
90 | "$\\qquad\\qquad$ values=X1),
\n",
91 | "$\\qquad\\qquad$ dict(label='string-2',
\n",
92 | "$\\qquad\\qquad$ values=X2),
\n",
93 | "$\\qquad\\qquad$ .
\n",
94 | "$\\qquad\\qquad$ .
\n",
95 | "$\\qquad\\qquad$ .
\n",
96 | "$\\qquad\\qquad$ dict(label='string-n',
\n",
97 | "$\\qquad\\qquad$ values=Xn)],
\n",
98 | "$\\qquad\\qquad$ ....
\n",
99 | "$\\qquad$ )
\n",
100 | "\n",
101 | "* The label in each dimension is assigned to the axes titles of the corresponding matrix cell.\n",
102 | "* More here: https://plot.ly/python/reference/#splom"
103 | ]
104 | },
105 | {
106 | "cell_type": "markdown",
107 | "metadata": {},
108 | "source": [
109 | "## Splom of the Iris data set"
110 | ]
111 | },
112 | {
113 | "cell_type": "code",
114 | "execution_count": null,
115 | "metadata": {},
116 | "outputs": [],
117 | "source": [
118 | "import plotly.graph_objects as go\n",
119 | "import pandas as pd\n",
120 | "\n",
121 | "df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/iris-data.csv')\n",
122 | "\n",
123 | "# The Iris dataset contains four data variables, sepal length, sepal width, petal length,\n",
124 | "# petal width, for 150 iris flowers. The flowers are labeled as `Iris-setosa`,\n",
125 | "# `Iris-versicolor`, `Iris-virginica`.\n",
126 | "\n",
127 | "# Define indices corresponding to flower categories, using pandas label encoding\n",
128 | "index_vals = df['class'].astype('category').cat.codes\n",
129 | "\n",
130 | "fig = go.Figure(data=go.Splom(\n",
131 | " dimensions=[dict(label='sepal length', values=df['sepal length'], visible=True),\n",
132 | " dict(label='sepal width', values=df['sepal width'], visible=True),\n",
133 | " dict(label='petal length', values=df['petal length'], visible=True),\n",
134 | " dict(label='petal width', values=df['petal width'], visible=True)],\n",
135 | " #We can choose to remove a variable from splom, by setting \n",
136 | " #visible=False in its corresponding dimension. \n",
137 | " #In this case the default grid associated to the scatterplot \n",
138 | " #matrix keeps its number of cells, but the cells in the row \n",
139 | " #and column corresponding to the visible false dimension are empty\n",
140 | " text=df['class'],\n",
141 | " marker=dict(color=index_vals,\n",
142 | " showscale=False, # colors encode categorical variables\n",
143 | " line_color='white', line_width=0.5),\n",
144 | " diagonal_visible=False, # show or remove plots on diagonal\n",
145 | " # To plot only the lower/upper half of the splom we switch the default showlowerhalf=True/showupperhalf=True to False:\n",
146 | " showupperhalf=False, # plot only the lower/upper half\n",
147 | " ) \n",
148 | " )\n",
149 | "\n",
150 | "\n",
151 | "fig.update_layout(\n",
152 | " title='Iris Data set',\n",
153 | " dragmode='select',\n",
154 | " width=600,\n",
155 | " height=600,\n",
156 | " hovermode='closest',\n",
157 | ")\n",
158 | "\n",
159 | "fig.show()"
160 | ]
161 | },
162 | {
163 | "cell_type": "code",
164 | "execution_count": null,
165 | "metadata": {},
166 | "outputs": [],
167 | "source": [
168 | "index_vals"
169 | ]
170 | },
171 | {
172 | "cell_type": "markdown",
173 | "metadata": {},
174 | "source": [
175 | "## Question: How can we display something else in the diagonal?"
176 | ]
177 | },
178 | {
179 | "cell_type": "markdown",
180 | "metadata": {},
181 | "source": [
182 | "## Using Figure Factory\n",
183 | "https://plot.ly/python/figure-factory-subplots/#plotlys-figure-factory-module\n",
184 | "\n",
185 | "We can customize what to display in the diagonal"
186 | ]
187 | },
188 | {
189 | "cell_type": "code",
190 | "execution_count": null,
191 | "metadata": {},
192 | "outputs": [],
193 | "source": [
194 | "import plotly.figure_factory as ff\n",
195 | "\n",
196 | "import pandas as pd\n",
197 | "\n",
198 | "df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/iris-data.csv')\n",
199 | "\n",
200 | "fig = ff.create_scatterplotmatrix(df, diag='histogram', index='class',\n",
201 | " height=800, width=800)\n",
202 | "fig.show()"
203 | ]
204 | },
205 | {
206 | "cell_type": "code",
207 | "execution_count": null,
208 | "metadata": {},
209 | "outputs": [],
210 | "source": []
211 | },
212 | {
213 | "cell_type": "code",
214 | "execution_count": null,
215 | "metadata": {},
216 | "outputs": [],
217 | "source": [
218 | "df"
219 | ]
220 | },
221 | {
222 | "cell_type": "code",
223 | "execution_count": null,
224 | "metadata": {},
225 | "outputs": [],
226 | "source": []
227 | }
228 | ],
229 | "metadata": {
230 | "kernelspec": {
231 | "display_name": "Python 3",
232 | "language": "python",
233 | "name": "python3"
234 | },
235 | "language_info": {
236 | "codemirror_mode": {
237 | "name": "ipython",
238 | "version": 3
239 | },
240 | "file_extension": ".py",
241 | "mimetype": "text/x-python",
242 | "name": "python",
243 | "nbconvert_exporter": "python",
244 | "pygments_lexer": "ipython3",
245 | "version": "3.7.6"
246 | }
247 | },
248 | "nbformat": 4,
249 | "nbformat_minor": 2
250 | }
251 |
--------------------------------------------------------------------------------
/notebooks/11- Histograms.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Histograms\n",
8 | "\n",
9 | "A histogram is an accurate representation of the distribution of numerical data. It is an estimate of the probability distribution of a continuous variable and was first introduced by Karl Pearson. It differs from a bar graph, in the sense that a bar graph relates two variables, but a histogram relates only one. To construct a histogram, the first step is to *\"bin\"* (or \"bucket\") the range of values-that is, divide the entire range of values into a series of intervals-and then count how many values fall into each interval. The bins are usually specified as consecutive, non-overlapping intervals of a variable. The bins (intervals) must be adjacent, and are often (but not required to be) of equal size. [Histogram on Wikipedia](https://en.wikipedia.org/wiki/Histogram)\n",
10 | " \n",
11 | "\n",
12 | "* Histograms are a special form of bar chart where the data represent continuous rather than discrete categories. \n",
13 | "* For example a histogram could be used to present details of the average number of hours exercise carried out by people of different ages because age is a continuous rather than a discrete category. \n",
14 | "* However, because a continuous category may have a large number of possible values the data are often grouped to reduce the number of data points. \n",
15 | "* For example, instead of drawing a bar for each individual age between 0 and 65, the data could be grouped into a series of continuous age ranges such as 16-24, 25-34, 35-44 etc.\n",
16 | "* Unlike a bar chart, in a histogram both the x- and y-axes have a scale. \n",
17 | "* This means that it is the area of the bar that is proportional to the size of the category represented and not just its height. [Source](https://www.le.ac.uk/oerresources/ssds/numeracyskills/page_32.htm)\n"
18 | ]
19 | },
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {},
23 | "source": [
24 | "## Histogram with Plotly Express\n",
25 | "In plotly a histogram is an aggregated bar chart, with several possible aggregation functions (e.g. sum, average, count...). Also, the data to be binned can be numerical data but also categorical or date data."
26 | ]
27 | },
28 | {
29 | "cell_type": "code",
30 | "execution_count": null,
31 | "metadata": {},
32 | "outputs": [],
33 | "source": [
34 | "import plotly.express as px\n",
35 | "df = px.data.tips()\n",
36 | "#fig = px.histogram(df, x=\"total_bill\")\n",
37 | "fig = px.histogram(df, x=\"total_bill\", nbins=55) # choose number of bins\n",
38 | "# Here we use a column with categorical data\n",
39 | "#fig = px.histogram(df, x=\"day\", nbins=2)\n",
40 | "fig.show()"
41 | ]
42 | },
43 | {
44 | "cell_type": "markdown",
45 | "metadata": {},
46 | "source": [
47 | "## Type of Normalization and Other Options\n",
48 | "* The default mode is to represent the count of samples in each bin. \n",
49 | "* With the *histnorm* argument, it is also possible to represent:\n",
50 | "* the percentage or fraction of samples in each bin (*histnorm='percent'* or *probability*)\n",
51 | "* or a density histogram (the sum of bars is equal to 100, *density*)\n",
52 | "* or a probability density histogram (sum equal to 1, *probability density*)."
53 | ]
54 | },
55 | {
56 | "cell_type": "code",
57 | "execution_count": null,
58 | "metadata": {},
59 | "outputs": [],
60 | "source": [
61 | "import plotly.express as px\n",
62 | "df = px.data.tips()\n",
63 | "#fig = px.histogram(df, x=\"total_bill\", histnorm='probability')\n",
64 | "fig = px.histogram(df, x=\"total_bill\",\n",
65 | " title='Histogram of bills',\n",
66 | " histnorm='probability',\n",
67 | " labels={'total_bill':'total bill'}, # can specify one label per df column\n",
68 | " opacity=0.8,\n",
69 | " #log_y=True, # represent bars with log scale\n",
70 | " color_discrete_sequence=['indianred'] # color of histogram bars\n",
71 | " )\n",
72 | "fig.show()"
73 | ]
74 | },
75 | {
76 | "cell_type": "markdown",
77 | "metadata": {},
78 | "source": [
79 | "## Several histograms for the different values of one column"
80 | ]
81 | },
82 | {
83 | "cell_type": "code",
84 | "execution_count": null,
85 | "metadata": {},
86 | "outputs": [],
87 | "source": [
88 | "import plotly.express as px\n",
89 | "df = px.data.tips()\n",
90 | "fig = px.histogram(df, x=\"total_bill\", color=\"sex\", opacity=0.5) \n",
91 | "fig.show()"
92 | ]
93 | },
94 | {
95 | "cell_type": "markdown",
96 | "metadata": {},
97 | "source": [
98 | "## Visualizing the distribution\n",
99 | "* With the *marginal* keyword, a subplot is drawn alongside the histogram, visualizing the distribution.\n",
100 | "* More on distribution plots later"
101 | ]
102 | },
103 | {
104 | "cell_type": "code",
105 | "execution_count": null,
106 | "metadata": {},
107 | "outputs": [],
108 | "source": [
109 | "import plotly.express as px\n",
110 | "df = px.data.tips()\n",
111 | "fig = px.histogram(df, x=\"total_bill\", color=\"sex\", marginal=\"violin\", # can be `box`, `violin`\n",
112 | " hover_data=df.columns)\n",
113 | "fig.show()"
114 | ]
115 | },
116 | {
117 | "cell_type": "markdown",
118 | "metadata": {},
119 | "source": [
120 | "## Histograms using Graphic Object"
121 | ]
122 | },
123 | {
124 | "cell_type": "code",
125 | "execution_count": null,
126 | "metadata": {
127 | "scrolled": false
128 | },
129 | "outputs": [],
130 | "source": [
131 | "# Basic Histogram\n",
132 | "import plotly.graph_objects as go\n",
133 | "\n",
134 | "import numpy as np\n",
135 | "np.random.seed(1)\n",
136 | "\n",
137 | "x = np.random.randn(500)\n",
138 | "\n",
139 | "#fig = go.Figure(data=[go.Histogram(x=x)])\n",
140 | "#Normalized Histogram\n",
141 | "fig = go.Figure(data=[go.Histogram(x=x, histnorm='probability')])\n",
142 | "fig.show()"
143 | ]
144 | },
145 | {
146 | "cell_type": "markdown",
147 | "metadata": {},
148 | "source": [
149 | "## Horizontal Histogram"
150 | ]
151 | },
152 | {
153 | "cell_type": "code",
154 | "execution_count": null,
155 | "metadata": {},
156 | "outputs": [],
157 | "source": [
158 | "import plotly.graph_objects as go\n",
159 | "\n",
160 | "import numpy as np\n",
161 | "\n",
162 | "y = np.random.randn(500)\n",
163 | "# Use `y` argument instead of `x` for horizontal histogram\n",
164 | "\n",
165 | "fig = go.Figure(data=[go.Histogram(y=y)])\n",
166 | "fig.show()"
167 | ]
168 | },
169 | {
170 | "cell_type": "markdown",
171 | "metadata": {},
172 | "source": [
173 | "## Overlaid Histogram"
174 | ]
175 | },
176 | {
177 | "cell_type": "code",
178 | "execution_count": null,
179 | "metadata": {},
180 | "outputs": [],
181 | "source": [
182 | "import plotly.graph_objects as go\n",
183 | "\n",
184 | "import numpy as np\n",
185 | "\n",
186 | "x0 = np.random.randn(500)\n",
187 | "# Add 1 to shift the mean of the Gaussian distribution\n",
188 | "x1 = np.random.randn(500) + 1\n",
189 | "\n",
190 | "fig = go.Figure()\n",
191 | "fig.add_trace(go.Histogram(x=x0))\n",
192 | "fig.add_trace(go.Histogram(x=x1))\n",
193 | "\n",
194 | "# Overlay both histograms\n",
195 | "fig.update_layout(barmode='overlay')\n",
196 | "# Reduce opacity to see both histograms\n",
197 | "fig.update_traces(opacity=0.55)\n",
198 | "fig.show()"
199 | ]
200 | },
201 | {
202 | "cell_type": "markdown",
203 | "metadata": {},
204 | "source": [
205 | "## Stacked Histograms"
206 | ]
207 | },
208 | {
209 | "cell_type": "code",
210 | "execution_count": null,
211 | "metadata": {},
212 | "outputs": [],
213 | "source": [
214 | "import plotly.graph_objects as go\n",
215 | "\n",
216 | "import numpy as np\n",
217 | "\n",
218 | "x0 = np.random.randn(2000)\n",
219 | "x1 = np.random.randn(2000) + 1\n",
220 | "\n",
221 | "fig = go.Figure()\n",
222 | "fig.add_trace(go.Histogram(x=x0))\n",
223 | "fig.add_trace(go.Histogram(x=x1))\n",
224 | "\n",
225 | "# The two histograms are drawn on top of another\n",
226 | "fig.update_layout(barmode='stack')\n",
227 | "fig.update_traces(opacity=0.75)\n",
228 | "\n",
229 | "fig.show()"
230 | ]
231 | },
232 | {
233 | "cell_type": "markdown",
234 | "metadata": {},
235 | "source": [
236 | "## Styled Histogram"
237 | ]
238 | },
239 | {
240 | "cell_type": "code",
241 | "execution_count": null,
242 | "metadata": {},
243 | "outputs": [],
244 | "source": [
245 | "import plotly.graph_objects as go\n",
246 | "\n",
247 | "import numpy as np\n",
248 | "x0 = np.random.randn(500)\n",
249 | "x1 = np.random.randn(500) + 1\n",
250 | "\n",
251 | "fig = go.Figure()\n",
252 | "fig.add_trace(go.Histogram(\n",
253 | " x=x0,\n",
254 | " histnorm='percent',\n",
255 | " name='control', # name used in legend and hover labels\n",
256 | " xbins=dict( # bins used for histogram\n",
257 | " start=-4.0,\n",
258 | " end=3.0,\n",
259 | " size=0.5\n",
260 | " ),\n",
261 | " marker_color='#EB89B5',\n",
262 | " opacity=0.75\n",
263 | "))\n",
264 | "fig.add_trace(go.Histogram(\n",
265 | " x=x1,\n",
266 | " histnorm='percent',\n",
267 | " name='experimental',\n",
268 | " xbins=dict(\n",
269 | " start=-3.0,\n",
270 | " end=4,\n",
271 | " size=0.5\n",
272 | " ),\n",
273 | " marker_color='#330C73',\n",
274 | " opacity=0.75\n",
275 | "))\n",
276 | "\n",
277 | "fig.update_layout(\n",
278 | " title_text='Sampled Results', # title of plot\n",
279 | " xaxis_title_text='Value', # xaxis label\n",
280 | " yaxis_title_text='Count', # yaxis label\n",
281 | " bargap=0.2, # gap between bars of adjacent location coordinates\n",
282 | " bargroupgap=0.1 # gap between bars of the same location coordinates\n",
283 | ")\n",
284 | "\n",
285 | "fig.show()"
286 | ]
287 | },
288 | {
289 | "cell_type": "markdown",
290 | "metadata": {},
291 | "source": [
292 | "## Cumulative Histogram\n",
293 | "The cumulative histogram is a histogram in which the vertical axis gives not just the counts for a single bin, but rather gives the counts for that bin plus all bins for smaller values of the response variable. [Source](https://mipav.cit.nih.gov/pubwiki/index.php/Cumulative_Histogram)"
294 | ]
295 | },
296 | {
297 | "cell_type": "code",
298 | "execution_count": null,
299 | "metadata": {},
300 | "outputs": [],
301 | "source": [
302 | "import plotly.graph_objects as go\n",
303 | "\n",
304 | "import numpy as np\n",
305 | "\n",
306 | "x = np.random.randn(500)\n",
307 | "fig = go.Figure(data=[go.Histogram(x=x, cumulative_enabled=True)])\n",
308 | "\n",
309 | "fig.show()"
310 | ]
311 | },
312 | {
313 | "cell_type": "markdown",
314 | "metadata": {},
315 | "source": [
316 | "## Custom Binning\n",
317 | "\n",
318 | "* For custom binning along *x-axis*, use the attribute *nbinsx*. \n",
319 | "* Please note that the autobin algorithm will choose a *'nice'* round bin size that may result in somewhat fewer than nbinsx total bins. \n",
320 | "* Alternatively, you can set the exact values for *xbins* along with *autobinx = False*.\n"
321 | ]
322 | },
323 | {
324 | "cell_type": "code",
325 | "execution_count": null,
326 | "metadata": {
327 | "scrolled": false
328 | },
329 | "outputs": [],
330 | "source": [
331 | "import plotly.graph_objects as go\n",
332 | "from plotly.subplots import make_subplots\n",
333 | "\n",
334 | "x = ['1970-01-01', '1970-01-01', '1970-02-01', '1970-04-01', '1970-01-02',\n",
335 | " '1972-01-31', '1970-02-13', '1971-04-19']\n",
336 | "\n",
337 | "fig = make_subplots(rows=3, cols=2)\n",
338 | "\n",
339 | "trace0 = go.Histogram(x=x, nbinsx=4)\n",
340 | "trace1 = go.Histogram(x=x, nbinsx = 8)\n",
341 | "trace2 = go.Histogram(x=x, nbinsx=10)\n",
342 | "trace3 = go.Histogram(x=x,\n",
343 | " xbins=dict(\n",
344 | " start='1969-11-15',\n",
345 | " end='1972-03-31',\n",
346 | " size='M18'), # M18 stands for 18 months\n",
347 | " autobinx=False\n",
348 | " )\n",
349 | "trace4 = go.Histogram(x=x,\n",
350 | " xbins=dict(\n",
351 | " start='1969-11-15',\n",
352 | " end='1972-03-31',\n",
353 | " size='M4'), # 4 months bin size\n",
354 | " autobinx=False\n",
355 | " )\n",
356 | "trace5 = go.Histogram(x=x,\n",
357 | " xbins=dict(\n",
358 | " start='1969-11-15',\n",
359 | " end='1972-03-31',\n",
360 | " size= 'M2'), # 2 months\n",
361 | " autobinx = False\n",
362 | " )\n",
363 | "\n",
364 | "fig.append_trace(trace0, 1, 1)\n",
365 | "fig.append_trace(trace1, 1, 2)\n",
366 | "fig.append_trace(trace2, 2, 1)\n",
367 | "fig.append_trace(trace3, 2, 2)\n",
368 | "fig.append_trace(trace4, 3, 1)\n",
369 | "fig.append_trace(trace5, 3, 2)\n",
370 | "\n",
371 | "fig.show()"
372 | ]
373 | },
374 | {
375 | "cell_type": "markdown",
376 | "metadata": {},
377 | "source": [
378 | "## Share bins between histograms\n",
379 | "In this example both histograms have a compatible bin settings using *bingroup* attribute. Note that traces on the same subplot, and with the same *barmode* (\"stack\", \"relative\", \"group\") are forced into the same *bingroup*, however traces with *barmode = \"overlay\"* and on different axes (of the same axis type) can have compatible bin settings. "
380 | ]
381 | },
382 | {
383 | "cell_type": "code",
384 | "execution_count": null,
385 | "metadata": {},
386 | "outputs": [],
387 | "source": [
388 | "import plotly.graph_objects as go\n",
389 | "import numpy as np\n",
390 | "\n",
391 | "fig = go.Figure(go.Histogram(\n",
392 | " x=np.random.randint(7, size=100),\n",
393 | " bingroup=1))\n",
394 | "\n",
395 | "fig.add_trace(go.Histogram(\n",
396 | " x=np.random.randint(7, size=20),\n",
397 | " bingroup=1))\n",
398 | "\n",
399 | "fig.update_layout(\n",
400 | " barmode=\"overlay\",\n",
401 | " bargap=0.1)\n",
402 | "\n",
403 | "fig.show()"
404 | ]
405 | },
406 | {
407 | "cell_type": "markdown",
408 | "metadata": {},
409 | "source": [
410 | "## 2D Histogram of a Bivariate Normal Distribution"
411 | ]
412 | },
413 | {
414 | "cell_type": "code",
415 | "execution_count": null,
416 | "metadata": {},
417 | "outputs": [],
418 | "source": [
419 | "import plotly.graph_objects as go\n",
420 | "\n",
421 | "import numpy as np\n",
422 | "\n",
423 | "x = np.random.randn(500)\n",
424 | "y = np.random.randn(500)+1\n",
425 | "\n",
426 | "fig = go.Figure(go.Histogram2d(x=x, y=y, histnorm='probability',\n",
427 | " autobinx=False,\n",
428 | " xbins=dict(start=-3, end=3, size=0.1),\n",
429 | " autobiny=False,\n",
430 | " ybins=dict(start=-2.5, end=4, size=0.1),\n",
431 | " colorscale=[[0, 'rgb(12,51,131)'], [0.25, 'rgb(10,136,186)'], [0.5, 'rgb(242,211,56)'], [0.75, 'rgb(242,143,56)'], [1, 'rgb(217,30,30)']]\n",
432 | " ))\n",
433 | "fig.show()"
434 | ]
435 | },
436 | {
437 | "cell_type": "markdown",
438 | "metadata": {},
439 | "source": [
440 | "## Sharing bin settings between 2D Histograms"
441 | ]
442 | },
443 | {
444 | "cell_type": "markdown",
445 | "metadata": {},
446 | "source": [
447 | "This example shows how to use *bingroup* attribute to have a compatible bin settings for both histograms. To define *start*, end and *size* value of *x-axis* and *y-axis* seperatly, set *ybins* and *xbins*."
448 | ]
449 | },
450 | {
451 | "cell_type": "code",
452 | "execution_count": null,
453 | "metadata": {},
454 | "outputs": [],
455 | "source": [
456 | "import plotly.graph_objects as go\n",
457 | "from plotly.subplots import make_subplots\n",
458 | "\n",
459 | "fig = make_subplots(2,2)\n",
460 | "fig.add_trace(go.Histogram2d(\n",
461 | " x = [ 1, 2, 2, 3, 4 ],\n",
462 | " y = [ 1, 2, 2, 3, 4 ],\n",
463 | " coloraxis = \"coloraxis\",\n",
464 | " xbins = {'start':1, 'size':1}), 1,1)\n",
465 | "fig.add_trace(go.Histogram2d(\n",
466 | " x = [ 4, 5, 5, 5, 6 ],\n",
467 | " y = [ 4, 5, 5, 5, 6 ],\n",
468 | " coloraxis = \"coloraxis\",\n",
469 | " ybins = {'start': 3, 'size': 1}),1,2)\n",
470 | "fig.add_trace(go.Histogram2d(\n",
471 | " x = [ 1, 2, 2, 3, 4 ],\n",
472 | " y = [ 1, 2, 2, 3, 4 ],\n",
473 | " bingroup = 1,\n",
474 | " coloraxis = \"coloraxis\",\n",
475 | " xbins = {'start':1, 'size':1}), 2,1)\n",
476 | "fig.add_trace(go.Histogram2d(\n",
477 | " x = [ 4, 5, 5, 5, 6 ],\n",
478 | " y = [ 4, 5, 5, 5, 6 ],\n",
479 | " bingroup = 1,\n",
480 | " coloraxis = \"coloraxis\",\n",
481 | " ybins = {'start': 3, 'size': 1}),2,2)\n",
482 | "fig.show()"
483 | ]
484 | },
485 | {
486 | "cell_type": "code",
487 | "execution_count": null,
488 | "metadata": {},
489 | "outputs": [],
490 | "source": []
491 | }
492 | ],
493 | "metadata": {
494 | "kernelspec": {
495 | "display_name": "Python 3",
496 | "language": "python",
497 | "name": "python3"
498 | },
499 | "language_info": {
500 | "codemirror_mode": {
501 | "name": "ipython",
502 | "version": 3
503 | },
504 | "file_extension": ".py",
505 | "mimetype": "text/x-python",
506 | "name": "python",
507 | "nbconvert_exporter": "python",
508 | "pygments_lexer": "ipython3",
509 | "version": "3.7.6"
510 | }
511 | },
512 | "nbformat": 4,
513 | "nbformat_minor": 2
514 | }
515 |
--------------------------------------------------------------------------------
/notebooks/12- Distribution Plots .ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Distribution Plots \n",
8 | "* It is important to look at the data (i.e. visualize it) before summarizing it with descriptive statistics or making inferences about parameters\n",
9 | "* It is not easy to see any patterns by looking at a large list of numbers \n",
10 | "* Also, a single descriptive statistic in isolation can be misleading and give the wrong impression of the data. "
11 | ]
12 | },
13 | {
14 | "cell_type": "code",
15 | "execution_count": null,
16 | "metadata": {},
17 | "outputs": [],
18 | "source": []
19 | },
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {},
23 | "source": [
24 | "## Combined Statistical Representations with Plotly Express"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "execution_count": null,
30 | "metadata": {},
31 | "outputs": [],
32 | "source": [
33 | "import plotly.express as px\n",
34 | "df = px.data.tips()\n",
35 | "# a rug plot draws a small vertical tick at each observation\n",
36 | "fig = px.histogram(df, x=\"total_bill\", y=\"tip\", color=\"sex\", marginal=\"rug\",\n",
37 | " hover_data=df.columns)\n",
38 | "fig.show()"
39 | ]
40 | },
41 | {
42 | "cell_type": "code",
43 | "execution_count": null,
44 | "metadata": {},
45 | "outputs": [],
46 | "source": [
47 | "import plotly.express as px\n",
48 | "df = px.data.tips()\n",
49 | "fig = px.histogram(df, x=\"total_bill\", y=\"tip\", color=\"sex\",\n",
50 | " marginal=\"violin\", # or violin, rug\n",
51 | " hover_data=df.columns)\n",
52 | "fig.show()"
53 | ]
54 | },
55 | {
56 | "cell_type": "markdown",
57 | "metadata": {},
58 | "source": [
59 | "## Using Figure Factory\n",
60 | "The distplot figure factory displays a combination of statistical representations of numerical data, such as histogram, kernel density estimation or normal curve, and rug plot."
61 | ]
62 | },
63 | {
64 | "cell_type": "markdown",
65 | "metadata": {},
66 | "source": [
67 | "## Basic Distplot\n",
68 | "A histogram, a kde plot and a rug plot are displayed."
69 | ]
70 | },
71 | {
72 | "cell_type": "code",
73 | "execution_count": null,
74 | "metadata": {},
75 | "outputs": [],
76 | "source": [
77 | "import plotly.figure_factory as ff\n",
78 | "import numpy as np\n",
79 | "np.random.seed(1)\n",
80 | "\n",
81 | "x = np.random.randn(1000)\n",
82 | "y = np.random.randn(1000)\n",
83 | "hist_data = [x,y]\n",
84 | "group_labels = ['distplot x', 'distplot y'] # name of the dataset\n",
85 | "\n",
86 | "fig = ff.create_distplot(hist_data, group_labels)\n",
87 | "fig.show()"
88 | ]
89 | },
90 | {
91 | "cell_type": "markdown",
92 | "metadata": {},
93 | "source": [
94 | "## Plot Multiple Datasets"
95 | ]
96 | },
97 | {
98 | "cell_type": "code",
99 | "execution_count": null,
100 | "metadata": {},
101 | "outputs": [],
102 | "source": [
103 | "import plotly.figure_factory as ff\n",
104 | "import numpy as np\n",
105 | "\n",
106 | "# Add histogram data\n",
107 | "x1 = np.random.randn(200) - 2\n",
108 | "x2 = np.random.randn(200)\n",
109 | "x3 = np.random.randn(200) + 2\n",
110 | "x4 = np.random.randn(200) + 4\n",
111 | "\n",
112 | "# Group data together\n",
113 | "hist_data = [x1, x2, x3, x4]\n",
114 | "\n",
115 | "group_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4']\n",
116 | "\n",
117 | "# Create distplot with custom bin_size\n",
118 | "fig = ff.create_distplot(hist_data, group_labels, bin_size=.2,curve_type='kde')\n",
119 | "fig.show()"
120 | ]
121 | },
122 | {
123 | "cell_type": "markdown",
124 | "metadata": {},
125 | "source": [
126 | "## Use Multiple Bin Sizes\n",
127 | "Different bin sizes are used for the different datasets with the bin_size argument."
128 | ]
129 | },
130 | {
131 | "cell_type": "code",
132 | "execution_count": null,
133 | "metadata": {},
134 | "outputs": [],
135 | "source": [
136 | "import plotly.figure_factory as ff\n",
137 | "import numpy as np\n",
138 | "\n",
139 | "# Add histogram data\n",
140 | "x1 = np.random.randn(200)-2\n",
141 | "x2 = np.random.randn(200)\n",
142 | "x3 = np.random.randn(200)+2\n",
143 | "x4 = np.random.randn(200)+4\n",
144 | "\n",
145 | "# Group data together\n",
146 | "hist_data = [x1, x2, x3, x4]\n",
147 | "\n",
148 | "group_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4']\n",
149 | "\n",
150 | "# Create distplot with custom bin_size\n",
151 | "fig = ff.create_distplot(hist_data, group_labels, bin_size=[.1, .25, .5, 1])\n",
152 | "fig.show()"
153 | ]
154 | },
155 | {
156 | "cell_type": "markdown",
157 | "metadata": {},
158 | "source": [
159 | "## Customize Rug Text, Colors & Title"
160 | ]
161 | },
162 | {
163 | "cell_type": "code",
164 | "execution_count": null,
165 | "metadata": {},
166 | "outputs": [],
167 | "source": [
168 | "import plotly.figure_factory as ff\n",
169 | "import numpy as np\n",
170 | "\n",
171 | "x1 = np.random.randn(26)\n",
172 | "x2 = np.random.randn(26) + .5\n",
173 | "\n",
174 | "group_labels = ['2014', '2015']\n",
175 | "\n",
176 | "rug_text_one = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',\n",
177 | " 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',\n",
178 | " 'u', 'v', 'w', 'x', 'y', 'z']\n",
179 | "\n",
180 | "rug_text_two = ['aa', 'bb', 'cc', 'dd', 'ee', 'ff', 'gg', 'hh', 'ii', 'jj',\n",
181 | " 'kk', 'll', 'mm', 'nn', 'oo', 'pp', 'qq', 'rr', 'ss', 'tt',\n",
182 | " 'uu', 'vv', 'ww', 'xx', 'yy', 'zz']\n",
183 | "\n",
184 | "rug_text = [rug_text_one, rug_text_two] # for hover in rug plot\n",
185 | "colors = ['rgb(0, 0, 100)', 'rgb(0, 200, 200)']\n",
186 | "\n",
187 | "# Create distplot with custom bin_size\n",
188 | "fig = ff.create_distplot(\n",
189 | " [x1, x2], group_labels, bin_size=.2,\n",
190 | " rug_text=rug_text, colors=colors)\n",
191 | "\n",
192 | "fig.update_layout(title_text='Customized Distplot')\n",
193 | "fig.show()"
194 | ]
195 | },
196 | {
197 | "cell_type": "markdown",
198 | "metadata": {},
199 | "source": [
200 | "## Plot Normal Curve"
201 | ]
202 | },
203 | {
204 | "cell_type": "code",
205 | "execution_count": null,
206 | "metadata": {},
207 | "outputs": [],
208 | "source": [
209 | "import plotly.figure_factory as ff\n",
210 | "import numpy as np\n",
211 | "\n",
212 | "x1 = np.random.randn(200)\n",
213 | "x2 = np.random.randn(200) + 2\n",
214 | "\n",
215 | "group_labels = ['Group 1', 'Group 2']\n",
216 | "\n",
217 | "colors = ['slategray', 'magenta']\n",
218 | "\n",
219 | "# Create distplot with curve_type set to 'normal'\n",
220 | "fig = ff.create_distplot([x1, x2], group_labels, bin_size=.5,\n",
221 | " curve_type='normal', # override default 'kde'\n",
222 | " colors=colors)\n",
223 | "\n",
224 | "# Add title\n",
225 | "fig.update_layout(title_text='Distplot with Normal Distribution')\n",
226 | "fig.show()"
227 | ]
228 | },
229 | {
230 | "cell_type": "markdown",
231 | "metadata": {},
232 | "source": [
233 | "## Choose What to Plot .. any combination of Curve, Rug and Hist"
234 | ]
235 | },
236 | {
237 | "cell_type": "code",
238 | "execution_count": null,
239 | "metadata": {},
240 | "outputs": [],
241 | "source": [
242 | "import plotly.figure_factory as ff\n",
243 | "import numpy as np\n",
244 | "\n",
245 | "x1 = np.random.randn(200) - 1\n",
246 | "x2 = np.random.randn(200)\n",
247 | "x3 = np.random.randn(200) + 1\n",
248 | "\n",
249 | "hist_data = [x1, x2, x3]\n",
250 | "\n",
251 | "group_labels = ['Group 1', 'Group 2', 'Group 3']\n",
252 | "colors = ['#333F44', '#37AA9C', '#94F3E4']\n",
253 | "\n",
254 | "# Create distplot with curve_type set to 'normal'\n",
255 | "\n",
256 | "fig = ff.create_distplot(hist_data, group_labels, colors=colors, bin_size=.25,\n",
257 | " show_curve=True, show_hist=True, show_rug=True)\n",
258 | "\n",
259 | "# Add title\n",
260 | "fig.update_layout(title_text='Curve and Rug Plot')\n",
261 | "fig.show()"
262 | ]
263 | },
264 | {
265 | "cell_type": "markdown",
266 | "metadata": {},
267 | "source": [
268 | "## Plot Hist and Rug with Different Bin Sizes"
269 | ]
270 | },
271 | {
272 | "cell_type": "code",
273 | "execution_count": null,
274 | "metadata": {},
275 | "outputs": [],
276 | "source": [
277 | "import plotly.figure_factory as ff\n",
278 | "import numpy as np\n",
279 | "\n",
280 | "x1 = np.random.randn(200) - 2\n",
281 | "x2 = np.random.randn(200)\n",
282 | "x3 = np.random.randn(200) + 2\n",
283 | "\n",
284 | "hist_data = [x1, x2, x3]\n",
285 | "\n",
286 | "group_labels = ['Group 1', 'Group 2', 'Group 3']\n",
287 | "colors = ['#393E46', '#2BCDC1', '#F66095']\n",
288 | "\n",
289 | "fig = ff.create_distplot(hist_data, group_labels, colors=colors,\n",
290 | " bin_size=[0.3, 0.2, 0.1], show_curve=True)\n",
291 | "\n",
292 | "# Add title\n",
293 | "fig.update(layout_title_text='Hist and Rug Plot')\n",
294 | "fig.show()"
295 | ]
296 | },
297 | {
298 | "cell_type": "markdown",
299 | "metadata": {},
300 | "source": [
301 | "## Distplot with Pandas"
302 | ]
303 | },
304 | {
305 | "cell_type": "code",
306 | "execution_count": null,
307 | "metadata": {},
308 | "outputs": [],
309 | "source": [
310 | "import plotly.figure_factory as ff\n",
311 | "import numpy as np\n",
312 | "import pandas as pd\n",
313 | "\n",
314 | "df = pd.DataFrame({'2012': np.random.randn(200),\n",
315 | " '2013': np.random.randn(200)+1})\n",
316 | "fig = ff.create_distplot([df[c] for c in df.columns], df.columns, bin_size=.25)\n",
317 | "fig.show()"
318 | ]
319 | },
320 | {
321 | "cell_type": "code",
322 | "execution_count": null,
323 | "metadata": {},
324 | "outputs": [],
325 | "source": [
326 | "df"
327 | ]
328 | },
329 | {
330 | "cell_type": "code",
331 | "execution_count": null,
332 | "metadata": {},
333 | "outputs": [],
334 | "source": []
335 | }
336 | ],
337 | "metadata": {
338 | "kernelspec": {
339 | "display_name": "Python 3",
340 | "language": "python",
341 | "name": "python3"
342 | },
343 | "language_info": {
344 | "codemirror_mode": {
345 | "name": "ipython",
346 | "version": 3
347 | },
348 | "file_extension": ".py",
349 | "mimetype": "text/x-python",
350 | "name": "python",
351 | "nbconvert_exporter": "python",
352 | "pygments_lexer": "ipython3",
353 | "version": "3.7.6"
354 | }
355 | },
356 | "nbformat": 4,
357 | "nbformat_minor": 2
358 | }
359 |
--------------------------------------------------------------------------------
/notebooks/13- Violin Plots .ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Violin Plot\n",
8 | "* A violin plot is a method of plotting numeric data.\n",
9 | "* It is similar to a box plot, with the addition of a rotated kernel density plot on each side.\n",
10 | "* Violin plots are similar to box plots, except that they also show the probability density of the data at different values, usually smoothed by a kernel density estimator.\n",
11 | "* Typically a violin plot will include all the data that is in a box plot: a marker for the median of the data; a box or marker indicating the interquartile range; and possibly all sample points, if the number of samples is not too high.\n",
12 | "* A violin plot is more informative than a plain box plot. \n",
13 | "* While a box plot only shows summary statistics such as mean/median and interquartile ranges, the violin plot shows the full distribution of the data. \n",
14 | "* The difference is particularly useful when the data distribution is multimodal (more than one peak). In this case a violin plot shows the presence of different peaks, their position and relative amplitude. \n",
15 | "[Violin Plot on wikipedia](https://en.wikipedia.org/wiki/Violin_plot)\n",
16 | "\n",
17 | ""
18 | ]
19 | },
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {},
23 | "source": [
24 | "## Basic Violin Plot with Plotly Express"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "execution_count": null,
30 | "metadata": {},
31 | "outputs": [],
32 | "source": [
33 | "import plotly.express as px\n",
34 | "\n",
35 | "df = px.data.tips()\n",
36 | "fig = px.violin(df, y=\"total_bill\")\n",
37 | "fig.show()"
38 | ]
39 | },
40 | {
41 | "cell_type": "markdown",
42 | "metadata": {},
43 | "source": [
44 | "## Violin plot with box and data points"
45 | ]
46 | },
47 | {
48 | "cell_type": "code",
49 | "execution_count": null,
50 | "metadata": {},
51 | "outputs": [],
52 | "source": [
53 | "import plotly.express as px\n",
54 | "\n",
55 | "df = px.data.tips()\n",
56 | "fig = px.violin(df, y=\"total_bill\", box=True, # draw box plot inside the violin\n",
57 | " points='outliers', # can be 'outliers', or False\n",
58 | " )\n",
59 | "fig.show()"
60 | ]
61 | },
62 | {
63 | "cell_type": "markdown",
64 | "metadata": {},
65 | "source": [
66 | "## Multiple Violin Plots"
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "execution_count": null,
72 | "metadata": {},
73 | "outputs": [],
74 | "source": [
75 | "import plotly.express as px\n",
76 | "\n",
77 | "df = px.data.tips()\n",
78 | "fig = px.violin(df, y=\"tip\", x=\"smoker\", color=\"sex\", box=True, points=\"all\",\n",
79 | " hover_data=df.columns)\n",
80 | "fig.show()"
81 | ]
82 | },
83 | {
84 | "cell_type": "code",
85 | "execution_count": null,
86 | "metadata": {},
87 | "outputs": [],
88 | "source": [
89 | "import plotly.express as px\n",
90 | "\n",
91 | "df = px.data.tips()\n",
92 | "fig = px.violin(df, y=\"tip\", color=\"sex\",\n",
93 | " violinmode='overlay', # draw violins on top of each other\n",
94 | " # default violinmode is 'group' as in example above\n",
95 | " hover_data=df.columns)\n",
96 | "fig.show()"
97 | ]
98 | },
99 | {
100 | "cell_type": "markdown",
101 | "metadata": {},
102 | "source": [
103 | "## Violin Plot Graphic Object"
104 | ]
105 | },
106 | {
107 | "cell_type": "code",
108 | "execution_count": null,
109 | "metadata": {},
110 | "outputs": [],
111 | "source": [
112 | "import plotly.graph_objects as go\n",
113 | "\n",
114 | "import pandas as pd\n",
115 | "\n",
116 | "df = pd.read_csv(\"https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv\")\n",
117 | "\n",
118 | "fig = go.Figure(data=go.Violin(y=df['total_bill'], box_visible=True, line_color='black',\n",
119 | " meanline_visible=True, fillcolor='lightseagreen', opacity=0.6,\n",
120 | " x0='Total Bill'))\n",
121 | "\n",
122 | "fig.update_layout(yaxis_zeroline=False)\n",
123 | "fig.show()"
124 | ]
125 | },
126 | {
127 | "cell_type": "markdown",
128 | "metadata": {},
129 | "source": [
130 | "## Multiple Traces"
131 | ]
132 | },
133 | {
134 | "cell_type": "code",
135 | "execution_count": null,
136 | "metadata": {},
137 | "outputs": [],
138 | "source": [
139 | "import plotly.graph_objects as go\n",
140 | "\n",
141 | "import pandas as pd\n",
142 | "\n",
143 | "df = pd.read_csv(\"https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv\")\n",
144 | "\n",
145 | "fig = go.Figure()\n",
146 | "\n",
147 | "days = ['Thur', 'Fri', 'Sat', 'Sun']\n",
148 | "\n",
149 | "for day in days:\n",
150 | " fig.add_trace(go.Violin(x=df['day'][df['day'] == day],\n",
151 | " y=df['total_bill'][df['day'] == day],\n",
152 | " name=day,\n",
153 | " box_visible=True,\n",
154 | " meanline_visible=True))\n",
155 | "\n",
156 | "fig.show()"
157 | ]
158 | },
159 | {
160 | "cell_type": "code",
161 | "execution_count": null,
162 | "metadata": {},
163 | "outputs": [],
164 | "source": [
165 | "#df = pd.read_csv(\"https://github.com/nsadawi/SampleCSVDatasets/blob/master/CSVs/nyc_crime.csv?raw=true\")\n",
166 | "#df.shape"
167 | ]
168 | },
169 | {
170 | "cell_type": "markdown",
171 | "metadata": {},
172 | "source": [
173 | "## Grouped Violin Plot"
174 | ]
175 | },
176 | {
177 | "cell_type": "code",
178 | "execution_count": null,
179 | "metadata": {},
180 | "outputs": [],
181 | "source": [
182 | "import plotly.graph_objects as go\n",
183 | "\n",
184 | "import pandas as pd\n",
185 | "\n",
186 | "df = pd.read_csv(\"https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv\")\n",
187 | "\n",
188 | "fig = go.Figure()\n",
189 | "\n",
190 | "fig.add_trace(go.Violin(x=df['day'][ df['sex'] == 'Male' ],\n",
191 | " y=df['total_bill'][ df['sex'] == 'Male' ],\n",
192 | " legendgroup='M', scalegroup='M', name='M',\n",
193 | " line_color='blue')\n",
194 | " )\n",
195 | "fig.add_trace(go.Violin(x=df['day'][ df['sex'] == 'Female' ],\n",
196 | " y=df['total_bill'][ df['sex'] == 'Female' ],\n",
197 | " legendgroup='F', scalegroup='F', name='F',\n",
198 | " line_color='orange')\n",
199 | " )\n",
200 | "\n",
201 | "fig.update_traces(box_visible=True, meanline_visible=True)\n",
202 | "fig.update_layout(violinmode='group')\n",
203 | "fig.show()"
204 | ]
205 | },
206 | {
207 | "cell_type": "markdown",
208 | "metadata": {},
209 | "source": [
210 | "## Split Violin Plot"
211 | ]
212 | },
213 | {
214 | "cell_type": "code",
215 | "execution_count": null,
216 | "metadata": {},
217 | "outputs": [],
218 | "source": [
219 | "import plotly.graph_objects as go\n",
220 | "\n",
221 | "import pandas as pd\n",
222 | "\n",
223 | "df = pd.read_csv(\"https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv\")\n",
224 | "\n",
225 | "fig = go.Figure()\n",
226 | "\n",
227 | "fig.add_trace(go.Violin(x=df['day'][ df['smoker'] == 'Yes' ],\n",
228 | " y=df['total_bill'][ df['smoker'] == 'Yes' ],\n",
229 | " legendgroup='Yes', scalegroup='Yes', name='Yes',\n",
230 | " side='negative',\n",
231 | " line_color='blue')\n",
232 | " )\n",
233 | "fig.add_trace(go.Violin(x=df['day'][ df['smoker'] == 'No' ],\n",
234 | " y=df['total_bill'][ df['smoker'] == 'No' ],\n",
235 | " legendgroup='No', scalegroup='No', name='No',\n",
236 | " side='positive',\n",
237 | " line_color='orange')\n",
238 | " )\n",
239 | "fig.update_traces(meanline_visible=True)\n",
240 | "fig.update_layout(violingap=0, violinmode='overlay')\n",
241 | "fig.show()"
242 | ]
243 | },
244 | {
245 | "cell_type": "markdown",
246 | "metadata": {},
247 | "source": [
248 | "## Ridgeline plot\n",
249 | "* A ridgeline plot (previously known as [Joy Plot](https://serialmentor.com/blog/2017/9/15/goodbye-joyplots)) shows the distribution of a numerical value for several groups. \n",
250 | "* They can be used for visualizing changes in distributions over time or space."
251 | ]
252 | },
253 | {
254 | "cell_type": "code",
255 | "execution_count": null,
256 | "metadata": {},
257 | "outputs": [],
258 | "source": [
259 | "import plotly.graph_objects as go\n",
260 | "from plotly.colors import n_colors\n",
261 | "import numpy as np\n",
262 | "np.random.seed(1)\n",
263 | "\n",
264 | "# 12 sets of normal distributed random data, with increasing mean and standard deviation\n",
265 | "data = (np.linspace(1, 2, 12)[:, np.newaxis] * np.random.randn(12, 200) +\n",
266 | " (np.arange(12) + 2 * np.random.random(12))[:, np.newaxis])\n",
267 | "\n",
268 | "# Generate colors within this range\n",
269 | "colors = n_colors('rgb(5, 200, 200)', 'rgb(200, 10, 10)', 12, colortype='rgb')\n",
270 | "\n",
271 | "fig = go.Figure()\n",
272 | "for data_line, color in zip(data, colors):\n",
273 | " fig.add_trace(go.Violin(x=data_line, line_color=color))\n",
274 | "\n",
275 | "fig.update_traces(orientation='h', side='positive', width=3, points=False)\n",
276 | "#fig.update_layout(xaxis_showgrid=False, xaxis_zeroline=False)\n",
277 | "fig.show()"
278 | ]
279 | },
280 | {
281 | "cell_type": "code",
282 | "execution_count": null,
283 | "metadata": {},
284 | "outputs": [],
285 | "source": [
286 | "data.shape"
287 | ]
288 | },
289 | {
290 | "cell_type": "code",
291 | "execution_count": null,
292 | "metadata": {},
293 | "outputs": [],
294 | "source": [
295 | "colors"
296 | ]
297 | },
298 | {
299 | "cell_type": "code",
300 | "execution_count": null,
301 | "metadata": {},
302 | "outputs": [],
303 | "source": []
304 | }
305 | ],
306 | "metadata": {
307 | "kernelspec": {
308 | "display_name": "Python 3",
309 | "language": "python",
310 | "name": "python3"
311 | },
312 | "language_info": {
313 | "codemirror_mode": {
314 | "name": "ipython",
315 | "version": 3
316 | },
317 | "file_extension": ".py",
318 | "mimetype": "text/x-python",
319 | "name": "python",
320 | "nbconvert_exporter": "python",
321 | "pygments_lexer": "ipython3",
322 | "version": "3.7.6"
323 | }
324 | },
325 | "nbformat": 4,
326 | "nbformat_minor": 2
327 | }
328 |
--------------------------------------------------------------------------------
/notebooks/14- Contour Plot.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Contour Plot and 2D Histogram Contour\n",
8 | "* A contour plot is a graphical technique for representing a 3-dimensional surface by plotting constant z slices, called contours, on a 2-dimensional format. \n",
9 | "* That is, given a value for z, lines are drawn for connecting the (x,y) coordinates where that z value occurs.\n",
10 | "* The contour plot is an alternative to a 3-D surface plot. \n",
11 | "\n",
12 | "[Contour Plot on NIST](https://www.itl.nist.gov/div898/handbook/eda/section3/contour.htm)\n",
13 | "\n",
14 | "\n",
15 | "\n",
16 | "The contour plot is formed by:\n",
17 | "* Vertical axis: Independent variable 2\n",
18 | "* Horizontal axis: Independent variable 1\n",
19 | "* Lines: iso-response values \n",
20 | "\n",
21 | "The independent variables are usually restricted to a regular grid. The actual techniques for determining the correct iso-response values are rather complex and are almost always computer generated.\n",
22 | "\n",
23 | "An additional variable may be required to specify the Z values for drawing the iso-lines. Some software packages require explicit values. Other software packages will determine them automatically.\n",
24 | "\n",
25 | "If the data (or function) do not form a regular grid, you typically need to perform a 2-D interpolation to form a regular grid. "
26 | ]
27 | },
28 | {
29 | "cell_type": "markdown",
30 | "metadata": {},
31 | "source": [
32 | "## Basic 2D Histogram Contour\n",
33 | "Notice this is a 2D Histogram Contour .. i.e. it counts how many values in each bin"
34 | ]
35 | },
36 | {
37 | "cell_type": "code",
38 | "execution_count": null,
39 | "metadata": {},
40 | "outputs": [],
41 | "source": [
42 | "import plotly.graph_objects as go\n",
43 | "\n",
44 | "import numpy as np\n",
45 | "np.random.seed(1)\n",
46 | "\n",
47 | "x = np.random.uniform(-1, 1, size=500)\n",
48 | "y = np.random.uniform(-1, 1, size=500)\n",
49 | "\n",
50 | "fig = go.Figure(go.Histogram2dContour(\n",
51 | " x = x,\n",
52 | " y = y\n",
53 | "))\n",
54 | "\n",
55 | "fig.show()"
56 | ]
57 | },
58 | {
59 | "cell_type": "markdown",
60 | "metadata": {},
61 | "source": [
62 | "## 2D Histogram Contour Colorscale"
63 | ]
64 | },
65 | {
66 | "cell_type": "code",
67 | "execution_count": null,
68 | "metadata": {},
69 | "outputs": [],
70 | "source": [
71 | "import plotly.graph_objects as go\n",
72 | "\n",
73 | "import numpy as np\n",
74 | "\n",
75 | "x = np.random.uniform(-1, 1, size=500)\n",
76 | "y = np.random.uniform(-1, 1, size=500)\n",
77 | "\n",
78 | "fig = go.Figure(go.Histogram2dContour(\n",
79 | " x = x,\n",
80 | " y = y,\n",
81 | " colorscale = 'rainbow'\n",
82 | "))\n",
83 | "\n",
84 | "fig.show()"
85 | ]
86 | },
87 | {
88 | "cell_type": "markdown",
89 | "metadata": {},
90 | "source": [
91 | "## 2D Histogram Contour Styled"
92 | ]
93 | },
94 | {
95 | "cell_type": "code",
96 | "execution_count": null,
97 | "metadata": {},
98 | "outputs": [],
99 | "source": [
100 | "import plotly.graph_objects as go\n",
101 | "\n",
102 | "import numpy as np\n",
103 | "\n",
104 | "x = np.random.uniform(-1, 1, size=500)\n",
105 | "y = np.random.uniform(-1, 1, size=500)\n",
106 | "z = np.random.uniform(-10, 10, size=500)\n",
107 | "\n",
108 | "\n",
109 | "fig = go.Figure(go.Histogram2dContour(\n",
110 | " x = x,\n",
111 | " y = y,\n",
112 | " z = z,\n",
113 | "\n",
114 | " colorscale = 'Jet',\n",
115 | " contours = dict(\n",
116 | " showlabels = False,\n",
117 | " labelfont = dict(\n",
118 | " family = 'Raleway',\n",
119 | " color = 'white'\n",
120 | " )\n",
121 | " ),\n",
122 | " hoverlabel = dict(\n",
123 | " bgcolor = 'white',\n",
124 | " bordercolor = 'black',\n",
125 | " font = dict(\n",
126 | " family = 'Raleway',\n",
127 | " color = 'black'\n",
128 | " )\n",
129 | " )\n",
130 | "\n",
131 | "))\n",
132 | "\n",
133 | "fig.show()"
134 | ]
135 | },
136 | {
137 | "cell_type": "markdown",
138 | "metadata": {},
139 | "source": [
140 | "## 2D Histogram Contour Subplot"
141 | ]
142 | },
143 | {
144 | "cell_type": "code",
145 | "execution_count": null,
146 | "metadata": {},
147 | "outputs": [],
148 | "source": [
149 | "import plotly.graph_objects as go\n",
150 | "\n",
151 | "import numpy as np\n",
152 | "\n",
153 | "t = np.linspace(-1, 1.2, 2000)\n",
154 | "x = (t**3) + (0.3 * np.random.randn(2000))\n",
155 | "y = (t**6) + (0.3 * np.random.randn(2000))\n",
156 | "\n",
157 | "fig = go.Figure()\n",
158 | "fig.add_trace(go.Histogram2dContour(\n",
159 | " x = x,\n",
160 | " y = y,\n",
161 | " colorscale = 'Blues',\n",
162 | " reversescale = True,\n",
163 | " xaxis = 'x',\n",
164 | " yaxis = 'y'\n",
165 | " ))\n",
166 | "fig.add_trace(go.Scatter(\n",
167 | " x = x,\n",
168 | " y = y,\n",
169 | " xaxis = 'x',\n",
170 | " yaxis = 'y',\n",
171 | " mode = 'markers',\n",
172 | " marker = dict(\n",
173 | " color = 'rgba(0,0,0,0.3)',\n",
174 | " size = 3\n",
175 | " )\n",
176 | " ))\n",
177 | "fig.add_trace(go.Histogram(\n",
178 | " y = y,\n",
179 | " xaxis = 'x2',\n",
180 | " marker = dict(\n",
181 | " color = 'rgba(0,0,0,1)'\n",
182 | " )\n",
183 | " ))\n",
184 | "fig.add_trace(go.Histogram(\n",
185 | " x = x,\n",
186 | " yaxis = 'y2',\n",
187 | " marker = dict(\n",
188 | " color = 'rgba(0,0,0,1)'\n",
189 | " )\n",
190 | " ))\n",
191 | "\n",
192 | "fig.update_layout(\n",
193 | " autosize = False,\n",
194 | " xaxis = dict(\n",
195 | " zeroline = False,\n",
196 | " domain = [0,0.85],\n",
197 | " showgrid = False\n",
198 | " ),\n",
199 | " yaxis = dict(\n",
200 | " zeroline = False,\n",
201 | " domain = [0,0.85],\n",
202 | " showgrid = False\n",
203 | " ),\n",
204 | " xaxis2 = dict(\n",
205 | " zeroline = False,\n",
206 | " domain = [0.85,1],\n",
207 | " showgrid = False\n",
208 | " ),\n",
209 | " yaxis2 = dict(\n",
210 | " zeroline = False,\n",
211 | " domain = [0.85,1],\n",
212 | " showgrid = False\n",
213 | " ),\n",
214 | " height = 600,\n",
215 | " width = 600,\n",
216 | " bargap = 0,\n",
217 | " hovermode = 'closest',\n",
218 | " showlegend = False\n",
219 | ")\n",
220 | "\n",
221 | "fig.show()"
222 | ]
223 | },
224 | {
225 | "cell_type": "markdown",
226 | "metadata": {},
227 | "source": [
228 | "## Basic Contour Plot\n",
229 | "* A 2D contour plot shows the contour lines of a 2D numerical array *z*, i.e. interpolated lines of isovalues of *z*.\n",
230 | "### Contour Line:\n",
231 | "* A contour line (also isoline, isopleth, or isarithm) of a function of two variables is a curve along which the function has a constant value, so that the curve joins points of equal value.\n",
232 | "* It is a plane section of the three-dimensional graph of the function f(x, y) parallel to the (x, y)-plane.\n",
233 | "* In cartography, a contour line (often just called a \"contour\") joins points of equal elevation (height) above a given level, such as mean sea level.\n",
234 | "* A contour map is a map illustrated with contour lines, for example a topographic map, which thus shows valleys and hills, and the steepness or gentleness of slopes.\n",
235 | "* The contour interval of a contour map is the difference in elevation between successive contour lines.\n",
236 | "[Contour Line on Wikipedia](https://en.wikipedia.org/wiki/Contour_line)\n"
237 | ]
238 | },
239 | {
240 | "cell_type": "code",
241 | "execution_count": null,
242 | "metadata": {},
243 | "outputs": [],
244 | "source": [
245 | "import plotly.graph_objects as go\n",
246 | "\n",
247 | "fig = go.Figure(data =\n",
248 | " go.Contour(\n",
249 | " # notice z is 2D .. because we have x and y\n",
250 | " z=[[10, 10.625, 12.5, 15.625, 20],\n",
251 | " [5.625, 6.25, 8.125, 11.25, 15.625],\n",
252 | " [2.5, 3.125, 5., 8.125, 12.5],\n",
253 | " [0.625, 1.25, 3.125, 6.25, 10.625],\n",
254 | " [0, 0.625, 2.5, 5.625, 10]]\n",
255 | " ))\n",
256 | "fig.show()"
257 | ]
258 | },
259 | {
260 | "cell_type": "markdown",
261 | "metadata": {},
262 | "source": [
263 | "## Setting X and Y Coordinates in a Contour Plot"
264 | ]
265 | },
266 | {
267 | "cell_type": "code",
268 | "execution_count": null,
269 | "metadata": {},
270 | "outputs": [],
271 | "source": [
272 | "import plotly.graph_objects as go\n",
273 | "\n",
274 | "fig = go.Figure(data =\n",
275 | " go.Contour(\n",
276 | " z=[[10, 10.625, 12.5, 15.625, 20],\n",
277 | " [5.625, 6.25, 8.125, 11.25, 15.625],\n",
278 | " [2.5, 3.125, 5., 8.125, 12.5],\n",
279 | " [0.625, 1.25, 3.125, 6.25, 10.625],\n",
280 | " [0, 0.625, 2.5, 5.625, 10]],\n",
281 | " x=[-9, -6, -5 , -3, -1], # horizontal axis\n",
282 | " y=[0, 1, 4, 5, 7], # vertical axis\n",
283 | " #colorscale='Electric'#Colorscale for Contour Plot\n",
284 | " ))\n",
285 | "fig.show()"
286 | ]
287 | },
288 | {
289 | "cell_type": "markdown",
290 | "metadata": {},
291 | "source": [
292 | "## Contour Line Labels"
293 | ]
294 | },
295 | {
296 | "cell_type": "code",
297 | "execution_count": null,
298 | "metadata": {},
299 | "outputs": [],
300 | "source": [
301 | "import plotly.graph_objects as go\n",
302 | "\n",
303 | "fig = go.Figure(data=\n",
304 | " go.Contour(\n",
305 | " z=[[10, 10.625, 12.5, 15.625, 20],\n",
306 | " [5.625, 6.25, 8.125, 11.25, 15.625],\n",
307 | " [2.5, 3.125, 5., 8.125, 12.5],\n",
308 | " [0.625, 1.25, 3.125, 6.25, 10.625],\n",
309 | " [0, 0.625, 2.5, 5.625, 10]],\n",
310 | " #contours_coloring='lines',\n",
311 | " contours=dict(\n",
312 | " coloring ='heatmap',\n",
313 | " showlabels = True, # show labels on contours\n",
314 | " labelfont = dict( # label font properties\n",
315 | " size = 12,\n",
316 | " color = 'white',\n",
317 | " ) \n",
318 | " ),\n",
319 | " # Customize Colorbar\n",
320 | " #colorbar=dict(\n",
321 | " # title='Color bar title', # title here\n",
322 | " # titleside='right',\n",
323 | " # titlefont=dict(\n",
324 | " # size=14,\n",
325 | " # family='Arial, sans-serif')\n",
326 | " #)\n",
327 | " ))\n",
328 | "\n",
329 | "fig.show()"
330 | ]
331 | },
332 | {
333 | "cell_type": "markdown",
334 | "metadata": {},
335 | "source": [
336 | "## 3D Surface Plot\n",
337 | "* A surface plot shows a functional relationship between a dependent variable (Z), and two independent variables (X and Y). \n",
338 | "* The plot is a companion plot to the contour plot. "
339 | ]
340 | },
341 | {
342 | "cell_type": "code",
343 | "execution_count": null,
344 | "metadata": {},
345 | "outputs": [],
346 | "source": [
347 | "# Topographical 3D Surface Plot\n",
348 | "import plotly.graph_objects as go\n",
349 | "\n",
350 | "import pandas as pd\n",
351 | "\n",
352 | "# Read data from a csv\n",
353 | "z_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/api_docs/mt_bruno_elevation.csv')\n",
354 | "#z_matrix = np.random.rand(30,20)\n",
355 | "\n",
356 | "z = z_data.values\n",
357 | "# to specify x and y data\n",
358 | "#sh_0, sh_1 = z.shape\n",
359 | "#x, y = np.linspace(0, 1, sh_0), np.linspace(0, 1, sh_1)\n",
360 | "\n",
361 | "\n",
362 | "# z is a 2D matrix\n",
363 | "fig = go.Figure(data=[go.Surface(z=z)])\n",
364 | "\n",
365 | "fig.update_layout(title='Mt Bruno Elevation', autosize=False,\n",
366 | " width=500, height=500,\n",
367 | " margin=dict(l=65, r=50, b=65, t=90))\n",
368 | "\n",
369 | "fig.show()"
370 | ]
371 | },
372 | {
373 | "cell_type": "markdown",
374 | "metadata": {},
375 | "source": [
376 | "## Surface Plot With Contours\n",
377 | "It is possible to display contour lines"
378 | ]
379 | },
380 | {
381 | "cell_type": "code",
382 | "execution_count": null,
383 | "metadata": {},
384 | "outputs": [],
385 | "source": [
386 | "import plotly.graph_objects as go\n",
387 | "\n",
388 | "import pandas as pd\n",
389 | "\n",
390 | "# Read data from a csv\n",
391 | "z_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/api_docs/mt_bruno_elevation.csv')\n",
392 | "\n",
393 | "fig = go.Figure(data=[go.Surface(z=z_data.values)])\n",
394 | "fig.update_traces(contours_z=dict(show=True, usecolormap=True,\n",
395 | " highlightcolor=\"limegreen\", project_z=True))\n",
396 | "fig.update_layout(title='Mt Bruno Elevation', autosize=False,\n",
397 | " scene_camera_eye=dict(x=1.87, y=0.88, z=-0.64),\n",
398 | " width=500, height=500,\n",
399 | " margin=dict(l=65, r=50, b=65, t=90)\n",
400 | ")\n",
401 | "\n",
402 | "fig.show()"
403 | ]
404 | },
405 | {
406 | "cell_type": "markdown",
407 | "metadata": {},
408 | "source": [
409 | "## Multiple 3D Surface Plots"
410 | ]
411 | },
412 | {
413 | "cell_type": "code",
414 | "execution_count": null,
415 | "metadata": {},
416 | "outputs": [],
417 | "source": [
418 | "import plotly.graph_objects as go\n",
419 | "\n",
420 | "z1 = np.random.rand(30,20)\n",
421 | "\n",
422 | "z2 = z1 + 1\n",
423 | "z3 = z1 - 1\n",
424 | "\n",
425 | "fig = go.Figure(data=[\n",
426 | " go.Surface(z=z1),\n",
427 | " go.Surface(z=z2, showscale=False, opacity=0.9),\n",
428 | " go.Surface(z=z3, showscale=False, opacity=0.9)\n",
429 | "\n",
430 | "])\n",
431 | "\n",
432 | "fig.show()\n",
433 | "\n"
434 | ]
435 | },
436 | {
437 | "cell_type": "code",
438 | "execution_count": null,
439 | "metadata": {},
440 | "outputs": [],
441 | "source": []
442 | }
443 | ],
444 | "metadata": {
445 | "kernelspec": {
446 | "display_name": "Python 3",
447 | "language": "python",
448 | "name": "python3"
449 | },
450 | "language_info": {
451 | "codemirror_mode": {
452 | "name": "ipython",
453 | "version": 3
454 | },
455 | "file_extension": ".py",
456 | "mimetype": "text/x-python",
457 | "name": "python",
458 | "nbconvert_exporter": "python",
459 | "pygments_lexer": "ipython3",
460 | "version": "3.7.6"
461 | }
462 | },
463 | "nbformat": 4,
464 | "nbformat_minor": 2
465 | }
466 |
--------------------------------------------------------------------------------
/notebooks/15- Table.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Plotly Table\n",
8 | "* Plotly provides a Table object for detailed data viewing (*go.Table*). \n",
9 | "* The data are arranged in a grid of rows and columns.\n",
10 | "* Most styling can be specified for header, columns, rows or individual cells. \n",
11 | "* Table is using a column-major order, ie. the grid is represented as a vector of column vectors."
12 | ]
13 | },
14 | {
15 | "cell_type": "markdown",
16 | "metadata": {},
17 | "source": [
18 | "## Basic Styled Table"
19 | ]
20 | },
21 | {
22 | "cell_type": "code",
23 | "execution_count": null,
24 | "metadata": {},
25 | "outputs": [],
26 | "source": [
27 | "import plotly.graph_objects as go\n",
28 | "\n",
29 | "fig = go.Figure(data=[go.Table(\n",
30 | " header=dict(values=['A Scores', 'B Scores'],\n",
31 | " line_color='darkslategray',\n",
32 | " fill_color='lightskyblue',\n",
33 | " align='left'),\n",
34 | " cells=dict(values=[[100, 90, 80, 90], # 1st column\n",
35 | " [95, 85, 75, 95]], # 2nd column\n",
36 | " line_color='darkslategray',\n",
37 | " fill_color='lightcyan',\n",
38 | " align='left'))\n",
39 | "])\n",
40 | "\n",
41 | "fig.update_layout(width=500, height=300)\n",
42 | "fig.show()"
43 | ]
44 | },
45 | {
46 | "cell_type": "code",
47 | "execution_count": null,
48 | "metadata": {},
49 | "outputs": [],
50 | "source": [
51 | "import plotly.graph_objects as go\n",
52 | "import pandas as pd\n",
53 | "\n",
54 | "df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv')\n",
55 | "text = list(df['State'].apply(str))\n",
56 | "fig = go.Figure(data=[go.Table(\n",
57 | " header=dict(values=list(df.columns),\n",
58 | " fill_color='paleturquoise',\n",
59 | " align='left'),\n",
60 | " cells=dict(values=[df.Rank, df.State, df.Postal, df.Population],\n",
61 | " fill_color='lavender',\n",
62 | " align='left'),\n",
63 | " #text = list(df['State'].apply(str)),\n",
64 | " hoverinfo = 'text'\n",
65 | ")\n",
66 | "])\n",
67 | "\n",
68 | "fig.show()"
69 | ]
70 | },
71 | {
72 | "cell_type": "code",
73 | "execution_count": null,
74 | "metadata": {},
75 | "outputs": [],
76 | "source": [
77 | "df"
78 | ]
79 | },
80 | {
81 | "cell_type": "code",
82 | "execution_count": null,
83 | "metadata": {},
84 | "outputs": [],
85 | "source": []
86 | }
87 | ],
88 | "metadata": {
89 | "kernelspec": {
90 | "display_name": "Python 3",
91 | "language": "python",
92 | "name": "python3"
93 | },
94 | "language_info": {
95 | "codemirror_mode": {
96 | "name": "ipython",
97 | "version": 3
98 | },
99 | "file_extension": ".py",
100 | "mimetype": "text/x-python",
101 | "name": "python",
102 | "nbconvert_exporter": "python",
103 | "pygments_lexer": "ipython3",
104 | "version": "3.7.6"
105 | }
106 | },
107 | "nbformat": 4,
108 | "nbformat_minor": 2
109 | }
110 |
--------------------------------------------------------------------------------
/notebooks/Hover Text.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Hover Text\n",
8 | "https://plot.ly/python/v3/hover-text-and-formatting/\n",
9 | "\n",
10 | "https://plot.ly/python/text-and-annotations/"
11 | ]
12 | },
13 | {
14 | "cell_type": "code",
15 | "execution_count": null,
16 | "metadata": {},
17 | "outputs": [],
18 | "source": [
19 | "import plotly.graph_objs as go\n",
20 | "\n",
21 | "fig = go.Figure()\n",
22 | "\n",
23 | "import pandas as pd\n",
24 | "df = pd.read_csv('https://raw.githubusercontent.com/nsadawi/SampleCSVDatasets/master/CSVs/stock-prices-2017-2019.csv')\n",
25 | "\n",
26 | "fig.add_trace(go.Scatter(\n",
27 | " #x = [1,2,3,4,5],\n",
28 | " #y = [2,1,6,4,4],\n",
29 | " x = df['Date'], y =df['IBM'],\n",
30 | " #text = [\"Text A\", \"Text B\", \"Text C\", \"Text D\", \"Text E\"],\n",
31 | " #text = list(df['Date'].apply(str)),\n",
32 | " #hoverinfo = 'text',\n",
33 | " marker = dict(\n",
34 | " color = 'green'\n",
35 | " ),\n",
36 | " showlegend = False\n",
37 | " ))\n",
38 | "\n",
39 | "fig.show()"
40 | ]
41 | },
42 | {
43 | "cell_type": "code",
44 | "execution_count": null,
45 | "metadata": {},
46 | "outputs": [],
47 | "source": []
48 | }
49 | ],
50 | "metadata": {
51 | "kernelspec": {
52 | "display_name": "Python 3",
53 | "language": "python",
54 | "name": "python3"
55 | },
56 | "language_info": {
57 | "codemirror_mode": {
58 | "name": "ipython",
59 | "version": 3
60 | },
61 | "file_extension": ".py",
62 | "mimetype": "text/x-python",
63 | "name": "python",
64 | "nbconvert_exporter": "python",
65 | "pygments_lexer": "ipython3",
66 | "version": "3.7.6"
67 | }
68 | },
69 | "nbformat": 4,
70 | "nbformat_minor": 2
71 | }
72 |
--------------------------------------------------------------------------------
/notebooks/Static Image Export.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Static Image Export\n",
8 | "https://plot.ly/python/static-image-export/"
9 | ]
10 | },
11 | {
12 | "cell_type": "code",
13 | "execution_count": null,
14 | "metadata": {},
15 | "outputs": [],
16 | "source": [
17 | "#conda install -c plotly plotly-orca"
18 | ]
19 | },
20 | {
21 | "cell_type": "code",
22 | "execution_count": null,
23 | "metadata": {},
24 | "outputs": [],
25 | "source": [
26 | "import plotly.graph_objects as go\n",
27 | "import numpy as np\n",
28 | "np.random.seed(100)\n",
29 | "\n",
30 | "N = 100\n",
31 | "x = np.random.rand(N)\n",
32 | "y = np.random.rand(N)\n",
33 | "colors = np.random.rand(N)\n",
34 | "sz = np.random.rand(N) * 30\n",
35 | "\n",
36 | "fig = go.Figure()\n",
37 | "fig.add_trace(go.Scatter(\n",
38 | " x=x,\n",
39 | " y=y,\n",
40 | " mode=\"markers\",\n",
41 | " marker=go.scatter.Marker(\n",
42 | " size=sz,\n",
43 | " color=colors,\n",
44 | " opacity=0.6,\n",
45 | " colorscale=\"Viridis\"\n",
46 | " )\n",
47 | "))\n",
48 | "\n",
49 | "fig.show()"
50 | ]
51 | },
52 | {
53 | "cell_type": "code",
54 | "execution_count": null,
55 | "metadata": {},
56 | "outputs": [],
57 | "source": [
58 | "import plotly.io as pio\n",
59 | "pio.renderers\n"
60 | ]
61 | },
62 | {
63 | "cell_type": "code",
64 | "execution_count": null,
65 | "metadata": {},
66 | "outputs": [],
67 | "source": [
68 | "#fig.show(renderer='pdf')\n",
69 | "fig.write_image(\"fig1.pdf\")"
70 | ]
71 | },
72 | {
73 | "cell_type": "code",
74 | "execution_count": null,
75 | "metadata": {},
76 | "outputs": [],
77 | "source": [
78 | "fig.write_image(\"fig1.png\")"
79 | ]
80 | },
81 | {
82 | "cell_type": "code",
83 | "execution_count": null,
84 | "metadata": {},
85 | "outputs": [],
86 | "source": [
87 | "fig.write_image(\"fig1.json\")"
88 | ]
89 | },
90 | {
91 | "cell_type": "code",
92 | "execution_count": null,
93 | "metadata": {
94 | "scrolled": false
95 | },
96 | "outputs": [],
97 | "source": [
98 | "#fig.to_json()"
99 | ]
100 | },
101 | {
102 | "cell_type": "code",
103 | "execution_count": null,
104 | "metadata": {},
105 | "outputs": [],
106 | "source": []
107 | }
108 | ],
109 | "metadata": {
110 | "kernelspec": {
111 | "display_name": "Python 3",
112 | "language": "python",
113 | "name": "python3"
114 | },
115 | "language_info": {
116 | "codemirror_mode": {
117 | "name": "ipython",
118 | "version": 3
119 | },
120 | "file_extension": ".py",
121 | "mimetype": "text/x-python",
122 | "name": "python",
123 | "nbconvert_exporter": "python",
124 | "pygments_lexer": "ipython3",
125 | "version": "3.7.6"
126 | }
127 | },
128 | "nbformat": 4,
129 | "nbformat_minor": 2
130 | }
131 |
--------------------------------------------------------------------------------