python matplotlib filled boxplots

前端 未结 2 2027
北海茫月
北海茫月 2020-12-14 23:12

Does anyone know if we can plot filled boxplots in python matplotlib? I\'ve checked http://matplotlib.org/api/pyplot_api.html but I couldn\'t find useful information about t

2条回答
  •  一整个雨季
    2020-12-14 23:27

    The example that @Fenikso shows an example of doing this, but it actually does it in a sub-optimal way.

    Basically, you want to pass patch_artist=True to boxplot.

    As a quick example:

    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

提交回复
热议问题