In [28]:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
%matplotlib inline
In [119]:
def boxwhisker_2d(axis, x, y, cutoffs=[1, 25, 75, 99], **kwargs):
right = np.percentile(x, cutoffs[2])
left = np.percentile(x, cutoffs[1])
mid_x = np.median(x)
whisker_right = np.percentile(x, cutoffs[3])
whisker_left = np.percentile(x, cutoffs[0])
top = np.percentile(y, cutoffs[2])
bottom = np.percentile(y, cutoffs[1])
mid_y = np.median(y)
whisker_top = np.percentile(y, cutoffs[3])
whisker_bottom = np.percentile(y, cutoffs[0])
axis.add_patch(patches.Rectangle((left, bottom), right-left, top-bottom, **kwargs))
axis.errorbar(mid_x, mid_y, xerr=[[mid_x-whisker_left, whisker_right-mid_x]],
yerr=[[mid_y-whisker_bottom, whisker_top-mid_y]],
marker=None, linewidth=2, color='black', zorder=100, capsize=5)
In [120]:
x1 = np.random.normal(0, 10, 1000)
y1 = np.random.normal(20, 30, 1000)
x2 = np.random.normal(-20, 10, 1000)
y2 = np.random.normal(-30, 30, 1000)
fig, axis = plt.subplots(1, figsize=(10, 10))
axis.plot(x1, y1, marker='o', linestyle='none', color='#7570b3', alpha = 0.5)
axis.plot(x2, y2, marker='o', linestyle='none', color='#d95f02', alpha = 0.5)
boxwhisker_2d(axis, x1, y1, facecolor='#7570b3', alpha=0.75, zorder=10, linewidth=2)
boxwhisker_2d(axis, x2, y2, facecolor='#d95f02', alpha=0.75, zorder=10, linewidth=2)
#rect.set_facecolor('#7570b3')
#rect.set_alpha(0.75)
#rect.zorder = 10
#axis.add_patch(rect)