python繪畫

以下是關於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()



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值