python繪畫

来源:互联网 发布:csol游戏数据异常 编辑:程序博客网 时间:2024/06/05 22:42

以下是關於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 npimport matplotlib.pyplot as pltfrom 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 -> Bn_bin = 100 # Discretizes the interpolation into binscmap_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=znewz[np.where(z>=10)]=(z[np.where(z>=10)]-10)/(zmax-10)+10ticksarr=[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()



0 0
原创粉丝点击