python boxplot legend 1

python boxplot legend

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)


fig, ax = plt.subplots()
bp1 = ax.boxplot(data1, positions=[1,4], notch=True, widths=0.35, 
                 patch_artist=True, boxprops=dict(facecolor="C0"))
bp2 = ax.boxplot(data2, positions=[2,5], notch=True, widths=0.35, 
                 patch_artist=True, boxprops=dict(facecolor="C2"))

ax.legend([bp1["boxes"][0], bp2["boxes"][0]], ['A', 'B'], loc='upper right')


Here is what the above code is Doing:
1. Create a figure and axes object.
2. Create two boxplots, one for each dataset.
3. Create a legend.
4. Set the x-axis limits.
5. Show the plot.

Similar Posts