3D散点图colorbar matplotlib Python

时间:2015-05-08 17:23:15

标签: python matplotlib colorbar

根据class FormName(forms.ModelForm): label = forms.CharField(label="Label", min_length=2, max_length=100, required=True, widget=forms.TextInput(attrs={'class': 'form-control input-sm'})) hour = forms.ChoiceField(label="Hour", choices=choice_hour, required=True, widget=forms.Select(attrs={'class': 'form-control input-sm'})) minute = forms.ChoiceField(label="Minute", choices=choice_minute, required=True, widget=forms.Select(attrs={'class': 'form-control input-sm'})) period = forms.MultipleChoiceField(label="Day of week", choices=choice_period, required=True, error_messages={'required': 'At least you must select one day'}, widget=forms.CheckboxSelectMultiple(renderer=HorizontalCheckboxRenderer)) snooze = forms.ChoiceField(label="Auto stop", choices=choice_snooze, required=True, widget=forms.Select(attrs={'class': 'form-control input-sm'})) mode = forms.ChoiceField(label="Mode", choices=choice_mode, required=True, widget=forms.Select(attrs={'class': 'form-control input-sm'})) webradio = forms.ModelChoiceField(queryset=Webradio.objects.all(), widget=forms.Select(attrs={'class': 'form-control input-sm'}), required=True) class Meta: model = Alarmclock fields = ['label', 'hour', 'minute', 'period', 'snooze', 'mode', 'webradio'] def clean_mode(self): mode = self.cleaned_data.get('mode') if mode == 'music': self.fields['webradio'].required = False return super(FormName,self).clean_mode() 的值,我无法在我的3D散点图中添加颜色条,该散点图的颜色范围为minmax。我尝试过在stackoverflow上显示的各种尝试,没有任何成功。我真的很感激任何帮助,因为我对此有重大损失。

我最近的尝试是从下面显示的代码中删除的。

我的代码:

bifurWidth

Plot output

尝试的示例及其错误:

如果我在绘制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

1 个答案:

答案 0 :(得分:2)

你的代码没有运行(缺少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)

然后拨打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()