Skip to content

Commit 13a1c2e

Browse files
Merge pull request #2051 from plotly/px_legend_title
use legend titles in PX
2 parents 872dfba + 80fe697 commit 13a1c2e

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

Diff for: doc/python/plotly-express.md

+4-5
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.1"
9-
jupytext_version: 1.1.1
8+
format_version: '1.2'
9+
jupytext_version: 1.3.1
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -20,10 +20,9 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.7.3
23+
version: 3.6.8
2424
plotly:
25-
description:
26-
Plotly Express is a terse, consistent, high-level API for rapid data
25+
description: Plotly Express is a terse, consistent, high-level API for rapid data
2726
exploration and figure generation.
2827
display_as: file_settings
2928
language: python

Diff for: packages/python/plotly/plotly/express/_core.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,7 @@ def make_figure(args, constructor, trace_patch={}, layout_patch={}):
11841184
nrows = ncols = 1
11851185
col_labels = []
11861186
row_labels = []
1187+
trace_name_labels = None
11871188
for group_name in sorted_group_names:
11881189
group = grouped.get_group(group_name if len(group_name) > 1 else group_name[0])
11891190
mapping_labels = OrderedDict()
@@ -1197,7 +1198,7 @@ def make_figure(args, constructor, trace_patch={}, layout_patch={}):
11971198
trace_name_labels[key] = str(val)
11981199
if m.variable == "animation_frame":
11991200
frame_name = val
1200-
trace_name = ", ".join(k + "=" + v for k, v in trace_name_labels.items())
1201+
trace_name = ", ".join(trace_name_labels.values())
12011202
if frame_name not in trace_names_by_frame:
12021203
trace_names_by_frame[frame_name] = set()
12031204
trace_names = trace_names_by_frame[frame_name]
@@ -1346,7 +1347,9 @@ def make_figure(args, constructor, trace_patch={}, layout_patch={}):
13461347
for v in ["title", "height", "width"]:
13471348
if args[v]:
13481349
layout_patch[v] = args[v]
1349-
layout_patch["legend"] = {"tracegroupgap": 0}
1350+
layout_patch["legend"] = dict(tracegroupgap=0)
1351+
if trace_name_labels:
1352+
layout_patch["legend"]["title"] = ", ".join(trace_name_labels)
13501353
if "title" not in layout_patch and args["template"].layout.margin.t is None:
13511354
layout_patch["margin"] = {"t": 60}
13521355
if (

Diff for: packages/python/plotly/plotly/tests/test_core/test_px/test_px.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,18 @@ def test_labels():
6363
facet_col="day",
6464
color="size",
6565
symbol="sex",
66-
labels={c: c[::-1] for c in tips.columns},
66+
labels={c: c.upper() for c in tips.columns},
6767
)
68-
assert "xes" in fig.data[0].hovertemplate
69-
assert "llib_latot" in fig.data[0].hovertemplate
70-
assert "ezis" in fig.data[0].hovertemplate
71-
assert "yad" in fig.data[0].hovertemplate
72-
assert "emit" in fig.data[0].hovertemplate
73-
assert fig.data[0].name.startswith("xes")
74-
assert fig.layout.xaxis.title.text == "llib_latot"
75-
assert fig.layout.coloraxis.colorbar.title.text == "ezis"
76-
assert fig.layout.annotations[0].text.startswith("yad")
77-
assert fig.layout.annotations[4].text.startswith("emit")
68+
assert "SEX" in fig.data[0].hovertemplate
69+
assert "TOTAL_BILL" in fig.data[0].hovertemplate
70+
assert "SIZE" in fig.data[0].hovertemplate
71+
assert "DAY" in fig.data[0].hovertemplate
72+
assert "TIME" in fig.data[0].hovertemplate
73+
assert fig.layout.legend.title.text.startswith("SEX")
74+
assert fig.layout.xaxis.title.text == "TOTAL_BILL"
75+
assert fig.layout.coloraxis.colorbar.title.text == "SIZE"
76+
assert fig.layout.annotations[0].text.startswith("DAY")
77+
assert fig.layout.annotations[4].text.startswith("TIME")
7878

7979

8080
def test_px_templates():

0 commit comments

Comments
 (0)