|
| 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! |
0 commit comments