Skip to content

Using edgecolors='face' with stackplot causes value error when using plt.legend() #20258

Closed

Description

@Stratton19

Bug report

I was making a stack plot and wanted the edge colors of the plotted lines to be the same as the fill color instead of the default white, so I set edgecolor='face'. This produces a value error when using plt.legend(). Without the legend function, there are no errors and it plots as expected. I tried changing different parameters of the legend() function, but didn't have any success.

Code for reproduction

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Create a dataset
my_count = [
    "France", "Australia", "Japan", "USA", "Germany",
    "Congo", "China", "England", "Spain", "Greece",
    "Marocco", "South Africa", "Indonesia", "Peru", "Chili", "Brazil"
]
df = pd.DataFrame(
    {
        "country": np.repeat(my_count, 10),
        "years": list(range(2000, 2010)) * 16,
        "value": np.random.rand(160)
    }
)
pivot = df.groupby(["country", "years"])["value"].sum().unstack(0).cumsum()
# Plot dataset
plt.stackplot(
    pivot.index.values,
    pivot.T.values,
    labels=pivot.columns,
    alpha=0.2,
    edgecolors="face"
)
plt.legend()
plt.show()

Actual outcome

image
with the following value error:

ValueError: Using a string of single character colors as a color sequence is not supported. The colors can be passed as an explicit list instead.

Expected outcome

The same graph as above, but with a legend.

Matplotlib version

  • Operating system: Microsoft Windows 10
  • Matplotlib version: 3.4.2
  • Matplotlib backend: module://ipykernel.pylab.backend_inline
  • Python version: 3.8.5
  • Other libraries: numpy, pandas (to create dataset)

Installed matplotlib using 'pip install matplotlib'.

Activity

  1. jklymak commented on May 18, 2021

    @jklymak
    Member

    I agree this doesn't work. Did it ever work?

  2. added this to the v3.5.0 milestone on May 18, 2021
  3. jklymak commented on May 18, 2021

    @jklymak
    Member

    Allowing edgecolor='face' is relatively new (I think 3.4; #18480, @efiring).

    It seems that when we try to make the coloured polygons for legend we don't parse the 'face' value for get_edgecolors. Probably a relatively easy fix. I'll bump the milestone since this is a new feature that isn't quite working as expected with legends...

  4. modified the milestones: v3.5.0, v3.4.3 on May 18, 2021
  5. Stratton19 commented on May 18, 2021

    @Stratton19
    Author

    To my knowledge, it has never worked.

  6. added a commit that references this issue on May 19, 2021
    81a9464
  7. added a commit that references this issue on Jun 13, 2021
    791052f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions