Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib-有没有在颜色条刻度中使用整数和小数的方法?_Python_Matplotlib_Colorbar - Fatal编程技术网

Python Matplotlib-有没有在颜色条刻度中使用整数和小数的方法?

Python Matplotlib-有没有在颜色条刻度中使用整数和小数的方法?,python,matplotlib,colorbar,Python,Matplotlib,Colorbar,问题 我有一个绘图,我需要颜色条刻度更大,因为我要将这个绘图添加到多面板绘图中&刻度标签将很难阅读,否则所有内容都会如此浓缩。我的问题是,如果我把标签变大,它们会在靠近颜色条末端时因为.0小数而相互碰撞。但是我不能使所有的记号都是整数,因为我需要靠近颜色条左侧和中心的小数标签 代码 这是我用来制作情节的代码 #Set variables lonlabels = ['0','45E','90E','135E','180','135W','90W','45W','0'] latlabels = [

问题

我有一个绘图,我需要颜色条刻度更大,因为我要将这个绘图添加到多面板绘图中&刻度标签将很难阅读,否则所有内容都会如此浓缩。我的问题是,如果我把标签变大,它们会在靠近颜色条末端时因为.0小数而相互碰撞。但是我不能使所有的记号都是整数,因为我需要靠近颜色条左侧和中心的小数标签

代码

这是我用来制作情节的代码

#Set variables
lonlabels = ['0','45E','90E','135E','180','135W','90W','45W','0']
latlabels = ['90S','60S','30S','Eq.','30N','60N','90N']

#Set cmap properties
bounds = np.array([0.001,0.01,0.1,1,5,10,25,50])
norm = colors.LogNorm(vmin=0.0001,vmax=50) #creates logarithmic scale
cmap = plt.get_cmap('jet')
cmap.set_over('#660000')  #everything above range of colormap

#Create basemap
fig,ax = plt.subplots(figsize=(15.,10.))
m = Basemap(projection='cyl',llcrnrlat=-90,urcrnrlat=90,llcrnrlon=0,urcrnrlon=360.,lon_0=180.,resolution='c')
m.drawcoastlines(linewidth=1)
m.drawcountries(linewidth=1)
m.drawparallels(np.arange(-90,90,30.),linewidth=0.3)
m.drawmeridians(np.arange(-180.,180.,45.),linewidth=0.3)   
meshlon,meshlat = np.meshgrid(lon,lat)
x,y = m(meshlon,meshlat)

#Plot variables
trend = m.pcolormesh(x,y,lintrends_120,cmap=cmap, norm=norm, shading='gouraud',vmin=0.0001,vmax=50)

#Set plot properties
#Colorbar
cbar=m.colorbar(trend, size='8%',ticks=bounds,extend="max",location='bottom',pad=0.8)
cbar.set_label(label='Linear Trend (mm/day/decade)',size=25)
cbar.set_ticklabels(bounds)
for t in cbar.ax.get_xticklabels():
     t.set_fontsize(20)
#Titles & labels
ax.set_title('c) 1979-2098',fontsize=35)
ax.set_xlabel('Longitude',fontsize=25)
ax.set_xticks(np.arange(0,405,45))
ax.set_xticklabels(lonlabels,fontsize=20)
ax.set_ylabel('Latitude',fontsize=25)
ax.set_yticks(np.arange(-90,120,30))
ax.set_yticklabels(latlabels,fontsize=20)
靠近底部的最后一段代码中的colorbar块

问题


有没有一种方法可以在颜色栏标记标签中组合小数(浮点)和整数,使1.0、2.0等显示为1、2等?我尝试创建两个单独的np.array()实例,其中一个是小数,另一个是整数,但是当我附加它们时,它们都变成了浮点数。

很难测试,因为您的示例无法直接运行,但您应该能够将标签设置为您喜欢的任何字符串。大概是这样的:

bound_labels = [str(v) if v <=1 else str(int(v)) for v in bounds]
cbar.set_ticklabels(bound_labels)

bound_labels=[str(v)if v很难测试,因为您的示例无法直接运行,但您应该能够将标签设置为您喜欢的任何字符串。类似于:

bound_labels = [str(v) if v <=1 else str(int(v)) for v in bounds]
cbar.set_ticklabels(bound_labels)
bound_labels=[str(v)if vTry

试一试


它只需要一个额外的注释:bounds=['0.001'、'0.01'、'0.1'、'1'、'5'、'10'、'25'、'50']应该是boundlabels=['0.001'、'0.01'、'0.1'、'1'、'5'、'10'、'25'、'50']或类似的东西,因为仍然需要指定np.array()边界才能使记号位于正确的位置。它只需要一个额外的注释:bounds=['0.001'、'0.01'、'0.1'、'1'、'5'、'10'、'25'、'50']应该是boundlabels=['0.001'、'0.01'、'0.1'、'1'、'5'、'10'、'25'、'50']或类似的东西,因为仍然需要指定np.array()边界才能使记号位于正确的位置。