Skip to content

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

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

Closed
Stratton19 opened this issue May 18, 2021 · 3 comments · Fixed by #20265
Closed

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

Stratton19 opened this issue May 18, 2021 · 3 comments · Fixed by #20265

Comments

@Stratton19
Copy link

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'.

@jklymak
Copy link
Member

jklymak commented May 18, 2021

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

@jklymak jklymak added this to the v3.5.0 milestone May 18, 2021
@jklymak
Copy link
Member

jklymak commented May 18, 2021

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...

@jklymak jklymak modified the milestones: v3.5.0, v3.4.3 May 18, 2021
@Stratton19
Copy link
Author

To my knowledge, it has never worked.

efiring added a commit to efiring/matplotlib that referenced this issue May 19, 2021
@efiring efiring mentioned this issue May 19, 2021
5 tasks
jklymak pushed a commit to jklymak/matplotlib that referenced this issue Jun 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants