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