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.
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', ...
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.
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.
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.
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 :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With