以下是關於python繪畫的有用資料。
自定義COLORBAR
可參考http://matplotlib.org/examples/pylab_examples/custom_cmap.html
__author__ = 'user'
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
x = np.arange(0, np.pi, 0.1)
y = np.arange(0, 2*np.pi, 0.1)
X, Y = np.meshgrid(x, y)
Z = np.cos(X) * np.sin(Y) * 10
colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] # R -> G -> B
n_bin = 100 # Discretizes the interpolation into bins
cmap_name = 'my_list'
fig, ax = plt.subplots()
cm = LinearSegmentedColormap.from_list(cmap_name, colors, N=n_bin)
im = ax.imshow(Z, interpolation='nearest', origin='lower', cmap=cm)
ax.set_title("N bins: %s" % n_bin)
fig.colorbar(im, ax=ax)plt.show()
設定COLORBAR範圍
import matplotlib
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np
def draw950type3_num():
cslev = np.arange(0,30,0.5)
cmap=cm.coolwarm
cs = m.contourf(x,y,wind,cslev,cmap=cmap,extend='both')
cs.cmap.set_under('b')
cbar = m.colorbar(cs,location='bottom',pad="8%")
cbar.set_label('wind speed (m/s)')
自定義colormap&非線性colorbar
__author__ = 'user'
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
##--data--##
x=np.array([[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]])
y=np.array([[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]])
z=np.array([[12,15,19,20],[1,2,3,5],[1,1,2,3],[1,4,4,7]])
z=np.double(z)
##--my colormap--##
colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] # R -> G -> B
n_bin = 100 # Discretizes the interpolation into bins
cmap_name = 'my_list'
cm = LinearSegmentedColormap.from_list(cmap_name, colors, N=n_bin)
##--plot--##
fig, ax = plt.subplots()
ax.set_title("N bins: %s" % n_bin)
##--nonlinear colorbar--##
zmax=np.ceil(np.max(z))
newz=z
newz[np.where(z>=10)]=(z[np.where(z>=10)]-10)/(zmax-10)+10
ticksarr=[1,2,3,4,5,6,7,8,9,10,11]
cs=plt.contourf(x,y,newz,ticksarr,cm=cm)
cbar=fig.colorbar(cs, ax=ax)
cbar.set_ticks(ticksarr)
cbar.set_ticklabels(ticksarr[0:10]+[zmax])
plt.show()
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2QvwVe0lmdhJ3ZvwFM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2LcZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39TMyUTMycDMzIjNwATM2EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)