How to create a treemap with Python where total size is the count of all rows

Viewed 196

I'm trying to build a treemap (although I'd have preferred a single percentage bar chart, but I just can't figure it out at the moment). The df looks something like this:

df = pd.DataFrame({'fruit': ['apple', 'apple', 'kiwi'],
               'color': ['red', 'green', 'green']})

The resulting chart should give a % breakdown of "color"; the metric should be the total count (not distinct).

I pre-aggregated the data, although I'm unsure whether it's at all necessary, like this:

example = (df["color"].value_counts()).rename_axis("fruit_color").to_frame("count")

When I run the code below, I get the following error: "unsupported operand type(s) for +: 'int' and 'str'"

squarify.plot(example)

I can see it's trying to calculate the total_size by summing up all the individual sizes and, for some reason, it doesn't work.

Can you help understand what I'm doing wrong? Also, is it possible to add % labels fairly easily, or just value labels if it requires too much work?

0 Answers
Related