title: How to generate a sphere in 3D Numpy array
body: <p>Given a 3D numpy array of shape (256, 256, 256), how would I make a solid sphere shape inside? The code below generates a series of increasing and decreasing circles but is diamond shaped when viewed in the two other dimensions. </p>
  
  <pre><code>def make_sphere(arr, x_pos, y_pos, z_pos, radius=10, size=256, plot=False):
  
      val = 255            
      for r in range(radius):
          y, x = np.ogrid[-x_pos:n-x_pos, -y_pos:size-y_pos]
          mask = x*x + y*y &lt;= r*r 
          top_half = arr[z_pos+r]
          top_half[mask] = val #+ np.random.randint(val)
          arr[z_pos+r] = top_half
  
      for r in range(radius, 0, -1):
          y, x = np.ogrid[-x_pos:size-x_pos, -y_pos:size-y_pos]
          mask = x*x + y*y &lt;= r*r 
          bottom_half = arr[z_pos+r]
          bottom_half[mask] = val#+ np.random.randint(val)
          arr[z_pos+2*radius-r] = bottom_half
  
      if plot:
          for i in range(2*radius):
              if arr[z_pos+i].max() != 0:
                  print(z_pos+i)
                  plt.imshow(arr[z_pos+i])
                  plt.show()
  
      return arr
  </code></pre>