python boxplot legend

Solutions on MaxInterview for python boxplot legend by the best coders in the world

showing results for - "python boxplot legend"
Missie
27 May 2019
1import matplotlib.pyplot as plt
2import numpy as np; np.random.seed(1)
3
4data1=np.random.randn(40,2)
5data2=np.random.randn(30,2)
6
7fig, ax = plt.subplots()
8bp1 = ax.boxplot(data1, positions=[1,4], notch=True, widths=0.35, 
9                 patch_artist=True, boxprops=dict(facecolor="C0"))
10bp2 = ax.boxplot(data2, positions=[2,5], notch=True, widths=0.35, 
11                 patch_artist=True, boxprops=dict(facecolor="C2"))
12
13ax.legend([bp1["boxes"][0], bp2["boxes"][0]], ['A', 'B'], loc='upper right')
14
15ax.set_xlim(0,6)
16plt.show()