3
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()

The attribute of edge color is working but the latter one of facecolor is giving an error:

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

Before facecolor output is:

enter image description here

DavidG
  • 24,279
  • 14
  • 89
  • 82
mtr_007
  • 59
  • 1
  • 10

1 Answers1

3

You are missing one crucial thing.

When you want to change the color of the box, you must specify:

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

I have checked the following code and it works:

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")