Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中向3D绘图添加第四个变量_Python_Matplotlib_Plot_Mayavi_4d - Fatal编程技术网

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

在Python中向3D绘图添加第四个变量,python,matplotlib,plot,mayavi,4d,Python,Matplotlib,Plot,Mayavi,4d,我有3个不同的参数X,Y和Z在一个值范围内,对于这些参数的每一个组合,都有一个特定的V值。为了更清楚,数据看起来像这样 X Y Z V 1 1 2 10 1 2 3 15 etc... 我希望使用曲面/等高线图来可视化数据,使用V作为颜色来查看其在该点的值,但我不知道如何使用Python将自定义着色方案添加到混合中。有没有关于如何做到这一点的想法(或者这种可视化完全是愚蠢的) 非常感谢 这实际上取决于您计划如何绘制这些数据。我喜欢用gnuplot绘制图形:它简单、免费、直观。

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

X  Y  Z  V
1  1  2  10
1  2  3  15
etc...
我希望使用曲面/等高线图来可视化数据,使用V作为颜色来查看其在该点的值,但我不知道如何使用Python将自定义着色方案添加到混合中。有没有关于如何做到这一点的想法(或者这种可视化完全是愚蠢的)


非常感谢

这实际上取决于您计划如何绘制这些数据。我喜欢用
gnuplot
绘制图形:它简单、免费、直观。要使用
gnuplot
绘制示例,必须将这些行打印到一个文件中(仅包含这四列),并使用如下代码进行绘制

 reset
 set terminal png
 set output "out.png"
 splot "file.txt" using 1:2:3:4 with lines palette
假设您将数据保存到文件
file.txt
splot
表示曲面图。当然,这是一个最低限度的例子


或者,您可以使用
matplotlib
,但在我看来,这并不直观。虽然它的优点是将所有处理集中在python中。

但它实际上取决于您计划如何绘制这些数据。我喜欢用
gnuplot
绘制图形:它简单、免费、直观。要使用
gnuplot
绘制示例,必须将这些行打印到一个文件中(仅包含这四列),并使用如下代码进行绘制

 reset
 set terminal png
 set output "out.png"
 splot "file.txt" using 1:2:3:4 with lines palette
假设您将数据保存到文件
file.txt
splot
表示曲面图。当然,这是一个最低限度的例子


或者,您可以使用
matplotlib
,但在我看来,这并不直观。虽然它的优点是将所有处理集中在python中。

但它实际上取决于您计划如何绘制这些数据。我喜欢用
gnuplot
绘制图形:它简单、免费、直观。要使用
gnuplot
绘制示例,必须将这些行打印到一个文件中(仅包含这四列),并使用如下代码进行绘制

 reset
 set terminal png
 set output "out.png"
 splot "file.txt" using 1:2:3:4 with lines palette
假设您将数据保存到文件
file.txt
splot
表示曲面图。当然,这是一个最低限度的例子


或者,您可以使用
matplotlib
,但在我看来,这并不直观。虽然它的优点是将所有处理集中在python中。

但它实际上取决于您计划如何绘制这些数据。我喜欢用
gnuplot
绘制图形:它简单、免费、直观。要使用
gnuplot
绘制示例,必须将这些行打印到一个文件中(仅包含这四列),并使用如下代码进行绘制

 reset
 set terminal png
 set output "out.png"
 splot "file.txt" using 1:2:3:4 with lines palette
假设您将数据保存到文件
file.txt
splot
表示曲面图。当然,这是一个最低限度的例子


或者,您可以使用
matplotlib
,但在我看来,这并不直观。尽管它的优点是将所有处理集中在python中。

Matplotlib允许将FaceColor作为参数传递给例如。

这意味着您必须在计算机上执行2D插值 当前颜色数组,因为当前只有 矩形面的角(您提到过您有一条直线 网格)

你可以用 因此,正如您从文档中看到的,建议使用

给你一个简单的例子:

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\u 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
(使用
kind='linear'
更换
kx=1,ky=1
)。但是既然医生建议使用更快的 二维矩形样条线

现在,您可以绘制它了:

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的一个子集作为FaceColor(它似乎忽略了C的最后一列和最后一行)。这相当于在整个面片上仅使用由一个坐标(例如左上角)定义的颜色

一种更简单的方法是让matplotlib进行插值并获得面颜色,然后将其传递到真实的绘图:

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)

但是,这在版本中不起作用。Matplotlib允许将FaceColor作为参数传递给。

这意味着您必须在计算机上执行2D插值 当前颜色数组,因为当前只有 矩形面的角(您提到过您有一条直线 网格)

你可以用 因此,正如您从文档中看到的,建议使用

给你一个简单的例子:

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\u 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
(使用
kind='linear'
更换
kx=1,ky=1
)。但是既然医生建议使用更快的 二维矩形样条线

现在,您可以绘制它了:

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

正如你所看到的,在