gyli / PyWaffle

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

is it possible to control edge color? #37

Open JosephBARBIERDARNAL opened 1 month ago

JosephBARBIERDARNAL commented 1 month ago

Hello!

Is there a way to control the border of each block? Because if there isn't, I get the impression that it's not possible to create dark, satisfying graphics with pywaffle.

Here is an example (and its output):

import matplotlib.pyplot as plt
import pandas as pd
from pywaffle import Waffle

path = 'https://raw.githubusercontent.com/holtzy/R-graph-gallery/master/DATA/share-cereals.csv'
df = pd.read_csv(path)

def remove_html_tag(s):
    return s.split('</b>')[0][3:]

df['lab'] = df['lab'].apply(remove_html_tag)
df = df[df['type'] == 'feed']

background_color = "#222725"
pink = "#f72585"
black = "#4F0325"

number_of_bars = len(df)  # one bar per continent

# Init the whole figure and axes
fig, axs = plt.subplots(
   nrows=number_of_bars,
   ncols=1,
   figsize=(8, 6)
)
fig.set_facecolor(background_color)
ax.set_facecolor(background_color)

# Iterate over each bar and create it
for (i, row), ax in zip(df.iterrows(), axs):

    share = row['percent']
    values = [share, 100-share]

    Waffle.make_waffle(
        ax=ax,
        rows=4,
        columns=25,
        values=values,
        colors=[pink, black],
        edgecolor='white'
    )

plt.show()
Screenshot 2024-07-16 at 14 25 50
JosephBARBIERDARNAL commented 1 month ago

I'd like to be able to make something like:

Screenshot 2024-07-16 at 14 29 46
gyli commented 1 month ago

Hi @JosephBARBIERDARNAL! I think that's a nice to have feature, let me see if and how it can be aded.

JosephBARBIERDARNAL commented 1 month ago

cool! I'm ready to help if you need it

gyli commented 1 month ago

PR is surely welcomed! While before starting coding, I think it's discussable how to achieve this. At my first glance, I thought we just need to add 4 edges outside of each block. But what would be the default line weight? Seems like the screenshot you attached is a special case, that edges happen to fill the gaps, and I think we can make this as the default option. Then, how can we make the edge weight adjustable? So I can have block with edge and gap at the same time? Probably we can make a new parameter like edge_ratio (the ratio of edge width and gap width), similar to how to control gap size with interval_ratio_x and interval_ratio_y. Also, the block might not always be a square, and I don't have a good solution to add beautiful edges in this case. Probably we can skip this case.

JosephBARBIERDARNAL commented 4 weeks ago

Hello again!

My first intuition is as follows

then, depending on the usefulness you see in it, a way of controlling the weight of the edges (which means leaving partially empty spaces?)

but IMO the main use case would be to fill the spaces using a given color. Since I haven't looked at the source code yet, I don't measure how complex is it to implement?

Note: I'm don't have much free time at the time I'm writing this, but I will have much more from September (: