python 散点图图例边框颜色拒绝更改

python scatter plot legend border color refuses to change

此代码可能对其他在 matplotlib 中为 python 努力绘制散点图的人有用。除了这个问题外,它都有效。我已经在此站点和其他地方提供了许多帮助主题。根据我的代码中的评论,有多种方法可以在图例的边框上设置 edgecolor,但其中 none 对我有用。 Python 总是抛出一个错误,表明我正在尝试设置不存在的内容。

谁能看到这里的修复程序是什么?

%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

这两种方法都适用于 matplotlib 2.0。使用 edgecolor 参数

plt.legend(edgecolor="limegreen")

或者设置帧的属性

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

早期版本没有这些选项。