4

I have a 3D dataset which I visualize with a scatter plot. This is how it looks like:

This is how it looks like

I would now like to color the different dots depending on the density of the data. Is there any way I can do this in Python or MATLAB? Another option could be to bin the data and color the bins depending on how many data points lie within them. I binned the data by using Python's histogramdd function.

H,edges = np.histogramdd(al,bins=(16,16,16))

The idea is to have it look kind of like this:

this

using the code provided in this thread: 3D discrete heatmap in matplotlib

If you have any ideas on how I could do this, I would be really happy to hear them!

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
thebear
  • 133
  • 1
  • 7
  • the scatterplot itself (scatter3 in matlab) can take an argument to determine the size of each scatterpoint, and another argument to determine the colour. – Tasos Papastylianou Aug 27 '17 at 15:25
  • see https://stackoverflow.com/a/38557802/4183191 for a 2D example; the 3D example is identical; just add another argument for the 3rd dimension. You can also choose a shape for your points if you prefer squares. – Tasos Papastylianou Aug 27 '17 at 15:26
  • Yes thank you, I though about that but how do I get the density of the data for the individual datapoints in order to determine their color? – thebear Aug 27 '17 at 15:48
  • I calculate the number of neighbors in a certain range now to determine the color and it gives a nice looking result. If there is an inbuilt function for a 3d heatmap that would be really cool to know about though :) – thebear Aug 27 '17 at 16:12
  • There isn't an in built function as far as I know, but you should be able to fairly efficiently implement what you describe, in which case you have your solution, consider posting it as an answer – Wolfie Aug 27 '17 at 16:15

1 Answers1

0

Thank you all for your ideas. Using the hist3 fundtion does unfortunately not work since I have 3 dimensions and hist3 takes only two variables and calculates the histogram values as the third. My solution for now is to calculate for each data point the number of points which are in a certain radius. Then I use these values to color my plot with scatter3(x,y,z,2,c)

c=zeros(size(x));
for i=1:length(x)
  j=1:length(x);
  j(i)=[];
  s = sort((x(j)-x(i)).^2+(y(j)-y(i)).^2+(d(j)-d(i)).^2);
  c(i)=sum(s<2);
end
scatter3(d,x,y,2,c)
thebear
  • 133
  • 1
  • 7
  • add a minimal working example containing some code so that it would be *more* useful for the future visitors as well – Sardar Usama Aug 27 '17 at 16:25