nmalkin / plot-likert

Python library to visualize results from Likert scale survey questions
BSD 3-Clause "New" or "Revised" License
102 stars 25 forks source link

Remove "legend" entry from legend #15

Closed charlesbaynham closed 3 years ago

charlesbaynham commented 3 years ago

Hide the "legend" padding box by marking it with a _ for matplotlib to ignore (see http://blog.rtwilson.com/easily-hiding-items-from-the-legend-in-matplotlib/)

PS hope you don't mind all these little changes!

nmalkin commented 3 years ago

PS hope you don't mind all these little changes!

Quite the opposite, I really appreciate them! These are all things that have annoyed me but I haven't gotten around to fixing them. Thank you for taking the time to improve the experience for everyone!

Regarding this specific change, I think it's great — I'd definitely be happy to remove the "Legend" entry.

One question, though: when I run with this change, I get the following warning:

[…]/lib/python3.9/site-packages/pandas/plotting/_matplotlib/core.py:613: UserWarning: The handle <BarContainer object of 2 artists> has a label of '_padding_row' which cannot be automatically added to the legend.
  ax.legend(handles, labels, loc="best", title=title)

(Matplotlib version 3.3.4)

Do you get the same warning?

Fortunately, I do think there's another option: per the guide, having the label be an empty string also hides it.

I made this change:

diff --git a/plot_likert/plot_likert.py b/plot_likert/plot_likert.py
index aa875e1..ebfe75b 100644
--- a/plot_likert/plot_likert.py
+++ b/plot_likert/plot_likert.py
@@ -62,7 +62,7 @@ def plot_counts(
     padding_values = (middles - center).abs()
     padded_counts = pd.concat([padding_values, counts], axis=1)
     # hack to "hide" the label for the padding
-    padded_counts = padded_counts.rename({0: "Legend"}, axis=1)
+    padded_counts = padded_counts.rename({0: ""}, axis=1)

…and it seems to have the desired effect, with no warning.

So maybe we can go with that?

charlesbaynham commented 3 years ago

Embarrassingly, yes I do, but it was swamped in the other output from the rest of my code so I hadn't noticed it. But yes, your solution makes a lot of sense! I'll add it into this PR

nmalkin commented 3 years ago

Great! Let's go with that approach, then. Thanks again!