Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add legend title to px.pie #3508

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/python/plotly/plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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)