Reputation: 11
I try to create a heatmap using hist2d. I have (x,y) coordinates and z values with a probability of this points.
x = list(range(1920))
y = list(range(1080))
z = np.zeros((1920, 1080))
plt.hist2d(x,y, bins = 256, weights=z)
VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
return array(a, dtype, copy=False, order=order, subok=True)
Traceback (most recent call last):
File "create_heatmap3.py", line 109, in <module>
plt.hist2d(x,y, bins = 50, weights=z)
File "/home/wojtek/_mgr/venv/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2849, in hist2d
**({"data": data} if data is not None else {}), **kwargs)
File "/home/wojtek/_mgr/venv/lib/python3.7/site-packages/matplotlib/__init__.py", line 1352, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "/home/wojtek/_mgr/venv/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 7073, in hist2d
density=density, weights=weights)
File "<__array_function__ internals>", line 6, in histogram2d
File "/home/wojtek/_mgr/venv/lib/python3.7/site-packages/numpy/lib/twodim_base.py", line 713, in histogram2d
hist, edges = histogramdd([x, y], bins, range, normed, weights, density)
File "<__array_function__ internals>", line 6, in histogramdd
File "/home/wojtek/_mgr/venv/lib/python3.7/site-packages/numpy/lib/histograms.py", line 1049, in histogramdd
smin, smax = _get_outer_edges(sample[:,i], range[i])
File "/home/wojtek/_mgr/venv/lib/python3.7/site-packages/numpy/lib/histograms.py", line 322, in _get_outer_edges
if not (np.isfinite(first_edge) and np.isfinite(last_edge)):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Upvotes: 1
Views: 130
Reputation: 2375
You misunderstand what the hist2d
plot does and what sort of input it expects. You don't get to construct the grid, that's what hist2d
does. You should simply supply the datapoints w_i(x_i, y_i)
that the histogram accumulates. Matplotlibs documentation:
weights array-like, shape (n, ), optional
An array of values w_i weighing each sample (x_i, y_i).
x = np.random.rand(10000) * 1920
y = np.random.rand(10000) * 1080
w = np.random.rand(10000)
plt.hist2d(x, y, weights=w)
plt.show()
Upvotes: 1