Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add image annotations to boxplot

I would like to add image annotations to a boxplot, akin to what they did with the bar chart in this post: How can I add images to bars in axes (matplotlib)

My dataframe looks like this:

import pandas as pd
import numpy as np

names = ['PersonA', 'PersonB', 'PersonC', 'PersonD','PersonE','PersonF']
regions = ['NorthEast','NorthWest','SouthEast','SouthWest']
dates = pd.date_range(start = '2021-05-28', end = '2021-08-23', freq = 'D')

df = pd.DataFrame({'runtime': np.repeat(dates, len(names))})
df['name'] = len(dates)*names
df['A'] = 40 + 20*np.random.random(len(df))
df['B'] = .1 * np.random.random(len(df))
df['C'] = 1 +.5 * np.random.random(len(df))
df['region'] = np.resize(regions,len(df))

I tried to use the AnnotationBbox method which worked great for my time-series, but I'm not entirely sure if it can be applied here.

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
from matplotlib.cbook import get_sample_data

fig, ax = plt.subplots(
df.boxplot(column='A', by=['name'],ax=ax,showmeans=True, fontsize=8, grid=False)

for name in names:
  rslt_df = df[df['name']==name] 
  val = rslt_df['A'].values[0]
  xy = (0, val)

  fn = get_sample_data(f"{name}.png", asfileobj=False)
  arr_img = plt.imread(fn, format='png')
  imagebox = OffsetImage(arr_img, zoom=0.125)
  imagebox.image.axes = ax

  ab = AnnotationBbox(imagebox, xy,xybox=(15.,0),xycoords='data',boxcoords="offset points",pad=0,frameon=False)
  ax.add_artist(ab)

PersonA.png PersonB.png PersonC.png

like image 965
p3hndrx Avatar asked May 19 '26 06:05

p3hndrx


1 Answers

  • The code in the OP if very similar to Add image annotations to bar plots axis tick labels, but needs to be modified because boxplots are slightly different the barplots.
  • The main issue was xy didn't have the correct values.
    • The xy and xybox parameters can be adjusted to place the images anywhere.
  • By default, boxplot positions the ticks at range(1, n+1), as explained in this answer
    • Reset the tick positions with a 0 index: positions=range(len(names))
  • df was created with names = ['PersonA', 'PersonB', 'PersonC'] since only 3 images were provided.
ax = df.boxplot(column='A', by=['name'], showmeans=True, fontsize=8, grid=False, positions=range(len(names)))
ax.set(xlabel=None, title=None)

# move the xtick labels
ax.set_xticks(range(len(names)))
ax.set_xticklabels(countries)
ax.tick_params(axis='x', which='major', pad=30)

# use the ytick values to locate the image
y = ax.get_yticks()[1]

for i, (name, data) in enumerate(df.groupby('name')):

    xy = (i, y)

    fn = f"data/so_data/2021-08-28/{name}.png"  # path to file
    arr_img = plt.imread(fn, format='png')
    imagebox = OffsetImage(arr_img, zoom=0.125)
    imagebox.image.axes = ax

    ab = AnnotationBbox(imagebox, xy, xybox=(0, -30), xycoords='data', boxcoords="offset points", pad=0, frameon=False)
    ax.add_artist(ab)

enter image description here

like image 111
Trenton McKinney Avatar answered May 20 '26 19:05

Trenton McKinney



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!