我无法将彩条添加到我的3D散点图中,根据min的值在maxbifurWidth范围内进行着色。我已经尝试了在stackoverflow上显示的各种尝试,但都没有成功。任何帮助将不胜感激,因为我对此感到非常不知所措。

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

我的代码:

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)




尝试及其错误的示例:

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

最佳答案

您的代码未运行(x0,dataSorted,y0等丢失),因此无法正常工作(另请注意,x0,power_array,y0在fn调用中的顺序错误)。您需要将句柄返回到散点图以绘制颜色栏。如果您更改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)


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

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

关于python - 3D散点图colorbar matplotlib Python,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30129476/

10-12 21:26