From ccaf54ff09006167d7aedd05c144234c55349f17 Mon Sep 17 00:00:00 2001 From: lucafs Date: Wed, 8 Dec 2021 21:56:53 -0300 Subject: [PATCH 1/2] Fix Issue 2966 px.pie has no legend title --- .../python/plotly/plotly/express/_core.py | 3 +++ .../test_core/test_graph_objs/test_pie.py | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 packages/python/plotly/plotly/tests/test_core/test_graph_objs/test_pie.py diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index 01c789ecab2..4e34069d8d1 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_core/test_graph_objs/test_pie.py b/packages/python/plotly/plotly/tests/test_core/test_graph_objs/test_pie.py new file mode 100644 index 00000000000..a94c71cc597 --- /dev/null +++ b/packages/python/plotly/plotly/tests/test_core/test_graph_objs/test_pie.py @@ -0,0 +1,24 @@ +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) From bf1a97a9f8d796926439620911eca559d09e32d4 Mon Sep 17 00:00:00 2001 From: lucafs Date: Thu, 9 Dec 2021 13:15:24 -0300 Subject: [PATCH 2/2] Fixes format --- packages/python/plotly/plotly/express/_core.py | 6 +++--- .../test_px}/test_pie.py | 11 ++++++----- 2 files changed, 9 insertions(+), 8 deletions(-) rename packages/python/plotly/plotly/tests/{test_core/test_graph_objs => test_optional/test_px}/test_pie.py (71%) diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index 4e34069d8d1..deab43f9df9 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -2146,9 +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'] + 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_core/test_graph_objs/test_pie.py b/packages/python/plotly/plotly/tests/test_optional/test_px/test_pie.py similarity index 71% rename from packages/python/plotly/plotly/tests/test_core/test_graph_objs/test_pie.py rename to packages/python/plotly/plotly/tests/test_optional/test_px/test_pie.py index a94c71cc597..4d28df3f066 100644 --- a/packages/python/plotly/plotly/tests/test_core/test_graph_objs/test_pie.py +++ b/packages/python/plotly/plotly/tests/test_optional/test_px/test_pie.py @@ -7,7 +7,7 @@ def _compare_legend_to_column_name(name_column, name_from_fig): - """Compare legend title name to the name given to it + """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) @@ -16,9 +16,10 @@ def _compare_legend_to_column_name(name_column, name_from_fig): def test_pie_like_px(): # Pie - data_name = 'col1' - names_name = 'col2' + 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') + 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)