3D散点图colorbar matplotlib Python

时间:2022-09-10 22:22:32

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 *, none have had any success. Any help would really be appreciated, as I am at a major loss with this.

我不能在我的3D散点图中添加一个颜色条,该散点图根据bifurWidth的值在min和max范围内着色。我已尝试在*上显示各种尝试,没有任何成功。我真的很感激任何帮助,因为我对此有重大损失。

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)

3D散点图colorbar matplotlib Python

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.

如果我在绘图的内部或外部使用fig.colorbar(a)for循环,我返回NoneType oject没有属性autoscale_None。

1 个解决方案

#1


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,

你的代码没有运行(缺少x0,dataSorted,y0等),所以不能让它工作(还要注意fn调用中x0,power_array,y0是错误的顺序)。您需要将手柄返回到散点图以绘制颜色条。如果更改myScatter函数以返回句柄,

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,

然后调用plt.colorbar(a)。最小(ish)的例子是,

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()

#1


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,

你的代码没有运行(缺少x0,dataSorted,y0等),所以不能让它工作(还要注意fn调用中x0,power_array,y0是错误的顺序)。您需要将手柄返回到散点图以绘制颜色条。如果更改myScatter函数以返回句柄,

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,

然后调用plt.colorbar(a)。最小(ish)的例子是,

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()