I am following the script of an answer to this question to plot a heatmap over a sphere using the SmoothDensityHistogram[] function. The data is a text file of N rows by 3 columns, with each column corresponding to x, y, and z positions confined to the surface of a sphere of arbitrary radius.

vecs = Import["C:\\...\\testing_3d_mathematica_v1.txt", "Table"];

hist = Image[ SmoothDensityHistogram[vecs, AspectRatio -> Automatic, ColorFunction -> "ThermometerColors", 
Frame -> False, ImagePadding -> None, PerformanceGoal -> "Quality", PlotRange -> {{-π, π}, {0, π}}, 
PlotRangePadding -> None], ImageResolution -> 256];

ParametricPlot3D[{Cos[θ] Sin[φ], Sin[θ] Sin[φ], Cos[φ]}, {θ, -π, π}, {φ, 0, π}, Lighting -> "Neutral", Mesh -> None, PlotStyle -> Texture[hist]]

I am receiving a "not a valid dataset or list of datasets" error. I try Flatten[vecs] but this results in an empty plot of a sphere with no histogram texture. I also suspect the axes limits are incorrect, but I'm not sure how to adapt this code to function with my data.

Attached are a few lines of data:

22.03 15.85 22.09

-7.39 2.68 34.10

-3.11 0.82 34.85

5.04 -6.17 34.08

  • $\begingroup$ Welcome to Mathematica StackExchange! Can you please edit the question include a few lines from your file testing_3d_mathematica_v1.txt, so that we can try to reproduce your problem? $\endgroup$
    – Domen
    Commented Apr 26 at 16:05
  • $\begingroup$ What's this have to do with "4D"? $\endgroup$ Commented Apr 26 at 16:25
  • $\begingroup$ Sorry, I'll edit the title. 4D was meant to describe that the plot is made up of three-dimensional positions with each having a weight. $\endgroup$
    – Zachary
    Commented Apr 26 at 16:30
  • $\begingroup$ How big is your data? $\endgroup$ Commented Apr 26 at 20:01
  • 1
    $\begingroup$ SmoothDensityHistogram is expecting a list of {x,y} points, from which it constructs a histogram, not {x,y,z} points. $\endgroup$
    – MelaGo
    Commented Apr 26 at 23:33

1 Answer 1


First of all it is a fake density plot (like most (if not all) of the posts in your link in OP) since density computed in 2D and then wrapped around a sphere is not same as if the density was computed straight on a sphere.

If you know a formula how to compute density straight on a sphere you can post it in a comment and I will redo the plots with that formula.

Secondly I used a logarithmic depiction because your data is such that without logarithm it would look only like a single disk of one color on a sphere of other color.

data = ImageData[Import["https://i.sstatic.net/UDaNRjtE.png"], 
      "Byte"] // Flatten // FromCharacterCode // Uncompress;
pdata = ToPolarCoordinates[data][[All, {2, 3}]];

Graphics3D[{Sphere[{0, 0, 0}, 35], Point[data]}, Boxed -> False]

hist = SmoothDensityHistogram[pdata, 
  PlotRange -> {{0, Pi}, {-Pi, Pi}}, AspectRatio -> Automatic, 
  PlotPoints -> 200, 
  ColorFunction -> (Blend["M10DefaultDensityGradient", Log[100 # + 1]/
      4.6] &), PlotRangePadding -> None, Frame -> False, 
  ImagePadding -> None]

SphericalPlot3D[1, {u, 0, Pi}, {v, -Pi, Pi}, Mesh -> None, 
 PlotStyle -> Texture[hist], Lighting -> "Neutral", Boxed -> False, 
 Axes -> False]

enter image description here

enter image description here

enter image description here

Data stored in image:

  • $\begingroup$ This is great -- thanks so much. $\endgroup$
    – Zachary
    Commented May 1 at 16:03
  • $\begingroup$ I assume the second image you posted is "i.sstatic.net/UDaNRjtE.png". How is this generated from the text file? $\endgroup$
    – Zachary
    Commented May 1 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.