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

Improve legend support for THStacks and TMultiGraphs #3

Open
joeycarter opened this issue Aug 23, 2021 · 0 comments
Open

Improve legend support for THStacks and TMultiGraphs #3

joeycarter opened this issue Aug 23, 2021 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@joeycarter
Copy link
Owner

Currently, individual components of THStacks and TMultiGraphs have to be added to the legend manually with TLegend::AddEntry(), e.g.:

import ROOT as root
import atlasplots as aplt

fig, ax = aplt.subplots()

h1 = root.TH1F(...)
h2 = root.TH1F(...)
hstack = root.THStack(...)
hstack.Add(h1)
hstack.Add(h2)
ax.plot(hstack)

legend = root.TLegend(...)
legend.AddEntry(h1, "Hist 1", "F")
legend.AddEntry(h2, "Hist 2", "F")
legend.Draw()

This goes against the goal of having matplotlib-like syntax and should be fixed. Possible alternatives are:

Use label=[...] in ax.plot()

Pass the legend-entry labels and the label formats as lists in the call to ax.plot():

h1 = root.TH1F("h1", "Hist 1", ...)
h2 = root.TH1F("h2", "Hist 2", ...)
hstack = root.THStack(...)
hstack.Add(h1)
hstack.Add(h2)
ax.plot(hstack, label=[x.GetTitle() for x in hstack.GetStack()], labelfmt=["F", "F"])
ax.legend(...)  # Creates legend with entry labels "Hist 1" and "Hist 2"

This is the way matplotlib handles legend entries for stacked histograms (see https://matplotlib.org/stable/gallery/statistics/histogram_multihist.html) and has the advantage of flexibility, but it is fairly verbose.

Use label="auto" in ax.plot()

Tell atlasplots to automatically scan through the THStack or TMultiGraph and add the legend entries based on the titles of the individual components:

h1 = root.TH1F("h1", "Hist 1", ...)
h2 = root.TH1F("h2", "Hist 2", ...)
hstack = root.THStack(...)
hstack.Add(h1)
hstack.Add(h2)
ax.plot(hstack, label="auto", labelfmt="F")
ax.legend(...)  # Creates legend with entry labels "Hist 1" and "Hist 2"

This option has the advantage of being concise and ensuring that the legend entries are correctly matched to the stack or multi-graph component, but may be less flexible in case different label formats are needed for each of the components.

Fill the legend manually

Finally, following the example of matplotlib, the component handles and format strings could be manually passed to ax.legend(), instead of using label=... in the call to ax.plot():

h1 = root.TH1F("h1", "Hist 1", ...)
h2 = root.TH1F("h2", "Hist 2", ...)
hstack = root.THStack(...)
hstack.Add(h1)
hstack.Add(h2)
ax.plot(hstack)
ax.legend([h1, h2], ["F", "F"])  # Creates legend with entry labels "Hist 1" and "Hist 2"
@joeycarter joeycarter added the enhancement New feature or request label Aug 23, 2021
@joeycarter joeycarter self-assigned this Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant