Skip to content

corrected color bug in pie/sunburst etc #2213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion doc/python/pie-charts.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jupyter:
text_representation:
extension: .md
format_name: markdown
format_version: "1.2"
format_version: '1.2'
jupytext_version: 1.3.0
kernelspec:
display_name: Python 3
Expand Down Expand Up @@ -73,6 +73,21 @@ fig = px.pie(df, values='tip', names='day', color_discrete_sequence=px.colors.se
fig.show()
```

### Using an explicit mapping for discrete colors

For more information about discrete colors, see the [dedicated page](/python/discrete-color).

```python
import plotly.express as px
df = px.data.tips()
fig = px.pie(df, values='tip', names='day', color='day',
color_discrete_map={'Thur':'lightcyan',
'Fri':'cyan',
'Sat':'royalblue',
'Sun':'darkblue'})
fig.show()
```

### Customizing a pie chart created with px.pie

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).
Expand Down
14 changes: 13 additions & 1 deletion doc/python/sunburst-charts.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jupyter:
text_representation:
extension: .md
format_name: markdown
format_version: "1.2"
format_version: '1.2'
jupytext_version: 1.3.0
kernelspec:
display_name: Python 3
Expand Down Expand Up @@ -109,6 +109,18 @@ fig = px.sunburst(df, path=['sex', 'day', 'time'], values='total_bill', color='t
fig.show()
```

### Using an explicit mapping for discrete colors

For more information about discrete colors, see the [dedicated page](/python/discrete-color).

```python
import plotly.express as px
df = px.data.tips()
fig = px.sunburst(df, path=['sex', 'day', 'time'], values='total_bill', color='time',
color_discrete_map={'(?)':'black', 'Lunch':'gold', 'Dinner':'darkblue'})
fig.show()
```

### Rectangular data with missing values

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).
Expand Down
18 changes: 15 additions & 3 deletions doc/python/treemaps.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ jupyter:
text_representation:
extension: .md
format_name: markdown
format_version: "1.2"
jupytext_version: 1.3.1
format_version: '1.2'
jupytext_version: 1.3.0
kernelspec:
display_name: Python 3
language: python
Expand All @@ -20,7 +20,7 @@ jupyter:
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.6.8
version: 3.7.3
plotly:
description: How to make Treemap Charts with Plotly
display_as: basic
Expand Down Expand Up @@ -101,6 +101,18 @@ fig = px.treemap(df, path=['all', 'sex', 'day', 'time'], values='total_bill', co
fig.show()
```

### Using an explicit mapping for discrete colors

For more information about discrete colors, see the [dedicated page](/python/discrete-color).

```python
import plotly.express as px
df = px.data.tips()
fig = px.treemap(df, path=['sex', 'day', 'time'], values='total_bill', color='time',
color_discrete_map={'(?)':'black', 'Lunch':'gold', 'Dinner':'darkblue'})
fig.show()
```

### Rectangular data with missing values

If the dataset is not fully rectangular, missing values should be supplied as `None`.
Expand Down
5 changes: 4 additions & 1 deletion packages/python/plotly/plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,10 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref):
mapping_labels[attr_label] = "%{color}"
else:
trace_patch["marker"]["colors"] = []
mapping = {}
if args["color_discrete_map"] is not None:
mapping = args["color_discrete_map"].copy()
else:
mapping = {}
for cat in trace_data[attr_value]:
if mapping.get(cat) is None:
mapping[cat] = args["color_discrete_sequence"][
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ def test_sunburst_treemap_with_path_color():
fig = px.sunburst(df, path=path, color="vendors")
assert len(np.unique(fig.data[0].marker.colors)) == 9

# Discrete color and color_discrete_map
cmap = {"Tech": "yellow", "Finance": "magenta", "(?)": "black"}
fig = px.sunburst(df, path=path, color="sectors", color_discrete_map=cmap)
assert np.all(np.in1d(fig.data[0].marker.colors, list(cmap.values())))

# Numerical column in path
df["regions"] = df["regions"].map({"North": 1, "South": 2})
path = ["total", "regions", "sectors", "vendors"]
Expand Down