0

I have a report coming up, so I want to make the boxplot more appealing. I have the code running, and I chose the colors( as indicated). However, the colors don't reflect on the plot figure and I also tried to put the "color = colors" inside the boxplot but it doesn't work.

Here is the code and figure:

import matplotlib.pyplot as plt

# select columns to plot
columns_to_plot = ['Customer_Age', 'Months_on_book', 'Credit_Limit', 'Total_Revolving_Bal', 'Avg_Open_To_Buy', 'Total_Trans_Amt', 'Total_Trans_Ct']

# create box plot
boxprops = {'linewidth': 2}
medianprops = {'linewidth': 2, 'color': 'orange'}
whiskerprops = {'linewidth': 2}
flierprops = {'marker': 'o', 'markersize': 5, 'alpha': 0.5}
capprops = {'linewidth': 2}

fig, ax = plt.subplots(figsize=(12,8))

colors = ['lightblue', 'lightgreen', 'lavender', 'pink', 'lightyellow', 'lightcoral', 'lightsalmon']

bp = BankChurners[columns_to_plot].boxplot(ax=ax, patch_artist=True, boxprops=boxprops, medianprops=medianprops, whiskerprops=whiskerprops, flierprops=flierprops, capprops=capprops)



# add title and labels
plt.title('Box Plot of Customer Data', fontsize=16)
plt.ylabel('Value', fontsize=14)
plt.xticks(rotation=45, fontsize=12)


# add gridlines
plt.grid(axis='y', linestyle='--', alpha=0.7)


# show plot
plt.show()

enter image description here

Riddle2795
  • 49
  • 6

1 Answers1

0

Please have a look at the responses to this similar question. But in short;

Assuming BankChurners is a Pandas DataFrame, you can set the colors like:

...

colors = dict(boxes='r', whiskers='r', medians='r', caps='r')

bp = BankChurners[columns_to_plot].boxplot(ax=ax, patch_artist=True, boxprops=boxprops, medianprops=medianprops, whiskerprops=whiskerprops, flierprops=flierprops, capprops=capprops, color=colors)

You need to provide the colors as a dict and not as a list.

Gosh
  • 13
  • 4