Monday, March 24, 2014

color the boxplot in python matplotlib

This is how you could color your plot in python using matplotlib library.. Basically, we should pass patch_artist=True to boxplot:

import matplotlib.pyplot as plt
import numpy as np

data = [np.random.normal(0, std, 1000) for std in range(1, 6)]
plt.boxplot(data, notch=True, patch_artist=True)

plt.show()

enter image description here

If you'd like to control the color, do something similar to this:
import matplotlib.pyplot as plt
import numpy as np

data = [np.random.normal(0, std, 1000) for std in range(1, 6)]

box = plt.boxplot(data, notch=True, patch_artist=True)

colors = ['cyan', 'lightblue', 'lightgreen', 'tan', 'pink']
for patch, color in zip(box['boxes'], colors):
    patch.set_facecolor(color)

plt.show()

enter image description here

1 comment: