Skip to content

Commit eeb4e90

Browse files
authored
Merge pull request #2213 from plotly/color-path-bug
corrected color bug in pie/sunburst etc
2 parents 1748f63 + e860847 commit eeb4e90

File tree

5 files changed

+53
-6
lines changed

5 files changed

+53
-6
lines changed

doc/python/pie-charts.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: "1.2"
8+
format_version: '1.2'
99
jupytext_version: 1.3.0
1010
kernelspec:
1111
display_name: Python 3
@@ -73,6 +73,21 @@ fig = px.pie(df, values='tip', names='day', color_discrete_sequence=px.colors.se
7373
fig.show()
7474
```
7575

76+
### Using an explicit mapping for discrete colors
77+
78+
For more information about discrete colors, see the [dedicated page](/python/discrete-color).
79+
80+
```python
81+
import plotly.express as px
82+
df = px.data.tips()
83+
fig = px.pie(df, values='tip', names='day', color='day',
84+
color_discrete_map={'Thur':'lightcyan',
85+
'Fri':'cyan',
86+
'Sat':'royalblue',
87+
'Sun':'darkblue'})
88+
fig.show()
89+
```
90+
7691
### Customizing a pie chart created with px.pie
7792

7893
In the example below, we first create a pie chart with `px,pie`, using some of its options such as `hover_data` (which columns should appear in the hover) or `labels` (renaming column names). For further tuning, we call `fig.update_traces` to set other parameters of the chart (you can also use `fig.update_layout` for changing the layout).

doc/python/sunburst-charts.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: "1.2"
8+
format_version: '1.2'
99
jupytext_version: 1.3.0
1010
kernelspec:
1111
display_name: Python 3
@@ -109,6 +109,18 @@ fig = px.sunburst(df, path=['sex', 'day', 'time'], values='total_bill', color='t
109109
fig.show()
110110
```
111111

112+
### Using an explicit mapping for discrete colors
113+
114+
For more information about discrete colors, see the [dedicated page](/python/discrete-color).
115+
116+
```python
117+
import plotly.express as px
118+
df = px.data.tips()
119+
fig = px.sunburst(df, path=['sex', 'day', 'time'], values='total_bill', color='time',
120+
color_discrete_map={'(?)':'black', 'Lunch':'gold', 'Dinner':'darkblue'})
121+
fig.show()
122+
```
123+
112124
### Rectangular data with missing values
113125

114126
If the dataset is not fully rectangular, missing values should be supplied as `None`. Note that the parents of `None` entries must be a leaf, i.e. it cannot have other children than `None` (otherwise a `ValueError` is raised).

doc/python/treemaps.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: "1.2"
9-
jupytext_version: 1.3.1
8+
format_version: '1.2'
9+
jupytext_version: 1.3.0
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.6.8
23+
version: 3.7.3
2424
plotly:
2525
description: How to make Treemap Charts with Plotly
2626
display_as: basic
@@ -101,6 +101,18 @@ fig = px.treemap(df, path=['all', 'sex', 'day', 'time'], values='total_bill', co
101101
fig.show()
102102
```
103103

104+
### Using an explicit mapping for discrete colors
105+
106+
For more information about discrete colors, see the [dedicated page](/python/discrete-color).
107+
108+
```python
109+
import plotly.express as px
110+
df = px.data.tips()
111+
fig = px.treemap(df, path=['sex', 'day', 'time'], values='total_bill', color='time',
112+
color_discrete_map={'(?)':'black', 'Lunch':'gold', 'Dinner':'darkblue'})
113+
fig.show()
114+
```
115+
104116
### Rectangular data with missing values
105117

106118
If the dataset is not fully rectangular, missing values should be supplied as `None`.

packages/python/plotly/plotly/express/_core.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,10 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref):
331331
mapping_labels[attr_label] = "%{color}"
332332
else:
333333
trace_patch["marker"]["colors"] = []
334-
mapping = {}
334+
if args["color_discrete_map"] is not None:
335+
mapping = args["color_discrete_map"].copy()
336+
else:
337+
mapping = {}
335338
for cat in trace_data[attr_value]:
336339
if mapping.get(cat) is None:
337340
mapping[cat] = args["color_discrete_sequence"][

packages/python/plotly/plotly/tests/test_core/test_px/test_px_functions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ def test_sunburst_treemap_with_path_color():
218218
fig = px.sunburst(df, path=path, color="vendors")
219219
assert len(np.unique(fig.data[0].marker.colors)) == 9
220220

221+
# Discrete color and color_discrete_map
222+
cmap = {"Tech": "yellow", "Finance": "magenta", "(?)": "black"}
223+
fig = px.sunburst(df, path=path, color="sectors", color_discrete_map=cmap)
224+
assert np.all(np.in1d(fig.data[0].marker.colors, list(cmap.values())))
225+
221226
# Numerical column in path
222227
df["regions"] = df["regions"].map({"North": 1, "South": 2})
223228
path = ["total", "regions", "sectors", "vendors"]

0 commit comments

Comments
 (0)