I am using the example from the docs (with a slightly modified data structure):
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
data = pd.DataFrame({"peaches":[1,4,7,5,8,2,5,7], "oranges":[2,7,9,1,9,2,7,3], "tomato":[3,8,5,4,5,6,7,1]})
labels = ['peaches', 'oranges', 'tomatoes']
colors = ['peachpuff', 'orange', 'tomato']
fig, ax = plt.subplots()
ax.set_ylabel('fruit weight (g)')
bplot = ax.boxplot(data,
patch_artist=True, # fill with color
tick_labels=labels) # will be used to label x-ticks
plt.show()
What I need to do is color the top part of each box (I.E. median to Q3) with one shade of blue, and the bottom part (median to Q2) with a deeper shade of blue (ignore the colors given in the example).
I think this is achieved by iterating through and adding a rectangle using ax.add.patch(plt.Rectangle).
But as a relative beginner in this area I can't work out how to iterate through the data and the boxplot. Please can anyone assist?
I am using the example from the docs (with a slightly modified data structure):
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
data = pd.DataFrame({"peaches":[1,4,7,5,8,2,5,7], "oranges":[2,7,9,1,9,2,7,3], "tomato":[3,8,5,4,5,6,7,1]})
labels = ['peaches', 'oranges', 'tomatoes']
colors = ['peachpuff', 'orange', 'tomato']
fig, ax = plt.subplots()
ax.set_ylabel('fruit weight (g)')
bplot = ax.boxplot(data,
patch_artist=True, # fill with color
tick_labels=labels) # will be used to label x-ticks
plt.show()
What I need to do is color the top part of each box (I.E. median to Q3) with one shade of blue, and the bottom part (median to Q2) with a deeper shade of blue (ignore the colors given in the example).
I think this is achieved by iterating through and adding a rectangle using ax.add.patch(plt.Rectangle).
But as a relative beginner in this area I can't work out how to iterate through the data and the boxplot. Please can anyone assist?
Share Improve this question asked Mar 12 at 16:18 BaldyBaldy 575 bronze badges1 Answer
Reset to default 1You can try something like the following:
Create a boxplot of known x axis coordinates, and width
Find the quartile ranges of the boxplot (gives you the y coordinates and height of the boxplot)
Add a rectangle of dimensions of the boxplot
import matplotlib.pyplot as plt
import matplotlib.patches as patches
# Create Dataset
data = pd.DataFrame({"peaches":[1,4,7,5,8,2,5,7], "oranges":[2,7,9,1,9,2,7,3], "tomato":[3,8,5,4,5,6,7,1]})
labels = ['peaches', 'oranges', 'tomatoes']
# Colors for the boxplot
color = ['skyblue', 'blue']
#x axis position for the boxplot
positions = [2, 5, 8]
#Width of the boxplots
width = [0.2] * data.columns.size
fig, ax = plt.subplots(1)
bplot = ax.boxplot(data,
patch_artist=False,
tick_labels=labels, widths=width, positions=positions)
# Add color to boxplots
for i, col in enumerate(data.columns):
q75, q50, q25 = np.percentile(data[col], [75, 50 ,25])
iqr = q75 - q25
median = q50
# Create a rectangle patch - offset by -width[i]/2 to overlay inside border
rect1 = patches.Rectangle((positions[i] -width[i]/2 , median), width[i], q25-median, facecolor=color[0], alpha=0.5)
rect2 = patches.Rectangle((positions[i] -width[i]/2, median), width[i], q75-median, facecolor=color[1], alpha=0.5)
# Add the rectangle to the axes
ax.add_patch(rect2)
ax.add_patch(rect1)
ax.set_xlim(np.min(positions) - np.min(positions)/2 , np.max(positions) + np.min(positions)/2)
# Display the plot
plt.ylabel('fruit weight (g)')
plt.show()