Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill Box Color in Box Plot

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.

like image 965
LDLeeHoue Avatar asked Aug 22 '20 17:08

LDLeeHoue


People also ask

How to change the color of a box plot in Matplotlib?

You can change the color of a box plot using setp on the returned value from boxplot (). This example defines a box_plot () function that allows the edge and fill colors to be specified: import matplotlib.pyplot as plt def box_plot (data, edge_color, fill_color): bp = ax.boxplot (data, patch_artist=True) for element in ['boxes', 'whiskers', ...

How do you fill a box plot with the same color?

The outliers will have the same black color, so write the command inside the geom_boxplot ( ) only. Here, we will use the keyword fill. Since we need the same color in the fill of boxplots, we will write the command inside the geom_boxplot ( ). By default, fill for outliers is black.

How to add color to outliers in the plot?

Outliers are observations that are located outside the whiskers of a box plot. We will keep the default black color for them. Use the command outlier.color to add color to the outliers in the plot.

Is it possible to plot multiple box plots on the same graph?

The notion was to plot multiple box plots on the same graph and have each box plot be darker or lighter based on its distance from the overall median in the dataset. The issue here is that Plotly only supports color scales for heatmaps, scatter plots & contour plots.


1 Answers

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

like image 176
Hasan Salim Kanmaz Avatar answered Oct 21 '22 12:10

Hasan Salim Kanmaz