【问题标题】:Choosing Another Color Palette for a Mosaic Plot为马赛克图选择另一个调色板
【发布时间】:2020-05-09 22:37:58
【问题描述】:

我有这个马赛克:

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={})

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

【问题讨论】:

  • properties={'color' : 'b'} 例如应该做的伎俩? docs
  • 不幸的是这不起作用,由于某种原因它不会改变任何颜色

标签: python statsmodels mosaic-plot


【解决方案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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-24
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    相关资源
    最近更新 更多