1

I'm having some trouble trying to set the color of legend entries. I want to select the same color as the line they are referring to. Here i post a runable script

import matplotlib.pyplot as plt

x, y = [1,2],[1,2]

fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(x,y,label='test',color='r')
ax.legend(labelcolor='r')

plt.show()

And that's the error I'm getting

Traceback (most recent call last):
  File "test.py", line 11, in <module>
    ax.legend(labelcolor='r')
  File "/home/username/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 406, in legend
    self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'labelcolor'

However I saw in the legend documentation that labelcolor should work as argument. Do you have any suggestions?

Thank you in advance

F. Addari
  • 13
  • 4
  • 1
    check your matplotlib version. This is a new feature in 3.3.0. (https://stackoverflow.com/a/63273370/6361531) – Scott Boston Aug 12 '20 at 16:28
  • Your script works for me with the latest Matplotlib. What version do you get for matplotlib according to this: https://matplotlib.org/3.1.1/faq/troubleshooting_faq.html#obtaining-matplotlib-version – Hans Aug 12 '20 at 16:31
  • Yes, I have matplotlib 3.1.1. I'm now trying to update it – F. Addari Aug 12 '20 at 16:36

1 Answers1

1

Upgrade matplotlib to version 3.3.0

Then try it again.

import matplotlib.pyplot as plt

x, y = [1,2],[1,2]

fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(x,y,label='test',color='r')
ax.legend(title='Guide', labelcolor='red')

plt.show()

enter image description here

Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • Well, I'm trying to update it (I have the conda environment) but It seems I'm not able to. I've tried to use conda update conda conda update --all conda install matplotlib=3.3.0 or even the commands suggested here https://anaconda.org/conda-forge/matplotlib – F. Addari Aug 12 '20 at 16:48
  • You can try to do it using pip.... `pip install -U matplotlib` – Scott Boston Aug 12 '20 at 16:48
  • @F.Addari You're welcome. Happy coding. Be safe and stay healthy! – Scott Boston Aug 12 '20 at 16:55