8765674
8765674

Reputation: 1234

3D scatter plot colorbar matplotlib Python

I cannot add a colorbar to my 3D scatter plot that is coloured in range of min and max according to the value of bifurWidth. I've tried various attempts shown on stackoverflow, none have had any success. Any help would really be appreciated, as I am at a major loss with this.

My most recent attempt is hashed out of the code shown below.

My code:

from glob import glob
from pylab import *
import numpy as np
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D

def myScatter(x0,y0,power_array,c,lw,s,vmin,vmax,cmap,label,ax):
        ax.scatter(x0,y0,power_array,c=c,lw=lw,s=s,vmin=min,vmax=max,cmap=cmhot,label=label)

fig = figure()
ax = fig.add_subplot(111, projection='3d')  
cmhot = get_cmap("jet") 
fig.tight_layout()
fig.set_size_inches(25,15)

min = 3 #colorbar range
max = 10
lw = 0 #linewidth
s = 10 #scatter size

for idx, p in enumerate(dataSorted[:,1]):
    powerLoop = dataSorted[idx,0]
    powerLoop = powerLoop.astype(np.float)
    bifurWidthLoop = dataSorted[idx,2]
    bifurWidthLoop = bifurWidthLoop.astype(np.float)

    y0 = genfromtxt(p, unpack=True, usecols=[0], skiprows=19, skip_footer=1)

    length = len(x0)
    power_array = [powerLoop] * length
    bifurWidth_array = [bifurWidthLoop] * length
    label = str(bifurWidth)

    a = myScatter(x0,power_array,y0,bifurWidth_array,lw,s,min,max,cmhot,label,ax)

    #cax = ax.imshow(y0, interpolation='nearest', vmin=min, vmax=max)
    #fig.colorbar(cax)
fig.savefig('test.png',dpi=300)

Plot output

Example of an attempt and its error:

If I use fig.colorbar(a) inside or outside of the plotting for loop, I return NoneType oject has no attribute autoscale_None.

Upvotes: 2

Views: 3531

Answers (1)

Ed Smith
Ed Smith

Reputation: 13196

Your code doesn't run (x0,dataSorted,y0,etc missing) so can't get it to work (also note x0,power_array,y0 are wrong order in fn call). You need to return the handle to the scatter plot in order to plot a colorbar. If you change your myScatter function to return the handle,

def myScatter(x0,y0,power_array,c,lw,s,vmin,vmax,cmap,label,ax):
    return ax.scatter(x0,y0,power_array,c=c,lw=lw,s=s,vmin=min,vmax=max,cmap=cmhot,label=label)

and then call plt.colorbar(a). A minimal(ish) example would be,

from glob import glob
from pylab import *
import numpy as np
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D

def myScatter(x0,y0,power_array,c,lw,s,vmin,vmax,cmap,label,ax):
        return ax.scatter(x0,y0,power_array,c=c,lw=lw,s=s,vmin=min,vmax=max,cmap=cmhot,label=label)

fig = figure()
ax = fig.add_subplot(111, projection='3d')  
cmhot = get_cmap("jet") 
fig.tight_layout()
fig.set_size_inches(25,15)

min = 3 #colorbar range
max = 10
lw = 0 #linewidth
s = 10 #scatter size
label = 'test'

power_array = np.random.random((100,10))
bifurWidth_array = np.random.random((100,10))*(max-min)+min
x0 = np.random.random((100,10))
y0 = np.random.random((100,10))

a = myScatter(x0,power_array,y0,bifurWidth_array,lw,s,min,max,cmhot,label,ax)
plt.colorbar(a)
plt.show()

Upvotes: 2

Related Questions