From 7ad9b69b84574c63502ce7619db2ac8976834ada Mon Sep 17 00:00:00 2001 From: Emmanuelle Gouillart Date: Tue, 18 Feb 2020 16:12:08 -0500 Subject: [PATCH 1/2] corrected color bug in pie/sunburst etc --- packages/python/plotly/plotly/express/_core.py | 5 ++++- .../plotly/tests/test_core/test_px/test_px_functions.py | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index 1f9e2706af7..9d586415633 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -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"][ diff --git a/packages/python/plotly/plotly/tests/test_core/test_px/test_px_functions.py b/packages/python/plotly/plotly/tests/test_core/test_px/test_px_functions.py index 0fc38c94d4d..e4ac26d28a5 100644 --- a/packages/python/plotly/plotly/tests/test_core/test_px/test_px_functions.py +++ b/packages/python/plotly/plotly/tests/test_core/test_px/test_px_functions.py @@ -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"] From e860847c23978f9e327937584604a9843c08fbba Mon Sep 17 00:00:00 2001 From: Emmanuelle Gouillart Date: Tue, 18 Feb 2020 17:31:21 -0500 Subject: [PATCH 2/2] added doc examples --- doc/python/pie-charts.md | 17 ++++++++++++++++- doc/python/sunburst-charts.md | 14 +++++++++++++- doc/python/treemaps.md | 18 +++++++++++++++--- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/doc/python/pie-charts.md b/doc/python/pie-charts.md index 75905b990cf..38dfd175c68 100644 --- a/doc/python/pie-charts.md +++ b/doc/python/pie-charts.md @@ -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 @@ -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). diff --git a/doc/python/sunburst-charts.md b/doc/python/sunburst-charts.md index 69f89133c35..1ee76475475 100644 --- a/doc/python/sunburst-charts.md +++ b/doc/python/sunburst-charts.md @@ -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 @@ -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). diff --git a/doc/python/treemaps.md b/doc/python/treemaps.md index 36dd866e008..5f9c9156943 100644 --- a/doc/python/treemaps.md +++ b/doc/python/treemaps.md @@ -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 @@ -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 @@ -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`.