1

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')
double-beep
  • 5,031
  • 17
  • 33
  • 41
Garet
  • 365
  • 2
  • 13
  • 1
    Could you provide a minimal working example? Also it is unclear what fails in your code: What error do you get? – hitzg Jan 26 '15 at 13:30
  • My problem is that I don't know what matplotlib expect in the weight parameter and I don't know how to make the 1D array from a pandas dataframe column. – Garet Jan 26 '15 at 13:43
  • 1
    Please show us the whole traceback in your question – ali_m Jan 26 '15 at 21:10
  • Sorry, I did not express myself well. That code does not give me any error. But I am not sure about how matplotlib uses the weight params. The documentacion says: "An array of values w_i weighing each sample (x_i, y_i)". But my z var is an Pandas Series. Is it ok? – Garet Jan 27 '15 at 08:36
  • You are asking different questions. I would break your question into two or three different questions. – tommy.carstensen May 04 '18 at 00:28
  • try http://corner.readthedocs.io – Ruggero Turra Jan 31 '19 at 10:16

1 Answers1

-1

You have to enter an array with the same length of x or y (these ones must have the same length, too)

Since z_col is a column in the dataframe, it will have the right dimension (the same as x_col or y_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

xkudsraw
  • 149
  • 12