python colorbar非线性映射,matplotlib的色彩映射

I'm new to python and after installing it I've accomplished to plot my 3d data using matplotlib. Sadly the only thing I don't know how to get done is the color part. My image just shows the surface but doesn't use the color bar at all. Here is my code.

from mpl_toolkits.mplot3d import Axes3D

from matplotlib import cm

from matplotlib.ticker import LinearLocator, FormatStrFormatter

import matplotlib.pyplot as plt

from matplotlib.mlab import griddata

import numpy as np

fig = plt.figure()

ax = fig.gca(projection='3d')

data = np.genfromtxt('Uizq.txt')

x = data[:,0]

y = data[:,1]

z = data[:,2]

xi = np.linspace(min(x), max(x))

yi = np.linspace(min(y), max(y))

ax.set_xlabel('X')

ax.set_ylabel('Y')

ax.set_zlabel('U')

X, Y = np.meshgrid(xi, yi)

Z = griddata(x, y, z, xi, yi)

ax.set_zlim3d(np.min(Z), np.max(Z))

surf = ax.plot_surface(X, Y, Z, rstride=2, cstride=2, cmap=cm.jet,

linewidth=0.5, antialiased=False)

fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

02c78689e46f54d2cc36764080de25bd.png

you can obviously see that it is all blue, and I want to relate the color with "U" using the full cm.jet spectrum. This might be a very noob question, so sorry if you rolled your eyes.

解决方案

Add the line

surf.set_clim([np.min(Z),np.max(Z)])

before you add the color bar.

It seems that the 3D plotting does not take into account the masking, so you are including NaN in the data, which confuses the automatic color limits.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值