Skip to content

Patch edgecolor of a legend item does not follow look of figure #7375

Closed

Description

@jorisvandenbossche

It's not about the edgecolor of the legend box, but of the legend items (if they are a patch). In the following example, the clear border around the patches in the legend is a bit surprising to me, and contrasts the look of the figure itself.

Example code:

fig, ax = plt.subplots()
ax.hist(np.random.randn(1000), alpha=0.5, label='group 1')
ax.hist(np.random.randn(1000) + 2, alpha=0.5, label='group 2')
ax.legend()

index

As a notebook: http://nbviewer.jupyter.org/gist/jorisvandenbossche/68b2831ea2b87c76d944e93edc134d59

This issue now surfaces because the default changed to no border for patches, and as such for the histogram in this example (before 2.0, this would not be an issue with the default look). And it becomes more apparent due to the use of a lower alpha in this specific example.

Not sure if this is still the case with master, but I tested with matplotlib 2.0.0b4, on Python 3.5 / Ubuntu (installed from conda-forge)

Activity

  1. anntzer commented on Nov 2, 2016

    @anntzer
    Contributor

    The culprit is https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/patches.py#L295

    self._edgecolor = colors.to_rgba(color, self._alpha)
    

    i.e. the edgecolor of the legend patch inherits the alpha value set by default.

    Fundamentally, this is again a consequence of the design choice of having alpha as a separate attribute, when it should really be part of the color spec (of course this doesn't prevent having helper functions to access and modify the alpha value).

    As a stopgap measure, I think we can fix to_rgba (and friends) to have a force_alpha=True kwarg, and use force_alpha=False here in the meaning of "use the alpha value from the color itself if it specifies one". But I'd say that ultimately, removing internal use of a separate alpha attribute (which can be done without changing the public API) is the way to go.

  2. added a commit that references this issue on Nov 21, 2016
    7a563e6
  3. added a commit that references this issue on Nov 22, 2016
    5d14495
  4. NelleV commented on Nov 22, 2016

    @NelleV
    Member

    closed in #7497

  5. added this to the milestone on Nov 22, 2016
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