Face pattern for boxes in boxplots

风格不统一 提交于 2019-12-01 05:58:21

The important aspect is to set patch_artist=True when calling boxplot.

import numpy as np
import matplotlib.pyplot as plt

# fake up some data
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), 0)

# basic plot
bp = plt.boxplot(data, patch_artist=True)

for box in bp['boxes']:
    # change outline color
    box.set(color='red', linewidth=2)
    # change fill color
    box.set(facecolor = 'green' )
    # change hatch
    box.set(hatch = '/')

plt.show()

The basic plot example is taken from the boxplot demo. However, none of those examples set patch_artist=True. If that statement is omitted, you will get this error:

AttributeError: 'Line2D' object has no attribute 'set_facecolor'

The boxplot demo 2 shows in great detail, how rectangles can be fitted to the boxplot in order to obtain coloring. This blog points to the option of the patch_artist.
For more ideas about hatches, refer to the hatch demo. The example above produces this figure:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!