Matplotlib 箱线图视觉样式:`whiskerprops` 不起作用
Matplotlib boxplot visual styles: `whiskerprops` does not work
在 matplotlib 的 boxplot
文档中我们可以读到:
whiskerprops : dict or None (default)
If provided, will set the plotting style of the whiskers
好的,所以我通过一个字典来设置胡须上的一些视觉样式:
whiskerprops = {'ls': 'solid', 'lw': 0.5, 'color': '#777777'}
boxplot(..., whiskerprops = whiskerprops)
除 color
外,其他设置均无效。
可以在其他道具上观察到相同的行为:capprops
、medianprops
、boxprops
等
后来我找到了原因,我会post在回答中。我这样做只是因为其他人可能会遇到同样的问题,而文档和教程没有回答这个问题。
当您传递字典来设置这些属性中的任何一个时,matplotlib 会将元素添加到您的字典中,避免只覆盖现有键。但它不知道有些属性有缩写:如果你有ls
,它会添加'linestyle': '--'
,如果你有lw
,它会添加'linewidth': 1.0
, 等等。这有两个含义:1)你不能在这里使用 shorthand 属性 名称,只能使用长名称; 2) 你的字典将被修改为调用 boxplot
的副作用。 Matplotlib 不会在内部制作副本,我认为它应该这样做。所以如果你想保留原来的dict,你需要copy.deepcopy(props)
.
值得一提的是,这些设置是 matplotlib.lines.Line2D
的参数,请参阅其文档了解其他可用属性。
在 matplotlib 的 boxplot
文档中我们可以读到:
whiskerprops : dict or None (default) If provided, will set the plotting style of the whiskers
好的,所以我通过一个字典来设置胡须上的一些视觉样式:
whiskerprops = {'ls': 'solid', 'lw': 0.5, 'color': '#777777'}
boxplot(..., whiskerprops = whiskerprops)
除 color
外,其他设置均无效。
可以在其他道具上观察到相同的行为:capprops
、medianprops
、boxprops
等
后来我找到了原因,我会post在回答中。我这样做只是因为其他人可能会遇到同样的问题,而文档和教程没有回答这个问题。
当您传递字典来设置这些属性中的任何一个时,matplotlib 会将元素添加到您的字典中,避免只覆盖现有键。但它不知道有些属性有缩写:如果你有ls
,它会添加'linestyle': '--'
,如果你有lw
,它会添加'linewidth': 1.0
, 等等。这有两个含义:1)你不能在这里使用 shorthand 属性 名称,只能使用长名称; 2) 你的字典将被修改为调用 boxplot
的副作用。 Matplotlib 不会在内部制作副本,我认为它应该这样做。所以如果你想保留原来的dict,你需要copy.deepcopy(props)
.
值得一提的是,这些设置是 matplotlib.lines.Line2D
的参数,请参阅其文档了解其他可用属性。