Skip to content

Commit a6cdc6d

Browse files
various
1 parent fcdaf35 commit a6cdc6d

File tree

5 files changed

+49
-196
lines changed

5 files changed

+49
-196
lines changed

python/choropleth-maps.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ Below we show how to create Choropleth Maps using either `px.choropleth` (one-li
3737
import plotly.express as px
3838

3939
gapminder = px.data.gapminder().query("year==2007")
40-
fig = px.choropleth(gapminder, locations="iso_alpha",
41-
color="lifeExp", # lifeExp is a column of gapminder
40+
fig = px.choropleth(gapminder, locations="iso_alpha",
41+
color="lifeExp", # lifeExp is a column of gapminder
4242
hover_name="country", # column to add to hover information
4343
color_continuous_scale=px.colors.sequential.Plasma)
4444
fig.show()
@@ -54,8 +54,8 @@ import plotly.graph_objects as go
5454
# Load data frame and tidy it.
5555
import pandas as pd
5656
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')
57-
58-
fig = go.Figure(data=go.Choropleth(
57+
58+
fig = go.Figure(data=go.Choropleth(
5959
locations=df['code'], # Spatial coordinates
6060
z = df['total exports'].astype(float), # Data to be color-coded
6161
locationmode = 'USA-states', # set of locations match entries in `locations`
@@ -87,7 +87,7 @@ df['text'] = df['state'] + '<br>' + \
8787
'Fruits ' + df['total fruits'] + ' Veggies ' + df['total veggies'] + '<br>' + \
8888
'Wheat ' + df['wheat'] + ' Corn ' + df['corn']
8989

90-
fig = go.Figure(data=go.Choropleth(
90+
fig = go.Figure(data=go.Choropleth(
9191
locations=df['code'],
9292
z=df['total exports'].astype(float),
9393
locationmode='USA-states',
@@ -118,7 +118,7 @@ import pandas as pd
118118

119119
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv')
120120

121-
fig = go.Figure(data=go.Choropleth(
121+
fig = go.Figure(data=go.Choropleth(
122122
locations = df['CODE'],
123123
z = df['GDP (BILLIONS)'],
124124
text = df['COUNTRY'],
@@ -179,7 +179,7 @@ for i in range(6,10)[::-1]:
179179
color=colors[i-6],
180180
line_width=0)
181181
)
182-
)
182+
)
183183

184184
df_sept = df.query('Month == 9')
185185
fig.data[0].update(text = df_sept['Value'].map('{:.0f}'.format).astype(str)+' '+\
@@ -272,6 +272,7 @@ fig = ff.create_choropleth(
272272
title_text = 'USA by Unemployment %',
273273
legend_title = '% unemployed'
274274
)
275+
fig.layout.template = None
275276
fig.show()
276277
```
277278

@@ -291,4 +292,4 @@ IFrame(src= "https://dash-simple-apps.plotly.host/dash-choroplethplot/code", wid
291292
```
292293

293294
#### Reference
294-
See https://plot.ly/python/reference/#choropleth for more information and chart attribute options!
295+
See https://plot.ly/python/reference/#choropleth for more information and chart attribute options!

python/county-choropleth.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ fips = ['06021', '06023', '06027',
7878
values = range(len(fips))
7979

8080
fig = ff.create_choropleth(fips=fips, values=values)
81+
fig.layout.template = None
8182
fig.show()
8283
```
8384

@@ -119,6 +120,7 @@ fig = ff.create_choropleth(
119120
county_outline={'color': 'rgb(255,255,255)', 'width': 0.5}, round_legend_values=True,
120121
legend_title='Population by County', title='California and Nearby States'
121122
)
123+
fig.layout.template = None
122124
fig.show()
123125
```
124126

@@ -148,6 +150,7 @@ fig = ff.create_choropleth(
148150
county_outline={'color': 'rgb(255,255,255)', 'width': 0.5},
149151
exponent_format=True,
150152
)
153+
fig.layout.template = None
151154
fig.show()
152155
```
153156

@@ -184,6 +187,7 @@ fig.update_layout(
184187
annotations = {'x': -0.12, 'xanchor': 'left'}
185188
)
186189

190+
fig.layout.template = None
187191
fig.show()
188192
```
189193

@@ -230,6 +234,7 @@ fig = ff.create_choropleth(
230234
title='Oregon'
231235
)
232236

237+
fig.layout.template = None
233238
fig.show()
234239
```
235240

@@ -263,6 +268,7 @@ fig = ff.create_choropleth(
263268
legend_title='% unemployed'
264269
)
265270

271+
fig.layout.template = None
266272
fig.show()
267273
```
268274

python/facet-plots.md

Lines changed: 16 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -36,204 +36,36 @@ jupyter:
3636
title: Python Facet and Trellis Plots | plotly
3737
---
3838

39-
#### Facet by Column
40-
A `facet grid` is a generalization of a scatterplot matrix where we can "facet" a row and/or column by another variable. Given some tabular data, stored in a `pandas.DataFrame`, we can plot one variable against another to form a regular scatter plot, _and_ we can pick a third faceting variable to form panels along the rows and/or columns to segment the data even further, forming a bunch of panels. We can also assign a coloring rule or a heatmap based on a color variable to color the plot.
4139

42-
```python
43-
import plotly.figure_factory as ff
44-
45-
import pandas as pd
46-
mpg = pd.read_table('https://raw.githubusercontent.com/plotly/datasets/master/mpg_2017.txt')
47-
48-
fig = ff.create_facet_grid(
49-
mpg,
50-
x='displ',
51-
y='cty',
52-
facet_col='cyl',
53-
)
54-
55-
fig.show()
56-
```
57-
58-
#### Facet by Row
59-
60-
```python
61-
import plotly.figure_factory as ff
62-
63-
import pandas as pd
64-
mpg = pd.read_table('https://raw.githubusercontent.com/plotly/datasets/master/mpg_2017.txt')
65-
66-
fig = ff.create_facet_grid(
67-
mpg,
68-
x='displ',
69-
y='cty',
70-
facet_row='cyl',
71-
marker={'color': 'rgb(86, 7, 100)'},
72-
)
73-
74-
fig.show()
75-
```
76-
77-
#### Facet by Row and Column
78-
79-
```python
80-
import plotly.figure_factory as ff
81-
82-
import pandas as pd
83-
mpg = pd.read_table('https://raw.githubusercontent.com/plotly/datasets/master/mpg_2017.txt')
40+
### Facet and Trellis Plots
8441

85-
fig = ff.create_facet_grid(
86-
mpg,
87-
x='displ',
88-
y='cty',
89-
facet_row='cyl',
90-
facet_col='drv',
91-
marker={'color': 'rgb(234, 239, 155)'},
92-
)
93-
94-
fig.show()
95-
```
42+
Facet plots, also known as trellis plots or small multiples, are figures made up of multiple subplots which have the same set of axes, where each subplot shows a subset of the data. While it is straightforward to use `plotly`'s [subplot capabilities]
43+
(/python/subplots/) to make such figures, it's far easier to use the built-in `facet_row` and `facet_col` arguments in the various [Plotly Express](/python/next/plotly-express/) functions.
9644

97-
#### Custom Colormap
45+
### Scatter Plot Column Facets
9846

9947
```python
100-
import plotly.figure_factory as ff
101-
102-
import pandas as pd
103-
tips = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/tips.csv')
104-
105-
fig = ff.create_facet_grid(
106-
tips,
107-
x='total_bill',
108-
y='tip',
109-
color_name='sex',
110-
show_boxes=False,
111-
marker={'size': 10, 'opacity': 1.0},
112-
colormap={'Male': 'rgb(165, 242, 242)', 'Female': 'rgb(253, 174, 216)'}
113-
)
48+
import plotly.express as px
49+
tips = px.data.tips()
50+
fig = px.scatter(tips, x="total_bill", y="tip", color="smoker", facet_col="sex")
11451
fig.show()
11552
```
11653

117-
#### Label Variable Name:Value
54+
### Bar Chart Row Facets
11855

11956
```python
120-
import plotly.figure_factory as ff
121-
122-
import pandas as pd
123-
mtcars = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/mtcars.csv')
124-
125-
fig = ff.create_facet_grid(
126-
mtcars,
127-
x='mpg',
128-
y='wt',
129-
facet_col='cyl',
130-
facet_col_labels='name',
131-
facet_row_labels='name',
132-
)
57+
import plotly.express as px
58+
tips = px.data.tips()
59+
fig = px.bar(tips, x="size", y="total_bill", color="sex", facet_row="smoker")
13360
fig.show()
13461
```
13562

136-
#### Custom Labels
63+
### Histogram Facet Grids
13764

13865
```python
139-
import plotly.figure_factory as ff
140-
141-
import pandas as pd
142-
mtcars = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/mtcars.csv')
143-
144-
fig = ff.create_facet_grid(
145-
mtcars,
146-
x='wt',
147-
y='mpg',
148-
facet_col='cyl',
149-
facet_col_labels={4: '$2^2 = 4$', 6: '$\\frac{18}{3} = 6$', 8: '$2\cdot4 = 8$'},
150-
marker={'color': 'rgb(240, 100, 2)'},
151-
)
152-
66+
import plotly.express as px
67+
tips = px.data.tips()
68+
fig = px.histogram(tips, x="total_bill", y="tip", color="sex", facet_row="time", facet_col="day",
69+
category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "time": ["Lunch", "Dinner"]})
15370
fig.show()
15471
```
155-
156-
#### Plot in 'ggplot2' style
157-
To learn more about ggplot2, check out http://ggplot2.tidyverse.org/reference/facet_grid.html
158-
159-
```python
160-
import plotly.figure_factory as ff
161-
162-
import pandas as pd
163-
tips = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/tips.csv')
164-
165-
fig = ff.create_facet_grid(
166-
tips,
167-
x='total_bill',
168-
y='tip',
169-
facet_row='sex',
170-
facet_col='smoker',
171-
marker={'symbol': 'circle-open', 'size': 10},
172-
ggplot2=True
173-
)
174-
fig.show()
175-
```
176-
177-
#### Plot with 'scattergl' traces
178-
179-
```python
180-
import plotly.figure_factory as ff
181-
182-
import pandas as pd
183-
mpg = pd.read_table('https://raw.githubusercontent.com/plotly/datasets/master/mpg_2017.txt')
184-
185-
grid = ff.create_facet_grid(
186-
mpg,
187-
x='class',
188-
y='displ',
189-
trace_type='scattergl',
190-
)
191-
192-
fig.show()
193-
```
194-
195-
#### Plot with Histogram Traces
196-
197-
```python
198-
import plotly.figure_factory as ff
199-
200-
import pandas as pd
201-
tips = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/tips.csv')
202-
203-
fig = ff.create_facet_grid(
204-
tips,
205-
x='total_bill',
206-
y='tip',
207-
facet_row='sex',
208-
facet_col='smoker',
209-
trace_type='histogram',
210-
)
211-
212-
fig.show()
213-
```
214-
215-
#### Other Trace Types
216-
Facet Grids support `scatter`, `scattergl`, `histogram`, `bar` and `box` trace types.
217-
218-
```python
219-
import plotly.figure_factory as ff
220-
221-
import pandas as pd
222-
tips = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/tips.csv')
223-
224-
fig = ff.create_facet_grid(
225-
tips,
226-
y='tip',
227-
facet_row='sex',
228-
facet_col='smoker',
229-
trace_type='box',
230-
)
231-
232-
fig.show()
233-
```
234-
235-
#### Reference
236-
237-
```python
238-
help(ff.create_facet_grid)
239-
```

python/scatter-plots-on-maps.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jupyter:
4040

4141
### Geographical Scatter Plot with px.scatter_geo
4242

43-
For data available as a tidy pandas DataFrame, use `px.scatter_geo` for a geographical scatter plot. The `size` argument is used to set the size of markers from a given column of the DataFrame.
43+
For data available as a tidy pandas DataFrame, use the [Plotly Express](/python/next/plotly-express/) function `px.scatter_geo` for a geographical scatter plot. The `size` argument is used to set the size of markers from a given column of the DataFrame.
4444

4545
```python
4646
import plotly.express as px
@@ -56,8 +56,8 @@ fig.show()
5656
```python
5757
import plotly.express as px
5858
gapminder = px.data.gapminder().query("year == 2007")
59-
fig = px.scatter_geo(gapminder, locations="iso_alpha",
60-
color="continent", # which column to use to set the color of markers
59+
fig = px.scatter_geo(gapminder, locations="iso_alpha",
60+
color="continent", # which column to use to set the color of markers
6161
hover_name="country", # column added to hover information
6262
size="pop", # size of markers
6363
projection="natural earth")
@@ -211,4 +211,4 @@ fig.show()
211211
```
212212

213213
#### Reference
214-
See https://plot.ly/python/reference/#scattergeo and https://plot.ly/python/reference/#layout-geo for more information and chart attribute options!
214+
See https://plot.ly/python/reference/#scattergeo and https://plot.ly/python/reference/#layout-geo for more information and chart attribute options!

python/scattermapbox.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,24 @@ jupyter:
3737
title: Python Scatter Plots with Mapbox | Plotly
3838
---
3939

40+
GOTTA PX
41+
4042
#### Mapbox Access Token
4143

4244
To plot on Mapbox maps with Plotly you'll need a Mapbox account and a public [Mapbox Access Token](https://www.mapbox.com/studio) which you can add to your [Plotly settings](https://plot.ly/settings/mapbox). If you're using a Chart Studio Enterprise server, please see additional instructions here: https://help.plot.ly/mapbox-atlas/.
4345

46+
### Basic example with Plotly Express
47+
48+
For data available as a tidy pandas DataFrame, use the [Plotly Express](/python/next/plotly-express/) function `px.scatter_mapbox` for a scatter plot on a tile map.
49+
50+
```python
51+
import plotly.express as px
52+
px.set_mapbox_access_token(open(".mapbox_token").read())
53+
carshare = px.data.carshare()
54+
fig = px.scatter_mapbox(carshare, lat="centroid_lat", lon="centroid_lon", color="peak_hour", size="car_hours",
55+
color_continuous_scale=px.colors.cyclical.IceFire, size_max=15, zoom=10)
56+
fig.show()
57+
```
4458

4559
#### Basic Example
4660

0 commit comments

Comments
 (0)