我想在Python中生成一个热图,类似于所示的图,其中圆圈的大小指示该单元格中样本的大小。我在seaborn的画廊看了看,找不到任何东西,而且我不认为我可以用matplotlib做到这一点。
Hi I would like to produce a heatmap in Python, similar to the one shown, where the size of the circle indicates the size of the sample in that cell. I looked in seaborn's gallery and couldn't find anything, and I don't think I can do this with matplotlib.
推荐答案
是相反的。尽管matplotlib几乎可以完成所有工作,但是seaborn仅提供了一小部分选项。 因此,使用matplotlib可以绘制一个 PatchCollection 的圆,如下所示。 注意:您可以同样使用散点图,但是由于散点的大小是以绝对单位表示的,因此很难将它们缩放到网格中。
It's the inverse. While matplotlib can do pretty much everything, seaborn only provides a small subset of options. So using matplotlib, you can plot a PatchCollection of circles as shown below. Note: You could equally use a scatter plot, but since scatter dot sizes are in absolute units it would be rather hard to scale them into the grid.
import numpy as np import matplotlib.pyplot as plt from matplotlib.collections import PatchCollection N = 10 M = 11 ylabels = ["".join(np.random.choice(list("PQRSTUVXYZ"), size=7)) for _ in range(N)] xlabels = ["".join(np.random.choice(list("ABCDE"), size=3)) for _ in range(M)] x, y = np.meshgrid(np.arange(M), np.arange(N)) s = np.random.randint(0, 180, size=(N,M)) c = np.random.rand(N, M)-0.5 fig, ax = plt.subplots() R = s/s.max()/2 circles = [plt.Circle((j,i), radius=r) for r, j, i in zip(R.flat, x.flat, y.flat)] col = PatchCollection(circles, array=c.flatten(), cmap="RdYlGn") ax.add_collection(col) ax.set(xticks=np.arange(M), yticks=np.arange(N), xticklabels=xlabels, yticklabels=ylabels) ax.set_xticks(np.arange(M+1)-0.5, minor=True) ax.set_yticks(np.arange(N+1)-0.5, minor=True) ax.grid(which='minor') fig.colorbar(col) plt.show()
更多推荐
带圆圈的热图指示人口规模
发布评论