当使用matplotlib时,无法在使用boxplot时使用facecolor属性[重复]。

问题描述 投票:0回答:1
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()

边缘颜色的属性是工作的,但是facecolor的后一个属性却给出了一个错误。

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

在facecolor输出之前:

enter image description here

python-3.x pandas numpy matplotlib boxplot
1个回答
1
投票

你缺少了一个关键的东西。

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

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")
© www.soinside.com 2019 - 2024. All rights reserved.