Skip to content

Duplicate legend labels with different colors can often result in an error. #10053

Closed

Description

@dpryan79

Bug report

If one makes a scatter plot (or a line graph) having multiple points with different markers and colors, but the same label, this will result in an error in version 2.1.1 (but not 2.1.0). A small example is provided below:

Code for reproduction

#!/usr/bin/env python
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt

color='#00ff00ff'
plt.scatter(0.0, 1.0, color=color, marker='o', label='test')
color='#00ffaaff'
plt.scatter(0.5, 0.0, color=color, marker='v', label='test')
plt.legend()
plt.savefig('foo.png')

Actual outcome

This example uses a scatter plot, but the same issue can arise with line plots if one uses a numpy vector as a color (e.g., [0.0, 1.0, 0.0, 1.0]). The error produced for the above example is as follows:

Traceback (most recent call last):
  File "foo.py", line 11, in <module>
    plt.legend()
  File "/home/ryan/.local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 3748, in legend
    ret = gca().legend(*args, **kwargs)
  File "/home/ryan/.local/lib/python2.7/site-packages/matplotlib/axes/_axes.py", line 498, in legend
    **kwargs)
  File "/home/ryan/.local/lib/python2.7/site-packages/matplotlib/legend.py", line 1402, in _parse_legend_args
    handles, labels = _get_legend_handles_labels(axs, handlers)
  File "/home/ryan/.local/lib/python2.7/site-packages/matplotlib/legend.py", line 1360, in _get_legend_handles_labels
    and not _in_handles(handle, label)):
  File "/home/ryan/.local/lib/python2.7/site-packages/matplotlib/legend.py", line 1344, in _in_handles
    if f_h.get_facecolor() != h.get_facecolor():
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

At the end of the day, this ends up being because the face (and edge) colors are converted to a numpy vector, which can't be compared with != since they have multiple elements. With the help of git bisect, I've tracked this down to this commit at the end of October.

Now I'll grant that this only happens if people use the same label for multiple symbols/lines, which is a bad idea...but one can't always prevent end users from doing that.

Activity

  1. dpryan79 commented on Dec 20, 2017

    @dpryan79
    Author

    I suspect that #10031 is related.

  2. added this to the v2.1.1-doc milestone on Dec 20, 2017
  3. jklymak commented on Dec 20, 2017

    @jklymak
    Member

    Definitely #10031 Thanks a lot for a reproducible example, however, which has eluded us.

    This is kind of strange because the same code works fine for ax.plot.

  4. modified the milestones: v2.1.1-doc, v2.1.2 on Dec 20, 2017
  5. dpryan79 commented on Dec 21, 2017

    @dpryan79
    Author

    If it's helpful, here's another example (this time using plot()):

    #!/usr/bin/env python
    import matplotlib as mpl
    mpl.use('Agg')
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.arange(0, 10, 0.01)
    y0 = np.sin(x)
    y1 = np.cos(x)
    color = np.array([0.0, 1.0, 0.0, 1.0])
    plt.plot(x, y0, color=color, label="test")
    color = np.array([0.0, 1.0, 0.6, 1.0])
    plt.plot(x, y1, color=color, label="test")
    plt.legend()
    plt.savefig('foo.png')
    
  6. jklymak commented on Dec 21, 2017

    @jklymak
    Member

    Fixed by #10064. Thanks a lot for the bug report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions