python设置曲面图颜色范围_Python:具有密度颜色的曲面图(Python:Curved surface plot with density colors)...

One method is to manually create and plot a collection of triangles:

(Edit: manually creating and coloring triangles around bin midpoints)

import numpy

import matplotlib

matplotlib.use('Agg')

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

from matplotlib.tri import Triangulation

from mpl_toolkits.mplot3d.art3d import Poly3DCollection

import matplotlib.cm as cm

# Generate a dataset

R = 1

# bin midpoints

theta = numpy.linspace(numpy.pi/6, numpy.pi/3, 20) + numpy.pi / 2

phi = numpy.linspace(numpy.pi/6, numpy.pi/3, 20)

ttheta, pphi = numpy.meshgrid(theta, phi)

x = R * numpy.sin(ttheta) * numpy.cos(pphi)

y = R * numpy.sin(ttheta) * numpy.sin(pphi)

z = R * numpy.cos(ttheta)

n = numpy.exp(-(ttheta - numpy.pi/4 - numpy.pi/2)**2 * 20 - (pphi - numpy.pi/4)**2 * 20)

mappable = cm.ScalarMappable(cmap=cm.coolwarm, norm=matplotlib.colors.Normalize(vmin=0, vmax=1))

colors = mappable.to_rgba(n)

# Scatter plot

fig = plt.figure()

ax = fig.add_subplot(1, 1, 1, projection='3d')

ax.scatter(x.flatten(), y.flatten(), z.flatten(), c=colors.reshape(x.size, 4))

ax.set_xlim(0.2, 0.8)

ax.set_ylim(0.2, 0.8)

ax.set_zlim(-0.9, -0.45)

ax.elev = 50

fig.savefig('t.png')

# Surface plot

# bin vertex spherical coordinates

dtheta = theta[1] - theta[0]

dphi = phi[1] - phi[0]

v_theta = numpy.concatenate([theta - dtheta/2, numpy.array([theta[-1] + dtheta/2])])

v_phi = numpy.concatenate([phi - dphi/2, numpy.array([phi[-1] + dphi/2])])

# bin vertex Cartesian coordinates

v_ttheta, v_pphi = numpy.meshgrid(v_theta, v_phi)

vx = R * numpy.sin(v_ttheta) * numpy.cos(v_pphi)

vy = R * numpy.sin(v_ttheta) * numpy.sin(v_pphi)

vz = R * numpy.cos(v_ttheta)

# Creating triangles and corresponding face colors

triangles = []

facecolors = []

for i in range(v_theta.size - 1):

for j in range(v_phi.size - 1):

triangles.extend([

[(i, j), (i + 1, j), (i, j + 1)],

[(i + 1, j), (i + 1, j + 1), (i, j + 1)]])

facecolors.extend([

colors[i, j],

colors[i, j]

])

triangle_vertices = numpy.array(

[[[vx[i,j], vy[i,j], vz[i,j]] for i, j in t] for t in triangles])

coll = Poly3DCollection(triangle_vertices, facecolors=facecolors, edgecolors=(0,0,0,0))

fig = plt.figure()

ax = fig.add_subplot(1, 1, 1, projection='3d')

ax.add_collection(coll)

ax.set_xlim(0.2, 0.8)

ax.set_ylim(0.2, 0.8)

ax.set_zlim(-0.9, -0.45)

ax.elev = 50

fig.savefig('t2.png')

The scatter plot:

The surface plot:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值