반응형
링크 : https://stackoverflow.com/questions/18704353/correcting-matplotlib-colorbar-ticks
샘플 코드
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib as mpl
#mpl.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.colors as mcolors
tick_label = [ "{}".format(i) for i in range(35)]
def colorbar_index(ncolors, cmap, fig):
cmap = cmap_discretize(cmap, ncolors)
mappable = cm.ScalarMappable(cmap=cmap)
mappable.set_array([])
mappable.set_clim(-0.5, ncolors+0.5)
return mappable, cmap
def cmap_discretize(cmap, N):
"""Return a discrete colormap from the continuous colormap cmap.
cmap: colormap instance, eg. cm.jet.
N: number of colors.
Example
x = resize(arange(100), (5,100))
djet = cmap_discretize(cm.jet, 5)
imshow(x, cmap=djet)
"""
if type(cmap) == str:
cmap = plt.get_cmap(cmap)
colors_i = np.concatenate((np.linspace(0, 1., N), (0.,0.,0.,0.)))
colors_rgba = cmap(colors_i)
colors_rgba[0] = [0,0,0,1]
indices = np.linspace(0, 1., N+1)
cdict = {}
for ki,key in enumerate(('red','green','blue')):
cdict[key] = [ (indices[i], colors_rgba[i-1,ki],
colors_rgba[i,ki]) for i in xrange(N+1) ]
# Return colormap object.
return mcolors.LinearSegmentedColormap(cmap.name +
"_%d"%N, cdict, 1024)
f = plt.figure()
axarr = []
axarr.append(f.add_subplot(4,1,1))
axarr.append(f.add_subplot(4,1,2))
axarr.append(f.add_subplot(4,1,3))
#axarr.append(f.add_subplot(3,1, 2))
ncolors = 34
mappable, cmap = colorbar_index(ncolors=ncolors, cmap=plt.cm.Paired, fig=f)
for i in range(3):
m_input_list = [range(0,ncolors),range(0,ncolors),range(0,ncolors)]
cax = axarr[i].matshow(m_input_list, cmap=cmap, vmin=0, vmax=33,
interpolation='none')
axarr[i].axis('tight')
axarr[i].tick_params(labeltop= False,
labelbottom= False,
labelleft=False,
labelright=False,
left=False,
right=False,
bottom=False,
top=False)
axarr[i].set_ylabel('Raw data', fontsize=7)
# axarr[i].set_xticks(range(0,ncolors))
# axarr[i].set_xticklabels(["{}".format(x)
for x in range(0, ncolors)], size=6)
f.subplots_adjust(right=0.78, left=0.05)
cbar_ax = f.add_axes([0.80,0.05,0.02,0.9])
colorbar = plt.colorbar(mappable, cax=cbar_ax)
colorbar.ax.tick_params(labelsize=7)
colorbar.set_ticks(np.linspace(0, ncolors, ncolors))
colorbar.set_ticklabels(tick_label)
plt.show()
반응형
'Programming > Python' 카테고리의 다른 글
VS Code anaconda 연동 (0) | 2017.06.28 |
---|---|
matplotlib 한글 문제 (1) | 2017.06.19 |
[파이썬] 꿀팁 (0) | 2017.05.04 |
라즈베리파이 picamera h264 to mp4 변환 (0) | 2017.05.03 |
python string to function call (0) | 2017.04.26 |