Reputation: 51
Is there any way to label the outliers in a box plot. like i am plotting the prices for each drug and trying to find places with overpriced drug. so i want to label the outliers with the name of the place from where it belong. How to achieve it using matplotlib ?
Upvotes: 0
Views: 140
Reputation: 4265
Boxplot lets you pass an object for flierprops
.
import random
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Mock data from the boxplot demo
spread = np.random.rand(50) * 100
center = np.ones(25) * 50
flier_high = np.random.rand(10) * 100 + 100
flier_low = np.random.rand(10) * -100
data = np.concatenate((spread, center, flier_high, flier_low))
# Set up
fig, ax = plt.subplots()
# flierprops example
red_square = dict(markerfacecolor='r', marker='s')
box = ax.boxplot(data, flierprops=red_square)
This simple sample produces:
If you want to label something, you can use plt.annotate
like so:
box = ax.boxplot(data,)
top_points = box["fliers"][0].get_data()
ax.scatter(top_points[0], top_points[1], marker="o")
# Roughly based on https://stackoverflow.com/a/5147430/10553976
ax.annotate("I labeled this", xy=(top_points[0][1], top_points[1][1]),
xytext=(-20, 20),
textcoords='offset points', ha='right', va='bottom',
bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
arrowprops=dict(arrowstyle = '->', connectionstyle='arc3,rad=0'))
And this sample produces:
Upvotes: 1