带圆圈的热图指示人口规模

编程入门 行业动态 更新时间:2024-10-27 01:28:37
本文介绍了带圆圈的热图指示人口规模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在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()

更多推荐

带圆圈的热图指示人口规模

本文发布于:2023-07-18 05:10:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141305.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:圆圈   指示   热图   人口   规模

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!