title: How to plot a 3_d representation of the probability density function in the bloch sphere?
body: <p>I have a model which gives me datasets in a specific form. Lets say its a radius + 2 angles (r,phi,theta). I would like to plot this data in the bloch sphere with two volumes indicating the regions where 25% or 75% of the data points are lying.</p>
  
  <p>Here an example of what I would like to visualize in 2D.</p>
  
  <p><a href="https://i.stack.imgur.com/vDaMe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vDaMe.png" alt="Here an example of what I would like to visualize in 2D."></a></p>
  
  <p>My idea was to:</p>
  
  <ol>
  <li><p>Bin the 3D data with np.histogramdd </p>
  
  <pre><code># transform my data onto a cartesian coordinate system
  X = amp * np.sin(2*chi) * np.cos(theta)
  Y = amp * np.sin(2*chi) * np.sin(theta)
  Z = amp * np.cos(2*chi)
  
  data_xyz=np.array([X,Y,Z]).T
  
  hist, binedges = np.histogramdd(data_xyz,bins=60, normed=False)
  
  
  list_of_weighted_entries = np.sort(np.reshape(hist,np.power(60,3)))
  weight_sum = np.sum(hist)
  </code></pre></li>
  <li><p>Reshape the binned data so that I get a list with descending order of the weighted indexes and define the threshold (25 or 75 %) to define which indexes are inside the specific pdf volume :</p>
  
  <pre><code>#For 25 %
  cumu_weight = 0
  index = 1
  while cumu_weight &lt; weight_sum * 0.25:
      cumu_weight = cumu_weight + list_of_weighted_entries[-index]
      index = index + 1
  
  treshhold_value = list_of_weighted_entries[-(index-1)]
  </code></pre></li>
  <li><p>Using marching cubes to plot those volumes</p>
  
  <pre><code>#calculate the bin_size for each direction
  diff0 = binedges[0][1] - binedges[0][0]
  diff1 = binedges[1][1] - binedges[1][0]
  diff2 = binedges[2][1] - binedges[2][0]
  
  verts, faces, normals, values = measure.marching_cubes_lewiner(hist, treshhold_value,(diff0,diff1,diff2))
  
  fig  = plt.figure(figsize=(10, 10))
  ax   = fig.add_subplot(111, projection='3d')
  mesh = Poly3DCollection(verts[faces],linewidths=0,facecolor="rosybrown")
  mesh.set_edgecolor('k')
  ax.add_collection3d(mesh)
  </code></pre></li>
  <li><p>Add some plotting styles etc....</p>
  
  <p>This gives me a quite nice image but something goes wrong as the volume should not be outside the transparent sphere+ it is rotated. Somewhere in the transformation from the bins back to the coordinates I seem to mess up. </p></li>
  </ol>
  
  <p>Does someone have an idea where?</p>
  
  <p>EDIT: I'm now quite sure that the problem lies in <code>Poly3DCollection</code> plotting. The thing is that this plot always starts at 0:0:0 but the coordinates I give histogramm function also have negative volumes-> I have to add a translation to the data I get after calculating my "PDF volumes"</p>
  
  <p>This gives me a quite nice image but something goes wrong as the volume should not be outside the transparent sphere (as the radius of the sphere is 1.1 times the max radius).</p>
  
  <p><a href="https://i.stack.imgur.com/5znlQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5znlQ.png" alt="enter image description here"></a></p>