3D 散点图颜色条 matplotlib Python

3D scatter plot colorbar matplotlib Python

我无法根据 bifurWidth 的值在 minmax 范围内为我的 3D 散点图添加颜色条。我尝试了 Whosebug 上显示的各种尝试,none 都取得了成功。真的很感激任何帮助,因为我对此感到很茫然。

我最近的尝试是从下面显示的代码中散列出来的。

我的代码:

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)

尝试示例及其错误:

如果我在绘图 for 循环内部或外部使用 fig.colorbar(a),我 return NoneType 对象没有属性 autoscale_None.

您的代码没有 运行(缺少 x0、dataSorted、y0 等),因此无法正常工作(另请注意 x0、power_array、y0 在 fn 中的顺序错误称呼)。您需要 return 散点图的句柄才能绘制颜色条。如果将 myScatter 函数更改为 return 句柄,

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)

然后调用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()