用权重绘制hist2d [英] Plot hist2d with weights
问题描述
我需要从熊猫数据框中绘制带有轮廓曲线和颜色条的hist2d
.
I need to plot a hist2d
with contour curves and colorbar from a pandas dataframe.
数据框具有三个列:
x_col, y_col, z_col
我想绘制这样的图形,其中z_col
是hist2d
的权重:
I want to plot something like this where z_col
are the weights of the hist2d
:
但是我不知道如何从hist2d函数将z_col转换为权重一维数组.
But I don't know how to transform the z_col into a weight 1D array from the hist2d function.
fdf = df.groupby([valueX, valueY], as_index=False).mean().sort([valueX, valueY])
x = fdf[valueX]
y = fdf[valueY]
z = fdf[valueZ]
(... axes instantiation)
bins = 100
counts, xbins, ybins, image = axes.hist2d(x, y, bins=bins, normed=True, weights=z)
axes.contour(counts, extent=[xbins.min(), xbins.max(), ybins.min(), ybins.max()], linewidths=3)
pc = axes.pcolor(counts, cmap=cm.jet)
fig.colorbar(pc)
axes_x.hist(x, bins=bins)
axes_y.hist(y, bins=bins, orientation='horizontal')
推荐答案
您必须输入长度为x或y的数组(这些数组也必须具有相同的长度)
You have to enter an array with the same length of x or y (these ones must have the same length, too)
由于z_col是数据框中的一列,因此它将具有正确的尺寸(与x_col或y_col相同)
Since z_col is a column in the dataframe, it will have the right dimension (the same as x_col or y_col)
要获取一个数组,您需要:dataframe.z_col.values这是一个数组).另一方面,dataframe.z_col是熊猫系列
To get an array you need: dataframe.z_col.values this is an array). On the other side, dataframe.z_col is a pandas series
这篇关于用权重绘制hist2d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!