Skip to content

Commit ccaf54f

Browse files
committed
Fix Issue 2966 px.pie has no legend title
1 parent f05faf7 commit ccaf54f

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,9 @@ def make_figure(args, constructor, trace_patch=None, layout_patch=None):
21462146
layout_patch["legend"] = dict(tracegroupgap=0)
21472147
if trace_name_labels:
21482148
layout_patch["legend"]["title_text"] = ", ".join(trace_name_labels)
2149+
elif('showlegend' in trace_patch.keys()):
2150+
if(trace_patch['showlegend'] and constructor == go.Pie):
2151+
layout_patch["legend"]["title_text"] = args['names']
21492152
if args["title"]:
21502153
layout_patch["title_text"] = args["title"]
21512154
elif args["template"].layout.margin.t is None:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import plotly.express as px
2+
import plotly.graph_objects as go
3+
from numpy.testing import assert_equal
4+
import numpy as np
5+
import pandas as pd
6+
import pytest
7+
8+
9+
def _compare_legend_to_column_name(name_column, name_from_fig):
10+
"""Compare legend title name to the name given to it
11+
in "names" value to see if is equal to it and not None.
12+
"""
13+
assert_equal(name_from_fig, name_column)
14+
assert name_from_fig
15+
16+
17+
def test_pie_like_px():
18+
# Pie
19+
data_name = 'col1'
20+
names_name = 'col2'
21+
df = pd.DataFrame(data={data_name: [3, 2, 1], names_name: [1, 2, 4]})
22+
fig = px.pie(df, values=data_name, names=names_name,
23+
title='Population of European continent')
24+
_compare_legend_to_column_name(names_name, fig.layout.legend.title.text)

0 commit comments

Comments
 (0)