0

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?

1 Answer 1

1

You 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()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.