更改马赛克图的默认颜色

Change the default colors of a mosaic plot

我想更改此马赛克图的颜色,使其可以黑白打印,但找不到更改此参数的方法

from statsmodels.graphics.mosaicplot import mosaic
import matplotlib.pyplot as plt
import pandas

x = ['yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes']
y = ['yes', 'yes', 'yes', 'yes', 'no', 'no', 'no']
data = pandas.DataFrame({'x': x, 'y': y})
mosaic(data, ['x', 'y'])

plt.savefig("mosaicplot.pdf", figsize=[10,5])
plt.show()

这是我实际拥有的:我看到我可以在此 link 上使用马赛克(属性)更改颜色:http://www.statsmodels.org/stable/generated/statsmodels.graphics.mosaicplot.mosaic.html 但我只能给出 2 种不同的颜色,我需要为每个图使用不同的颜色,如下所示:

The documentation 提到了一个 properties= 论点:

properties function (key) -> dict, optional

A function that for each tile in the mosaic take the key of the tile and returns the dictionary of properties of the generated Rectangle, like color, hatch or similar. A default properties set will be provided fot the keys whose color has not been defined, and will use color variation to help visually separates the various categories. It should return None to indicate that it should use the default property for the tile. A dictionary of the properties for each key can be passed, and it will be internally converted to the correct function

因此,您可以将函数(参见上面 link 中的示例)或更简单的字典传递给 properties= 以更改矩形的外观:

x = ['yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes']
y = ['yes', 'yes', 'yes', 'yes', 'no', 'no', 'no']
data = pandas.DataFrame({'x': x, 'y': y})

props = {}
props[('yes', 'yes')] = {'color': 'xkcd:orange'}
props[('yes','no')] = {'facecolor': 'xkcd:pale blue',
                       'edgecolor':'xkcd:light grey',
                       'hatch':'o'}
data = pandas.DataFrame({'x': x, 'y': y})
mosaic(data, ['x', 'y'], properties=props)

据我所知,any argument accepted by Rectangle可以在这本词典中传递。