ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

在Python中向3D图添加第4个变量

2019-10-10 05:57:50  阅读:385  来源: 互联网

标签:mayavi python matplotlib plot 4d


我在一个值范围内有3个不同的参数X,Y和Z,对于这些值的每种组合,它们都具有一定的V值.为了更清楚一点,数据看起来像这样.

X  Y  Z  V
1  1  2  10
1  2  3  15
etc...

我想使用表面/轮廓图来可视化数据,使用V作为颜色来查看该点的值,但是我看不到如何使用Python将我的自定义着色方案添加到混合中.关于如何执行此操作的任何想法(或者此可视化完全愚蠢)?

非常感谢!

解决方法:

Matplotlib允许将facecolors作为参数传递给例如
ax.plot_surface.

这意味着您必须在自己的电脑上执行2D插值
当前的颜色数组,因为您当前仅在
矩形面的角(您确实提到您有一个直线
格).

你可以用
scipy.interpolate.interp2d
为此,但是正如您从文档中看到的,建议使用
scipy.interpolate.RectBivariateSpline.

给你一个简单的例子:

import numpy as np
y,x = np.mgrid[1:10:10j, 1:10:10j] # returns 2D arrays
# You have 1D arrays that would make a rectangular grid if properly reshaped.
y,x = y.ravel(), x.ravel()  # so let's convert to 1D arrays
z = x*(x-y)
colors = np.cos(x**2) - np.sin(y)**2

现在,我有一个与您相似的数据集(x,y,z和
颜色).请注意,颜色是为
每个点(x,y).但是,当您要使用plot_surface进行绘制时,
生成矩形斑块,其角由那些点给出.

因此,接着进行插值:

from scipy.interpolate import RectBivariateSpline
# from scipy.interpolate import interp2d # could 've used this too, but docs suggest the faster RectBivariateSpline

# Define the points at the centers of the faces:
y_coords, x_coords = np.unique(y), np.unique(x)
y_centers, x_centers = [ arr[:-1] + np.diff(arr)/2 for arr in (y_coords, x_coords)]

# Convert back to a 2D grid, required for plot_surface:
Y = y.reshape(y_coords.size, -1)
X = x.reshape(-1, x_coords.size)
Z = z.reshape(X.shape)
C = colors.reshape(X.shape)
#Normalize the colors to fit in the range 0-1, ready for using in the colormap:
C -= C.min()
C /= C.max()

interp_func = RectBivariateSpline(x_coords, y_coords, C.T, kx=1, ky=1) # the kx, ky define the order of interpolation. Keep it simple, use linear interpolation.

在最后一步中,您可能还已经使用过interp2d(with kind =’linear’
替换kx = 1,ky = 1).但是由于文档建议使用更快
RectBivariateSpline …

现在您可以绘制它了:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.cm as cm

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
r = ax.plot_surface(X,Y,Z,
    facecolors=cm.hot(interp_func(x_centers, y_centers).T),
    rstride=1,  cstride=1) # only added because of this very limited dataset

如您所见,面上的颜色与数据集的高度无关.

请注意,您可能以为只需将2D数组C传递给facecolors就可以了,而matplotlib不会抱怨.但是,结果是不准确的,因为matplotlib将仅使用C的一部分作为面色(它似乎忽略了C的最后一列和最后一行).这等效于仅使用由整个色块中一个坐标(例如,左上角)定义的颜色.

一种更简单的方法是让matplotlib进行插值并获取facecolor,然后将其传递给实际图:

r = ax.plot_surface(X,Y,C, cmap='hot') # first plot the 2nd dataset, i.e. the colors
fc = r.get_facecolors()
ax.clear()
ax.plot_surface(X, Y, Z, facecolors=fc)

但是,由于this recently submitted bug,在< = 1.4.1版本中将不起作用.

标签:mayavi,python,matplotlib,plot,4d
来源: https://codeday.me/bug/20191010/1884692.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有