在使用 boxplot 时使用 matplotlib 时无法使用属性 facecolor

Not able to use the attribute facecolor while using matplotlib while using boxplot

import numpy as np
import pandas as pd
import os
import sys
import csv
import matplotlib.pyplot as plt

fifa = pd.read_csv(r"H:\matplotlib with pandas\fifa_data.csv")
barcelona = fifa.loc[fifa.Club == 'FC Barcelona']['Overall']
plt.figure(figsize=(5, 8))
madrid = fifa.loc[fifa.Club == 'Real Madrid']['Overall']
revs = fifa.loc[fifa.Club == 'New England Revolution']['Overall']
labels = ['FC Barcelona', 'Real Madrid', 'NE Revolution']
boxes = plt.boxplot([barcelona, madrid, revs], labels=labels)

for box in boxes['boxes']:
    box.set(color='#4243f5', linewidth=2)
    # fill color in boxes.
    box.set(facecolor='#abcdef')

plt.ylabel("FIFA Overall Ratings")
plt.title("Comparison of barcelona and real madrid team stats.")
plt.show()

edge color 属性有效,但是后面的facecolor 报错:

AttributeError: 'Line2D' object has no property 'facecolor'

facecolor输出之前是:

你错过了一件重要的事情。

当你想改变方框的颜色时,你必须指定:

boxes = ax.boxplot(x, patch_artist=True)

我检查了以下代码,它有效:

import matplotlib.pyplot as plt
%matplotlib inline

x = [[1, 2, 3], [4, 5, 6]]

fig = plt.figure()
ax = fig.add_subplot(111)
boxes = ax.boxplot(x, patch_artist=True)

for box in boxes["boxes"]:
    box.set(facecolor = "green")