1

I'd like to create a waffle chart in grey shapes for the following dataFrame

data = pd.DataFrame({'Category': ['a', 'b', 'c', 'd'], 'no_occurrence' : [594, 5, 10, 9]})

Here is what I've done so far based on this post

import matplotlib.pyplot as plt 
from pywaffle import Waffle
fig = plt.figure(
    FigureClass=Waffle, 
    rows=5,
    colors = ('lightgrey', 'black', 'darkgrey', 'lightgrey'),
    values=list(data['no_occurrence']/4),
    labels=list(data['Category']),
    icons = 'sticky-note', 
    icon_size = 11,
    figsize=(12, 8),
    icon_legend = True,
    legend={'loc': 'lower left','bbox_to_anchor': (0, -0.4), 'ncol': len(data), 'fontsize': 8}    
)

As the grey shapes are quite difficult to distinguish, I'd like to hatch the last category (in the figure and the legend) but I can't figure out how to add the hatches in the colors. I have a bar chart with the same categories, where I added hatches, so I'd like to stay consistent.

TiTo
  • 833
  • 2
  • 7
  • 28

1 Answers1

2

When using icons as the elements in a waffle chart, internally they are represented as text objects with a special font.

Using patheffects, hatching can be added, both to the icons in the plot as in the legend.

As you didn't provide toy data, nor an image, I made up some data to show the ideas. As my legend icons are smaller than the icons in the plot, I used a denser hatching for the legend.

import matplotlib.pyplot as plt
from matplotlib import patheffects
import numpy as np
from pywaffle import Waffle

values = [5, 14, 17, 18]
fig = plt.figure(
    FigureClass=Waffle,
    rows=5,
    colors=('lightgrey', 'black', 'darkgrey', 'lightgrey'),
    values=values,
    labels=[*'abcd'],
    icons='sticky-note',
    icon_size=60,
    figsize=(12, 8),
    icon_legend=True,
    legend={'loc': 'lower left', 'bbox_to_anchor': (0, -0.4), 'ncol': 4, 'fontsize': 15,
            'facecolor': 'white', 'edgecolor': 'black'})
for t in fig.ax.texts[-values[-1]:]:
    t.set_path_effects([patheffects.PathPatchEffect(hatch='xxx', fc='lightgrey', ec='white')])  # color='lightgrey')])
fig.ax.legend_.legendHandles[-1].set_path_effects(
    [patheffects.PathPatchEffect(hatch='xxxxx', fc='lightgrey', ec='white')])
fig.tight_layout()
plt.show()

example waffle chart

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Thank you @JohanC! I was not aware of `patheffects`. That solved my problem. The data was hidden in the dataFrame. I could have made it more explicit, sorry for that. – TiTo Mar 02 '21 at 12:25