简体   繁体   中英

How to Aggregate Items for a HeatMap or TreeMap?

I have a simple dataframe that looks like this.

Rating  Count
AA+ 0.06
AA  0
AA  0
AA+ 0.09
BBB 0.55
AA+ 0.04
AA  0
AA  0.11
B   0.54
A   0
BBB 0.5

I tried to plot it as both a HeatMap and a TreeMap. I tested this simple code and got an error.

f, ax = plt.subplots(figsize=(9, 6))
sns.heatmap(df1, annot=True, fmt=".01", linewidths=.5, ax=ax)

Error: TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

I think the dataframe has to be pivoted, or somehow coerced into some other form. What I would like to do is count the number of occurrences of 'AA+ 0.06' & 'AA+ 0.04', etc. There are several of these patterns repeating. There could be 1, 2, 3, 4, etc., counts or each. How can I do that, and plot the results in a HeatMap or TreeMap? Thank you.

This is what worked for me.

import squarify    # pip install squarify (algorithm for treemap)
import matplotlib

...etc.
squarify.plot(sizes=df_rtg['Count'], label=df_rtg['Rating'], alpha=.8 )
plt.axis('off')
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM