问题
I need to plot a hist2d
with contour curves and colorbar from a pandas dataframe.
The dataframe has three cols:
x_col, y_col, z_col
I want to plot something like this where z_col
are the weights of the hist2d
:
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')
来源:https://stackoverflow.com/questions/28150228/plot-hist2d-with-weights