Face pattern for boxes in boxplots

前端 未结 1 777
情深已故
情深已故 2021-01-12 04:24

I would like to do something of the sort (using matplotlib): \"enter

(from Colorfill b

相关标签:
1条回答
  • 2021-01-12 04:47

    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:

    enter image description here

    0 讨论(0)
提交回复
热议问题