0

I am wanting to plot all my spheres on the same graph, which should be 1 by 1 by 1. I am calling the plotting function shown below in several loops, as I want the spheres to change colours depending on some conditions. The coordinates of the centre of the spheres are described in another function in disk. However, all the spheres that I plot do change colour but are each on separate graphs. How would I change the code below so that each time I call the function plot_disks2, the spheres are added to the same graph. My code so far is:

def plot_disks2(disk, radius, c, ax=None):

    fig = plt.figure(figsize=(12,12), dpi=300)
    ax = fig.add_subplot(111, projection='3d')

    u = np.linspace(0, 2 * np.pi, 100)
    v = np.linspace(0, np.pi, 100)

    x = radius * np.outer(np.cos(u), np.sin(v))
    y = radius * np.outer(np.sin(u), np.sin(v))
    z = radius * np.outer(np.ones(np.size(u)), np.cos(v))

    sphere = ax.plot_surface(x+disk[0], y+disk[1], z+disk[2],  rstride=4, cstride=4, color=c, linewidth=0, alpha=0.5)
    ax.add_artist(sphere)
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Hello
  • 97
  • 1
  • 5
  • try moving the `fig=...` and `ax=...` lines out of the function and pass `ax` as argument. Note that `u`,`v`,`x`,`y`,`z` definition could also be out of the function to avoid recomputing them every time...(apart from the `radius` scaling) – Julien Nov 24 '16 at 00:49
  • it works!! thank you so much for your help. Just as an extra little question, do you know how I can make the x, y, z axes all equal to 1? Thanks again for the help – Hello Nov 24 '16 at 01:22
  • `ax.set_xlim([0,1])` and similar for `y` and `z` – Julien Nov 24 '16 at 01:32

0 Answers0