Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带圆圈的热图显示人口规模_Python_Matplotlib_Data Visualization_Seaborn - Fatal编程技术网

Python 带圆圈的热图显示人口规模

Python 带圆圈的热图显示人口规模,python,matplotlib,data-visualization,seaborn,Python,Matplotlib,Data Visualization,Seaborn,您好,我想用Python生成一个热图,类似于图中所示,圆圈的大小表示该单元格中样本的大小。我在seaborn的画廊里找了找,什么也找不到,我想我不能用matplotlib来做这件事 一个选项是使用matplotlib的散点图和图例和网格。可以通过指定比例来指定这些圆的大小。您还可以更改每个圆的颜色。您应该以某种方式指定X,Y值,以便圆直线排列。这是我从中得到的一个例子: volume=np.random.rayleigh(27,大小=40) 数量=np.随机.泊松(10,大小=40) 排名=np

您好,我想用Python生成一个热图,类似于图中所示,圆圈的大小表示该单元格中样本的大小。我在seaborn的画廊里找了找,什么也找不到,我想我不能用matplotlib来做这件事


一个选项是使用matplotlib的散点图和图例和网格。可以通过指定比例来指定这些圆的大小。您还可以更改每个圆的颜色。您应该以某种方式指定
X,Y
值,以便圆直线排列。这是我从中得到的一个例子:

volume=np.random.rayleigh(27,大小=40)
数量=np.随机.泊松(10,大小=40)
排名=np.随机.正常(大小=40)
价格=np.随机.均匀(1,10,尺寸=40)
图,ax=plt.子批次()
#因为在提供“s”的尺寸时,价格太低了,
#我们将其标准化为一些有用的点大小,s=0.3*(价格*3)**2
分散度=最大分散度(数量、数量、c=排名、s=0.3*(价格*3)**2,
vmin=-3,vmax=3,cmap=“光谱”)
#为排名(颜色)生成图例。尽管有40种不同的
#排名,我们只想在图例中显示其中的5个。
legend1=ax.legend(*散布.legend_元素(num=5),
loc=“左上”,title=“排名”)
ax.添加艺术家(legend1)
#为价格(尺寸)生成图例。因为我们想展示价格
#在美元中,我们使用*func*参数来提供函数的倒数
#用于从上面计算尺寸。*fmt*确保显示价格
#用美元。请注意,我们在这里的目标是5个元素,但在
#由于为我们选择的自动四舍五入价格,创建了图例。
kw=dict(prop=“size”,num=5,color=scatter.cmap(0.7),fmt=“${x:.2f}”,
func=lambda s:np.sqrt(s/.3)/3)
legend2=最大图例(*散点图例_元素(**kw),
loc=“右下”,title=“价格”)
plt.show()
输出:


以下是使用Bokeh图的可能解决方案:

import pandas as pd
from bokeh.palettes import RdBu
from bokeh.models import LinearColorMapper, ColumnDataSource, ColorBar
from bokeh.models.ranges import FactorRange
from bokeh.plotting import figure, show
from bokeh.io import output_notebook

import numpy as np

output_notebook()

d = dict(x = ['A','A','A', 'B','B','B','C','C','C','D','D','D'], 
         y = ['B','C','D', 'A','C','D','B','D','A','A','B','C'], 
         corr = np.random.uniform(low=-1, high=1, size=(12,)).tolist())

df = pd.DataFrame(d)

df['size'] = np.where(df['corr']<0, np.abs(df['corr']), df['corr'])*50
#added a new column to make the plot size

colors = list(reversed(RdBu[9]))
exp_cmap = LinearColorMapper(palette=colors, 
                             low = -1, 
                             high = 1)


p = figure(x_range = FactorRange(), y_range = FactorRange(), plot_width=700, 
           plot_height=450, title="Correlation",
           toolbar_location=None, tools="hover")

p.scatter("x","y",source=df, fill_alpha=1,  line_width=0, size="size", 
          fill_color={"field":"corr", "transform":exp_cmap})

p.x_range.factors = sorted(df['x'].unique().tolist())
p.y_range.factors = sorted(df['y'].unique().tolist(), reverse = True)

p.xaxis.axis_label = 'Values'
p.yaxis.axis_label = 'Values'

bar = ColorBar(color_mapper=exp_cmap, location=(0,0))
p.add_layout(bar, "right")

show(p)

将熊猫作为pd导入
从bokeh.palets导入RdBu
从bokeh.models导入LinearColorMapper、ColumnDataSource和ColorBar
从bokeh.models.ranges导入FactorRange
从bokeh.plotting导入图形,显示
从bokeh.io导入输出\u笔记本
将numpy作为np导入
输出_笔记本()
d=dict(x=['A'、'A'、'A'、'B'、'B'、'C'、'C'、'd'、'd'、'd'],
y=['B','C','D','A','C','D','B','D','A','A','B','C',],
corr=np.random.uniform(低=1,高=1,大小=(12,).tolist())
df=pd.数据帧(d)

df['size']=np。其中(df['corr']相反。虽然matplotlib可以做几乎所有事情,但seaborn只提供了一小部分选项。 因此,使用matplotlib,您可以绘制圆的
PatchCollection
,如下所示。
注意:您也可以使用散点图,但由于散点大小是以绝对单位表示的,因此很难将其缩放到网格中

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()