【问题标题】:Face pattern for boxes in boxplots箱线图中框的面型
【发布时间】:2015-04-28 16:46:41
【问题描述】:

我想做一些类似的事情(使用 matplotlib):

(来自Colorfill boxplot in R-cran with lines, dots, or similar

我看到了一些关于孵化的信息?但我真的无法对如何使用它做出正面或反面。

我还发现自己想知道如何更改参数,例如 boxprop dict 的可能属性——在 plt.boxplot(..., boxprops=boxpropsdict) 中使用。是否有可能只列出所有可能的属性?

【问题讨论】:

    标签: python matplotlib boxplot


    【解决方案1】:

    重要的方面是在调用boxplot时设置patch_artist=True

    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()
    

    基本情节示例取自boxplot demo。但是,这些示例都没有设置patch_artist=True。如果省略该语句,您将收到此错误:

    AttributeError: 'Line2D' 对象没有属性 'set_facecolor'

    boxplot demo 2 非常详细地展示了如何将矩形拟合到箱线图以获得着色。 This blog 指向patch_artist 的选项。
    有关影线的更多想法,请参阅hatch demo。上面的例子产生了这个图:

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2017-03-20
      • 2017-01-10
      • 1970-01-01
      • 2022-01-27
      • 1970-01-01
      • 2016-10-30
      相关资源
      最近更新 更多