简体   繁体   English

如何将 plot 与 python 中不同大小的矩形进行热 map

[英]How to plot a heat map with different sizes of rectangles in python

I have a dataframe looks like below:我有一个 dataframe 如下所示:

import pandas as pd

df = pd.DataFrame({'id':[1,2,3,4], 'grade':[98,50,10,100], 'c':[1000,29,69,150]})

I want to plot a heatmap with sub-rectangles that their sizes represent the values from column 'c' and the colors represent the values from 'grade'我想要 plot 一个带有子矩形的热图,它们的大小代表来自“c”列的值,而 colors 代表来自“等级”的值

If it is too complicated then I can just go with colors and grade pair and not the size and "c"如果它太复杂,那么我可以只用 go 和 colors 和等级对,而不是尺寸和“c”

I did some researches and found some examples with using sns.heatmap, but I could not get that work.我做了一些研究并找到了一些使用 sns.heatmap 的示例,但我无法完成这项工作。

You don't need heatmap, you need treemap .你不需要热图,你需要树图
Based on this link , you can use squarify like this:基于此链接,您可以像这样使用squarify

import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import squarify # pip install squarify (algorithm for treemap)</pre>
import pandas as pd

df = pd.DataFrame({'id':[1,2,3,4], 'grade':[98,50,10,100], 'c':[1000,29,69,150]})

# Create a dataset:
my_values=df.c # YOUR VALUES
 
# create a color palette, mapped to these values
cmap = matplotlib.cm.Blues
mini=min(my_values)
maxi=max(my_values)
norm = matplotlib.colors.Normalize(vmin=mini, vmax=maxi)
colors = [cmap(norm(value)) for value in my_values]
 
# Change color
squarify.plot(sizes=my_values, alpha=.8, color=colors )
plt.axis('off')
plt.show()

树形图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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