python - 3D 散点图颜色条 matplotlib Python

标签 python matplotlib colorbar

我无法将颜色条添加到根据 bifurWidth 值在 minmax 范围内着色的 3D 散点图。我尝试过 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)

Plot output

尝试及其错误的示例:

如果我在绘图 for 循环内部或外部使用 fig.colorbar(a),我会返回 NoneType oject has no attribute 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 散点图颜色条 matplotlib Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30129476/

相关文章:

python - Tkinter 中的无限循环?

python - 如何将对应于 'n' 的值列表排序到按 'n' 排序的大表中

python - 如何在不导入 matplotlib 的情况下导入 pandas 的子模块?

python - 将 matplotlib 3D 图形的框架更改为 x、y 和 z 箭头

python - 根据颜色图在条形图中的 y 值

python - Matplotlib 分配给子图的颜色条错误

python - 对于低于阈值的值使用相同颜色的散点图

python - MySQL 连接器无法使用表别名

python - 如何在 Django 中处理数据校正(但这不是 Django 特有的!)

python - matplotlib 中错误的 latex 渲染