2

I have this mosaic:

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

Simple Mosaic

I simply want to change the color palette to the colors / palette of my choice. Is there a way to do that? Also is there a way to reach & change other properties of the plot, for example color of the labels inside the rectangles?

G. M.
  • 33
  • 3
  • `properties={'color' : 'b'}` for instance should do the trick? [docs](https://www.statsmodels.org/stable/generated/statsmodels.graphics.mosaicplot.mosaic.html) – Torxed May 09 '20 at 22:44
  • Unfortunately that doesn't work, for some reason it doesn't change any color – G. M. May 10 '20 at 08:03

1 Answers1

1

To change the color you need to provide a mapping that matches the names. Don't think you can change the label color easily:

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)

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72