2

我有这个马赛克:

from statsmodels.graphics.mosaicplot import mosaic

mosaic_data = pd.DataFrame({'gender': labeled_gender, 'y': labeled_y})
mosaic(mosaic_data, ['gender','y'], title='Mosaic of Heart Disease Vs. Gender', ax=ax, properties={})

简单马赛克

我只是想将调色板更改为我选择的颜色/调色板。有没有办法做到这一点?还有一种方法可以达到和更改绘图的其他属性,例如矩形内标签的颜色?

4

1 回答 1

1

要更改颜色,您需要提供与名称匹配的映射。不要以为可以轻易改变标签颜色:

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

fig, ax = plt.subplots(1, 1,figsize=(6,6))
gender = np.repeat(['male','female'],30)
heart_disease = [np.random.choice(['heart disease','no heart disease'],30,p=[0.8,0.2]),
                 np.random.choice(['heart disease','no heart disease'],30,p=[0.5,0.5])]
data = pd.DataFrame({'gender': gender, 'heart disease': np.array(heart_disease).flatten()})
cols = {('male', 'heart disease'):'#9a1f40',('male', 'no heart disease'):'#d9455f',
        ('female','heart disease' ):'#74d4c0', ('female', 'no heart disease'):'#def4f0'}
x = mosaic(data,['gender','heart disease'], 
           properties = lambda key: {'color': cols[key]} ,
           ax=ax,gap=0.01)

在此处输入图像描述

于 2020-05-21T00:52:18.703 回答