This is more of a general question about 3d histogram creation in python.
I have attempted to create a 3d histogram using the X and Y arrays in the following code
import matplotlib
import pylab
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D
from matplotlib import cm
def threedhist():
X = [1, 3, 5, 8, 6, 7, 1, 2, 4, 5]
Y = [3, 4, 3, 6, 5, 3, 1, 2, 3, 8]
fig = pylab.figure()
ax = Axes3D(fig)
ax.hist([X, Y], bins=10, range=[[0, 10], [0, 10]])
plt.xlabel('X')
plt.ylabel('Y')
plt.zlabel('Frequency')
plt.title('Histogram')
plt.show()
However, I am getting the following error
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
a3dhistogram()
File "C:/Users/ckiser/Desktop/Projects/Tom/Python Files/threedhistogram.py", line 24, in a3dhistogram
ax.hist([X, Y], bins=10, range=[[0, 10], [0, 10]])
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 7668, in hist
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
File "C:\Python27\lib\site-packages\numpy\lib\function_base.py", line 169, in histogram
mn, mx = [mi+0.0 for mi in range]
TypeError: can only concatenate list (not "float") to list
I have tried the code with and without the "[" in the line ax.hist([X, Y], bins=10, range=[[0, 10], [0, 10]]) I have also tried the function from numpy without success H, xedges, yedges = np.histogram2d(x, y, bins = (10, 10)) Am I missing a step or a parameter? Any advice would be greatly appreciated.
I've added to @lxop's answer to allow for arbitrary size buckets:
Have a look at http://matplotlib.sourceforge.net/examples/mplot3d/hist3d_demo.html, this has a working example script.
I've improved the code at that link to be more of a histogram:
I'm not sure how to do it with Axes3D.hist ().
In this answer there is a solution for 2D and 3D Histograms of scattered points. The usage is simple:
Where
sub
is amatplotlib
"Subplot"
instance (3D or not) andpoints
contains the points used for the scatter plot.I posted this in a related thread about colored 3d bar plots, but I think it's also relevant here as I couldn't find a complete answer for what I needed in either thread. This code generates a histogram scatterplot for any sort of x-y data. The height represents the frequency of values in that bin. So, for example, if you had many data point where (x,y) = (20,20) it would be high and red. If you had few data points in the bin where (x,y) = (100,100) it would be low and blue.
Note: result will vary substantially depending on how much data you have and how many bins your choose for you histogram. Adjust accordingly!
The results for about 75k data points of mine are below. Note, you can drag and drop to different perspectives and may want to save multiple views for presentations, posterity.