diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index 01c789ecab2..deab43f9df9 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -2146,6 +2146,9 @@ def make_figure(args, constructor, trace_patch=None, layout_patch=None): layout_patch["legend"] = dict(tracegroupgap=0) if trace_name_labels: layout_patch["legend"]["title_text"] = ", ".join(trace_name_labels) + elif "showlegend" in trace_patch.keys(): + if trace_patch["showlegend"] and constructor == go.Pie: + layout_patch["legend"]["title_text"] = args["names"] if args["title"]: layout_patch["title_text"] = args["title"] elif args["template"].layout.margin.t is None: diff --git a/packages/python/plotly/plotly/tests/test_optional/test_px/test_pie.py b/packages/python/plotly/plotly/tests/test_optional/test_px/test_pie.py new file mode 100644 index 00000000000..4d28df3f066 --- /dev/null +++ b/packages/python/plotly/plotly/tests/test_optional/test_px/test_pie.py @@ -0,0 +1,25 @@ +import plotly.express as px +import plotly.graph_objects as go +from numpy.testing import assert_equal +import numpy as np +import pandas as pd +import pytest + + +def _compare_legend_to_column_name(name_column, name_from_fig): + """Compare legend title name to the name given to it + in "names" value to see if is equal to it and not None. + """ + assert_equal(name_from_fig, name_column) + assert name_from_fig + + +def test_pie_like_px(): + # Pie + data_name = "col1" + names_name = "col2" + df = pd.DataFrame(data={data_name: [3, 2, 1], names_name: [1, 2, 4]}) + fig = px.pie( + df, values=data_name, names=names_name, title="Population of European continent" + ) + _compare_legend_to_column_name(names_name, fig.layout.legend.title.text)