gyli / PyWaffle

🧇 Make Waffle Charts in Python.
MIT License
578 stars 105 forks source link

missing corner squares with automatic scaling #21

Closed mattdm closed 3 years ago

mattdm commented 3 years ago

I'm making 50×20 grids, so each block is 0.1%. I've found that with some data, the top right corner ends up empty. Here's a real-world example:

#!/usr/bin/python3

import matplotlib as m
m.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.dates as dates
from pywaffle import Waffle

m.style.use('seaborn-whitegrid')
m.rcParams['font.size'] = 12

fig = plt.figure( 
FigureClass=Waffle, 
 rows=20,
 columns=50,
 values={'32': 192,'33': 76,'34':59},
 figsize=[16, 9],
)              

fig.savefig('test.png',dpi=300)

plt.close()   

This results in:

test

... with an empty square in the top right -- a total of 999 squares instead of 1000.

I assume this is because all values are getting rounded down.

gyli commented 3 years ago

You are right, the numbers are rounded.

Two options here:

  1. Adding a new category like "Others" or something, to make sure the sum of all values 1000 or any multiples of 1000
  2. Adding rounding_rule='ceil' when calling figure. Here are more details https://pywaffle.readthedocs.io/en/latest/examples/value_scaling_and_auto_sizing.html?highlight=rounding_rule
mattdm commented 3 years ago

Ah, perfect. rounding_rule='ceil' is perfect for my purposes. Thanks!

mattdm commented 3 years ago

Result, for completeness:

test