Fill Box Color in Box Plot
Asked Answered
E

1

6

I have been trying to fill the boxes of a set of box plots with different colors. See code below.

# Box Plots
fig, axs = plt.subplots(2, 2, figsize = (10,10))
plt.subplots_adjust(hspace = .2,wspace = 0.4)
plt.tick_params(axis='x', which='both', bottom=False)

axs[0,0].boxplot(dfcensus["Median Age"],patch_artist=True)
axs[0,0].set_ylabel('Age', fontsize = '12')
axs[0,0].set_title('Median Age', fontsize = '16')
axs[0,0].get_xaxis().set_visible(False)
axs[0,0].set_facecolor('blue')

axs[0,1].boxplot(dfcensus["% Bachelor Degree or Higher"],patch_artist=True)
axs[0,1].set_ylabel('Percentage', fontsize = '12')
axs[0,1].set_title('% Bachelor Degree or Higher', fontsize = '16')
axs[0,1].get_xaxis().set_visible(False)
axs[0,1].set_facecolor('red')

axs[1,0].boxplot(dfcensus["Median Household Income"],patch_artist=True)
axs[1,0].set_ylabel('Dollars', fontsize = '12')
axs[1,0].set_title('Median Household Income', fontsize = '16')
axs[1,0].get_xaxis().set_visible(False)
axs[1,0].set_facecolor('green')

axs[1,1].boxplot(dfcensus["Median Home Value"],patch_artist=True)
axs[1,1].set_ylabel('Dollars', fontsize = '12')
axs[1,1].set_title('Median Home Value', fontsize = '16')
axs[1,1].get_xaxis().set_visible(False)
axs[1,1].set_facecolor=('orange')

plt.show()

Instead of filling the box in the box plot it fills the frame.

This is a picture of the output

I appreciate the help.

Episcopal answered 22/8, 2020 at 17:39 Comment(0)
G
5

adding a function as add_color will help you to solve your issue. Pls, See the below code.

Note: you can also give different colors to each element in the boxplot such as boxes are green, whiskers are blue etc.

import matplotlib.pyplot as plt

dummy_data = [1,2,3,4]

def add_color(bp, color):
    for element in ['boxes', 'whiskers', 'fliers', 'means', 'medians', 'caps']:
        plt.setp(bp[element], color=color)

# Box Plots
fig, axs = plt.subplots(2, 2, figsize = (10,10))
plt.subplots_adjust(hspace = .2,wspace = 0.4)
plt.tick_params(axis='x', which='both', bottom=False)

bp_1 = axs[0,0].boxplot(dummy_data,patch_artist=True)
axs[0,0].set_ylabel('Age', fontsize = '12')
axs[0,0].set_title('Median Age', fontsize = '16')
axs[0,0].get_xaxis().set_visible(False)
add_color(bp_1, "blue")

bp_2 = axs[0,1].boxplot(dummy_data, patch_artist=True)
axs[0,1].set_ylabel('Percentage', fontsize = '12')
axs[0,1].set_title('% Bachelor Degree or Higher', fontsize = '16')
axs[0,1].get_xaxis().set_visible(False)
add_color(bp_2, "red")

bp_3 = axs[1,0].boxplot(dummy_data, patch_artist=True)
axs[1,0].set_ylabel('Dollars', fontsize = '12')
axs[1,0].set_title('Median Household Income', fontsize = '16')
axs[1,0].get_xaxis().set_visible(False)
add_color(bp_3, "green")

bp_4 = axs[1,1].boxplot(dummy_data,patch_artist=True)
axs[1,1].set_ylabel('Dollars', fontsize = '12')
axs[1,1].set_title('Median Home Value', fontsize = '16')
axs[1,1].get_xaxis().set_visible(False)
add_color(bp_4, "orange")

plt.show()

Plot : enter image description here

Graehme answered 22/8, 2020 at 18:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.