简体   繁体   English

Matplotlib离散颜色条标签未正确放置

[英]Matplotlib discrete colorbar labels not placed correctly

I am just plotting image in matplotlib but the labels in colorbar are not getting placed properly.I have placed 12 classes in colorbar but it is showing only some of them. 我只是在matplotlib中绘制图像,但是colorbar中的标签没有正确放置。我在colorbar中放置了12个类,但它只显示了其中的一些。 I shall be thankful for the help. 我将感谢你的帮助。

from PIL import Image
from scipy import misc
import scipy
import pylab as pl
import numpy as np
from sklearn import svm
from sklearn import neighbors
import matplotlib.pyplot as plt
import matplotlib as mpl

def plotimage(labels,image,imname):#input - array of labels and image of 1..n classes
cmap = mpl.colors.ListedColormap(['r', 'g', 'b', 'c','k','y','m','w','#FFFF00','#FF8C00','#FF8EFC','#962404'])#only 12 classes at present  
#need to create pallate and labels later out of this def
bounds = [0,1, 2, 3,4, 5,6,7,8,9,10,11,12,13]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
img = plt.imshow(image, cmap=cmap )
cbar = plt.colorbar(img, cmap=cmap)  #norm=norm, boundaries=bounds, ticks=bounds
cbar.ax.set_yticklabels(labels)
plt.gcf().canvas.set_window_title("Classification")
#imsave("expr1_im", image.reshape(image.shape[0]/256,256), cmap=cmap)
#savefig(imname)
plt.title(imname)
plt.show()

b_imgarray = np.loadtxt("PlotData2001_Solar/SVM2001PixelArray.csv", delimiter=',')

labels = ['D','F','A','G','R','I','BS','ABG','W','BG','COAG','BSMIX']

a = np.reshape(b_imgarray, (1500,1500))
c = 'SVmimage'
plotimage(labels,a,c)

The image of misplaced bar legend. 错位的酒吧传奇的形象。 As you can see i have define 12 labels from 'D' to 'BSMIX' but it is showing upto ABG. 正如您所看到的,我已经定义了从“D”到“BSMIX”的12个标签,但它显示为ABG。 错位标签图片

imshow usually restricts the range of your cmap to the values which appear in the image. imshow通常会将cmap的范围限制为图像中显示的值。 In order to force imshow to use all values of your cmap use vmin and vmax : 为了强制imshow使用你的cmap的所有值,使用vminvmax

img = plt.imshow(image, cmap=cmap, vmin=0, vmax=13)

In order to align the labels at the centers of the discrete colorbar fields, you have to define fixed number of ticks (to avoid sub-units) and use clim() . 为了在离散颜色条字段的中心对齐标签,您必须定义固定数量的刻度(以避免子单位)并使用clim()

For example, if there are N discrete levels to be shown in the colorbar: 例如,如果要在颜色栏中显示N离散级别:

cbar = plt.colorbar(ticks=range(N))
plt.clim(-0.5, N - 0.5)

It's old question but it's still difficult to find the correct answer, and none of the above worked for me. 这是一个古老的问题,但仍然很难找到正确的答案,并且以上都没有为我工作。

thanks a lot @plosner... I have little bit tweaked it it and it works... 非常感谢@plosner ...我有点调整它,它的工作原理......

 def plotimage(labels,image,imname):#input - array of labels and image of 1..n classes cmap = mpl.colors.ListedColormap(['r', 'g', 'b', 'c','y','k','#FFFF00','#00FF00','#ADBE34','#FF8C00','w','#FF8EFC','#00FFDD','#673E03','#ADEE12','m','#12ABDE','#22EEAB','#33DDEF'])#only 8 classes at present ,'#FF8EFC','#00FF00','#00FFDD''#962404' '#FF8EFC','#962404' #need to create pallate and labels later out of this def #cmap = mpl.colors.ListedColormap(['r', 'g', 'b', 'c','m','k','y','w','#7FFF00','#FF8C00','#FFFF00','#FE6D06','#6E6E6E','#04B4AE'])#only 8 classes at present ,'#FF8EFC','#00FF00','#00FFDD''#962404' '#FF8EFC','#962404' bounds = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] norm = mpl.colors.BoundaryNorm(bounds, cmap.N) print cmap.N img = plt.imshow(image, cmap=cmap , vmin = 1, vmax =20) cbar = plt.colorbar(img, cmap=cmap,norm=norm, boundaries=bounds, ticks=bounds) #norm=norm, boundaries=bounds, ticks=bounds cbar.ax.set_yticklabels(labels) plt.gcf().canvas.set_window_title("Classification") #imsave("expr1_im", image.reshape(image.shape[0]/256,256), cmap=cmap) #savefig(imname) plt.title(imname) plt.show() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM