Skip to content
Advertisement

Matplotlib boxplot visual styles: `whiskerprops` does not work

In the documentation of matplotlib’s boxplot we can read:

whiskerprops : dict or None (default) If provided, will set the plotting style of the whiskers

Ok, so I passed a dict to set some visual styles on the whiskers:

whiskerprops = {'ls': 'solid', 'lw': 0.5, 'color': '#777777'}
boxplot(..., whiskerprops = whiskerprops)

The settings have no effect, except the color.

Same behaviour could be observed at the other props: capprops, medianprops, boxprops, etc.

Later I found the reason and I will post it in an answer. I am doing this only because others might face with the same issue, and the docs and tutorials do not answer this.

Advertisement

Answer

When you pass a dict to set any of these properties, matplotlib will add elements to your dictionary, avoiding only to overwrite the existing keys. But it is not aware that there are abbreviations for some of the properties: if you have ls, it will add 'linestyle': '--', if you have lw, it will add 'linewidth': 1.0, and so on. This has 2 implications: 1) you can not use the shorthand property names here, only the long names; 2) your dict will be modified as a side effect of calling boxplot. Matplotlib does not make a copy internally, as it should in my opinion. So if you want to keep the original dict, you need to copy.deepcopy(props).

It worths to mention, that these settings are arguments for matplotlib.lines.Line2D, see its docs for other properties available.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement