改变马赛克p的默认颜色

2024-05-08 11:47:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我想改变这个马赛克图的颜色,使它可以在黑白中打印,但找不到方法来改变这个参数

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

这是我实际拥有的:我看到我可以用马赛克(属性)在这个链接上改变颜色:http://www.statsmodels.org/stable/generated/statsmodels.graphics.mosaicplot.mosaic.html 但我只能给出两种不同的颜色,我需要为每个情节使用不同的颜色,比如: enter image description here


Tags: 方法nofromimportpandasdata参数matplotlib
1条回答
网友
1楼 · 发布于 2024-05-08 11:47:05

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

因此,您可以传递一个函数(参见上面链接中的示例),或者更简单地说是一个字典,来改变矩形的外观:

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)

enter image description here

据我所知,any argument accepted by ^{}可以在这本词典中流传。在

相关问题 更多 >