Skip to content

Commit 3a4c519

Browse files
pattern docs
1 parent 6c43268 commit 3a4c519

File tree

4 files changed

+233
-13
lines changed

4 files changed

+233
-13
lines changed

doc/python/bar-charts.md

+19-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.2'
9-
jupytext_version: 1.6.0
9+
jupytext_version: 1.4.2
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -47,7 +47,7 @@ fig.show()
4747
```
4848

4949

50-
#### Bar chart with Long Format Data
50+
#### Bar charts with Long Format Data
5151

5252
Long-form data has one row per observation, and one column per variable. This is suitable for storing and displaying multivariate data i.e. with dimension greater than 2. This format is sometimes called "tidy".
5353

@@ -69,7 +69,7 @@ fig.show()
6969
long_df
7070
```
7171

72-
#### Bar chart with Wide Format Data
72+
#### Bar charts with Wide Format Data
7373
Wide-form data has one row per value of one of the first variable, and one column per value of the second variable. This is suitable for storing and displaying 2-dimensional data.
7474

7575
```python
@@ -85,7 +85,7 @@ fig.show()
8585
wide_df
8686
```
8787

88-
### Bar chart in Dash
88+
### Bar charts in Dash
8989

9090
[Dash](https://plotly.com/dash/) is the best way to build analytical apps in Python using Plotly figures. To run the app below, run `pip install dash`, click "Download" to get the code and run `python app.py`.
9191

@@ -98,9 +98,9 @@ snippet_url = 'https://dash-gallery.plotly.host/python-docs-dash-snippets/'
9898
IFrame(snippet_url + 'bar-charts', width='100%', height=630)
9999
```
100100

101-
### Customize bar chart with Plotly Express
101+
### Customize bar charts with Plotly Express
102102

103-
The bar plot can be customized using keyword arguments.
103+
The bar plot can be customized using keyword arguments, for example to use [continuous color](https://plotly.com/python/colorscales/), as below, or [discrete color](/python/discrete-color/), as above.
104104

105105
```python
106106
import plotly.express as px
@@ -122,8 +122,9 @@ fig = px.bar(df, x="sex", y="total_bill", color='time')
122122
fig.show()
123123
```
124124

125+
The default stacked bar chart behavior can be changed to grouped (also known as clustered) using the `barmode` argument:
126+
125127
```python
126-
# Change the default stacking
127128
import plotly.express as px
128129
df = px.data.tips()
129130
fig = px.bar(df, x="sex", y="total_bill",
@@ -132,6 +133,17 @@ fig = px.bar(df, x="sex", y="total_bill",
132133
fig.show()
133134
```
134135

136+
Bar charts afford the use of [patterns (also known as hatching or texture)](/python/pattern-hatching-texture/) in addition to color:
137+
138+
```python
139+
import plotly.express as px
140+
df = px.data.medals_long()
141+
142+
fig = px.bar(df, x="medal", y="count", color="nation",
143+
pattern_shape="nation", pattern_shape_sequence=[".", "x", "+"])
144+
fig.show()
145+
```
146+
135147
#### Facetted subplots
136148

137149
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`.

doc/python/histograms.md

+48-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.2'
9-
jupytext_version: 1.6.0
9+
jupytext_version: 1.4.2
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.7.6
23+
version: 3.7.7
2424
plotly:
2525
description: How to make Histograms in Python with Plotly.
2626
display_as: statistical
@@ -36,12 +36,12 @@ jupyter:
3636
thumbnail: thumbnail/histogram.jpg
3737
---
3838

39-
In statistics, a [histogram](https://en.wikipedia.org/wiki/Histogram) is representation of the distribution of numerical data, where the data are binned and the count for each bin is represented. More generally, 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.
39+
In statistics, a [histogram](https://en.wikipedia.org/wiki/Histogram) is representation of the distribution of numerical data, where the data are binned and the count for each bin is represented. More generally, in plotly a histogram is an aggregated bar chart, with several possible aggregation functions (e.g. sum, average, count...).
4040

41-
If you're looking instead for bar charts, i.e. representing data with rectangular
41+
If you're looking instead for bar charts, i.e. representing *raw, unaggregated* data with rectangular
4242
bar, go to the [Bar Chart tutorial](/python/bar-charts/).
4343

44-
## Histogram with Plotly Express
44+
## Histograms with Plotly Express
4545

4646
[Plotly Express](/python/plotly-express/) is the easy-to-use, high-level interface to Plotly, which [operates on a variety of types of data](/python/px-arguments/) and produces [easy-to-style figures](/python/styling-plotly-express/).
4747

@@ -71,6 +71,30 @@ fig = px.histogram(df, x="total_bill", nbins=20)
7171
fig.show()
7272
```
7373

74+
### Histograms on Date Data
75+
76+
Plotly histograms will automatically bin date data in addition to numerical data:
77+
78+
```python
79+
import plotly.express as px
80+
81+
df = px.data.stocks()
82+
fig = px.histogram(df, x="date")
83+
fig.update_layout(bargap=0.2)
84+
fig.show()
85+
```
86+
87+
### Histograms on Categorical Data
88+
89+
Plotly histograms will automatically bin numerical or date data but can also be used on raw categorical data, as in the following example, where the X-axis value is the categorical "day" variable:
90+
91+
```python
92+
import plotly.express as px
93+
df = px.data.tips()
94+
fig = px.histogram(df, x="day", category_orders=dict(day=["Thur", "Fri", "Sat", "Sun"]))
95+
fig.show()
96+
```
97+
7498
#### Histograms in Dash
7599

76100
[Dash](https://plotly.com/dash/) is the best way to build analytical apps in Python using Plotly figures. To run the app below, run `pip install dash`, click "Download" to get the code and run `python app.py`.
@@ -147,6 +171,25 @@ fig = px.histogram(df, x="total_bill", y="tip", histfunc='avg')
147171
fig.show()
148172
```
149173

174+
The default `histfunc` is `sum` if `y` is given, and works with categorical as well as binned numeric data on the `x` axis:
175+
176+
```python
177+
import plotly.express as px
178+
df = px.data.tips()
179+
fig = px.histogram(df, x="day", y="total_bill", category_orders=dict(day=["Thur", "Fri", "Sat", "Sun"]))
180+
fig.show()
181+
```
182+
183+
Histograms afford the use of [patterns (also known as hatching or texture)](/python/pattern-hatching-texture/) in addition to color:
184+
185+
```python
186+
import plotly.express as px
187+
188+
df = px.data.tips()
189+
fig = px.histogram(df, x="sex", y="total_bill", color="sex", pattern_shape="smoker")
190+
fig.show()
191+
```
192+
150193
#### Visualizing the distribution
151194

152195
With the `marginal` keyword, a subplot is drawn alongside the histogram, visualizing the distribution. See [the distplot page](https://plotly.com/python/distplot/)for more examples of combined statistical representations.
+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
---
2+
jupyter:
3+
jupytext:
4+
notebook_metadata_filter: all
5+
text_representation:
6+
extension: .md
7+
format_name: markdown
8+
format_version: '1.2'
9+
jupytext_version: 1.4.2
10+
kernelspec:
11+
display_name: Python 3
12+
language: python
13+
name: python3
14+
language_info:
15+
codemirror_mode:
16+
name: ipython
17+
version: 3
18+
file_extension: .py
19+
mimetype: text/x-python
20+
name: python
21+
nbconvert_exporter: python
22+
pygments_lexer: ipython3
23+
version: 3.7.7
24+
plotly:
25+
description: How to use patterns (also known as hatching or texture) with bar
26+
charts.
27+
display_as: basic
28+
language: python
29+
layout: base
30+
name: Patterns, Hatching, Texture
31+
order: 18
32+
page_type: u-guide
33+
permalink: python/pattern-hatching-texture/
34+
thumbnail: thumbnail/pattern.png
35+
---
36+
37+
[Bar charts](/python/bar-charts/), [histograms](/python/histograms/) and [polar bar charts](/python/wind-rose-charts/) have large markers which support not only a fill color, but also an optional **pattern** (also known as "hatching" or "texture"). This can be used for a variety of reasons:
38+
39+
* to double-encode variables (i.e. using both color and pattern) to improve accessibility for visually-impaired end-users
40+
* to encode an additional variable beyond just using color
41+
* to make charts that are easier to print in black and white
42+
43+
44+
### Patterned Bar Charts with Plotly Express
45+
46+
the `px.bar()`, `px.histogram()` and `px.bar_polar()` functions support the `pattern_shape` argument. In the chart below, we double-encode `nation` using color and pattern:
47+
48+
```python
49+
import plotly.express as px
50+
df = px.data.medals_long()
51+
52+
fig = px.bar(df, x="medal", y="count", color="nation", pattern_shape="nation")
53+
fig.show()
54+
```
55+
56+
In the chart below we use `px.histogram()` instead of `px.bar()` to aggregate multiple values together, and encode one variable (sex) using both color and x-position and another (smoker) using patterns:
57+
58+
```python
59+
import plotly.express as px
60+
61+
df = px.data.tips()
62+
fig = px.histogram(df, x="sex", y="total_bill", color="sex", pattern_shape="smoker")
63+
fig.show()
64+
```
65+
66+
### Controlling Pattern Assignment
67+
68+
In the charts above, the first value of the variable assigned `pattern_shape` gets the empty pattern, but this (and indeed every pattern-to-variable assignment) can be controlled using `pattern_shape_sequence` and `pattern_shape_map`, analogously to the way [discrete colors](/python/discrete-color/) can be mapped using Plotly Express.
69+
70+
Here we use `pattern_shape_sequence` to replace the defaults and include a pattern-shape for the first variable:
71+
72+
```python tags=[]
73+
import plotly.express as px
74+
df = px.data.medals_long()
75+
76+
fig = px.bar(df, x="medal", y="count", color="nation",
77+
pattern_shape="nation", pattern_shape_sequence=[".", "x", "+"])
78+
fig.show()
79+
```
80+
81+
Here we use `pattern_shape_map` to explictly assign a shape to each value of `nation`, regardless of order:
82+
83+
```python
84+
import plotly.express as px
85+
df = px.data.medals_long()
86+
87+
fig = px.bar(df, x="medal", y="count", color="nation",
88+
pattern_shape="nation", pattern_shape_map={
89+
"China": ".", "Canada": "/", "South Korea": "+"
90+
})
91+
fig.show()
92+
```
93+
94+
### Black on White Patterns for Print
95+
96+
When creating figures meant to be printed on black and white printers, it is better to *replace* the fill-color with the pattern, rather than to overlay it. This can be controlled with the `<trace>.marker.pattern.fillmode` attribute, which defaults to `"overlay"` but can be set to `"replace"` instead. Changing this attribute, and using a simpler default template and color scheme gives the following output:
97+
98+
```python
99+
import plotly.express as px
100+
df = px.data.medals_long()
101+
102+
fig = px.bar(df, x="medal", y="count", color="nation",
103+
pattern_shape="nation", pattern_shape_sequence=[".", "x", "+"],
104+
template="simple_white"
105+
)
106+
fig.update_traces(
107+
marker=dict(color="black", line_color="black", pattern_fillmode="replace")
108+
)
109+
fig.show()
110+
```
111+
112+
Of course, this setting can be used without making the figure monochrome as well:
113+
114+
```python
115+
import plotly.express as px
116+
df = px.data.medals_long()
117+
118+
fig = px.bar(df, x="medal", y="count", color="nation",
119+
pattern_shape="nation", pattern_shape_sequence=[".", "x", "+"],
120+
)
121+
fig.update_traces(
122+
marker=dict(line_color="grey", pattern_fillmode="replace")
123+
)
124+
fig.show()
125+
```
126+
127+
### Patterns using Graph Objects
128+
129+
If Plotly Express does not provide a good starting point, it is also possible to use [the more generic `go.Bar` class from `plotly.graph_objects`](/python/graph-objects/).
130+
131+
```python
132+
import plotly.graph_objects as go
133+
134+
fig = go.Figure()
135+
136+
fig.add_trace(go.Bar(x=["a","b"], y=[1,2], marker_pattern_shape="."))
137+
fig.add_trace(go.Bar(x=["a","b"], y=[3,1], marker_pattern_shape="x"))
138+
fig.add_trace(go.Bar(x=["a","b"], y=[2,3], marker_pattern_shape="+"))
139+
140+
fig.show()
141+
```
142+
143+
#### Reference
144+
145+
See https://plotly.com/python/reference/bar/ for more information and chart attribute options!

doc/python/plotly-express.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.2'
9-
jupytext_version: 1.6.0
9+
jupytext_version: 1.4.2
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -130,6 +130,15 @@ fig = px.bar(df, x="sex", y="total_bill", color="smoker", barmode="group")
130130
fig.show()
131131
```
132132

133+
```python
134+
import plotly.express as px
135+
df = px.data.medals_long()
136+
137+
fig = px.bar(df, x="medal", y="count", color="nation",
138+
pattern_shape="nation", pattern_shape_sequence=[".", "x", "+"])
139+
fig.show()
140+
```
141+
133142
**Read more about [facet plots](/python/facet-plots/).**
134143

135144
```python
@@ -243,6 +252,17 @@ fig = px.treemap(df, path=[px.Constant('world'), 'continent', 'country'], values
243252
fig.show()
244253
```
245254

255+
**Read more about [treemaps](/python/icicle-charts/).**
256+
257+
```python
258+
import plotly.express as px
259+
import numpy as np
260+
df = px.data.gapminder().query("year == 2007")
261+
fig = px.icicle(df, path=[px.Constant('world'), 'continent', 'country'], values='pop',
262+
color='lifeExp', hover_data=['iso_alpha'])
263+
fig.show()
264+
```
265+
246266
#### Distributions
247267

248268
**Read more about [histograms](/python/histograms/).**

0 commit comments

Comments
 (0)