1

This code might be useful to others struggling w/ scatter plots in matplotlib for python. It works except for this one problem. I have poured over many help topics on this site and elsewhere. As per comments in my code, there are different ways to set the edgecolor on the border of the legend and yet none of them are working for me. Python always throws an error indicating I am trying to set something that does not exist.

Can anyone see what the fix is here?

%matplotlib inline
import matplotlib.pyplot as plt

stLen = iris[Species=='setosa']['Petal.Length']
stWid = iris[Species=='setosa']['Petal.Width']

vsLen = iris[Species=='versicolor']['Petal.Length']
vsWid = iris[Species=='versicolor']['Petal.Width']

viLen = iris[Species=='virginica']['Petal.Length']
viWid = iris[Species=='virginica']['Petal.Width']

plt.rcParams['figure.figsize'] = 8, 6
plt.rc('axes',edgecolor='black')

fntsz = 12  # global font size adjustment

sctplt1 = plt.scatter(stLen, stWid, c='blue',   alpha=0.8) 
sctplt2 = plt.scatter(vsLen, vsWid, c='red',    alpha=0.8)  
sctplt3 = plt.scatter(viLen, viWid, c='purple', alpha=0.8)

plt.legend((sctplt1, sctplt2, sctplt3),
           ('setosa', 'versicolor', 'virginica'),
           scatterpoints=3,
           loc='upper left',
           ncol=1,
           fontsize=10, frameon=True).get_frame().set_edgecolor('black')
                        # help claimed edgecolor should be legend() argument but errors say otherwise
                        # .getframe().set_edgecolor() was supposed to do it but its not working

plt.title('Iris', fontsize = fntsz)     # defaults to center ... to change this:  plt.title('title', loc='right' | 'left')
plt.xlabel('Petal Length', fontsize = fntsz)
plt.ylabel('Petal Width', fontsize = fntsz)

# turn off grid lines:
plt.grid(b=False)

# to save to a file
# fig.savefig('test.jpg')

plt.show
undercovertek
  • 148
  • 1
  • 11

1 Answers1

2

Both methods work in matplotlib 2.0. Either use the edgecolor argument

plt.legend(edgecolor="limegreen")

or set the property of the frame

legend = plt.legend()
legend.get_frame().set_edgecolor("limegreen")

Earlier versions don't have those options.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • I am using Python 2.7. I installed it and matplotlib by September of 2016 and I get errors on both commands that the attribute does not exist. Just checked conda list (I used Anaconda) and it looks like matplotlib is 1.5.3. I will look into whether it is possible to upgrade to 2.0 and if so I can then test your solution. – undercovertek Mar 31 '17 at 14:25
  • solution worked. upgrading numpy upgraded a slew of inter-dependant things including matplotlib and pandas. – undercovertek Mar 31 '17 at 14:57
  • Anaconda 4.2 installed old versions of many things on my system around that same time frame. I had a thread going under issues on github with them. Hopefully they have cleaned this up in Anaconda 4.3. They were looking into why many other pakcages were out of date on the 4.2 release after I reported several problems there. – TMWP Mar 31 '17 at 15:00