如何使用 Python 和 matplotlib 绘制 4D 绘图

发布于 2024-12-11 16:36:09 字数 1168 浏览 0 评论 0原文

我正在寻找一种使用 Python 和 matplotlib 创建四维图(曲面加色标)的方法。我能够使用前三个变量生成曲面,但我没有成功添加第四个变量的色标。下面是我的数据的一小部分。任何帮助将不胜感激。感谢

数据子集

var1    var2    var3    var4
10.39   73.32   2.02    28.26
11.13   68.71   1.86    27.83
12.71   74.27   1.89    28.26
11.46   91.06   1.63    28.26
11.72   85.38   1.51    28.26
13.39   78.68   1.89    28.26
13.02   68.02   2.01    28.26
12.08   64.37   2.18    28.26
11.58   60.71   2.28    28.26
8.94    65.67   1.92    27.04
11.61   59.57   2.32    27.52
19.06   74.49   1.69    63.35
17.52   73.62   1.73    63.51
19.52   71.52   1.79    63.51
18.76   67.55   1.86    63.51
19.84   53.34   2.3     63.51
20.19   59.82   1.97    63.51
17.43   57.89   2.05    63.38
17.9    59.95   1.89    63.51
18.97   57.84   2       63.51
19.22   57.74   2.05    63.51
17.55   55.66   1.99    63.51
19.22   101.31  6.76    94.29
19.41   99.47   6.07    94.15
18.99   94.01   7.32    94.08
19.88   103.57  6.98    94.58
19.08   95.38   5.66    94.14
20.36   100.43  6.13    94.47
20.13   98.78   7.37    94.47
20.36   89.36   8.79    94.71
20.96   84.48   8.33    94.01
21.02   83.97   6.78    94.72
19.6    95.64   6.56    94.57

I am looking for a way to create four-dimensional plots (surface plus a color scale) using Python and matplotlib. I am able to generate the surface using the first three variables, but I am not having success adding the color scale for the fourth variable. Here is a small subset of my data below. Any help would be greatly appreciated. Thanks

Data Subset

var1    var2    var3    var4
10.39   73.32   2.02    28.26
11.13   68.71   1.86    27.83
12.71   74.27   1.89    28.26
11.46   91.06   1.63    28.26
11.72   85.38   1.51    28.26
13.39   78.68   1.89    28.26
13.02   68.02   2.01    28.26
12.08   64.37   2.18    28.26
11.58   60.71   2.28    28.26
8.94    65.67   1.92    27.04
11.61   59.57   2.32    27.52
19.06   74.49   1.69    63.35
17.52   73.62   1.73    63.51
19.52   71.52   1.79    63.51
18.76   67.55   1.86    63.51
19.84   53.34   2.3     63.51
20.19   59.82   1.97    63.51
17.43   57.89   2.05    63.38
17.9    59.95   1.89    63.51
18.97   57.84   2       63.51
19.22   57.74   2.05    63.51
17.55   55.66   1.99    63.51
19.22   101.31  6.76    94.29
19.41   99.47   6.07    94.15
18.99   94.01   7.32    94.08
19.88   103.57  6.98    94.58
19.08   95.38   5.66    94.14
20.36   100.43  6.13    94.47
20.13   98.78   7.37    94.47
20.36   89.36   8.79    94.71
20.96   84.48   8.33    94.01
21.02   83.97   6.78    94.72
19.6    95.64   6.56    94.57

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪我终i 2024-12-18 16:36:09

要创建所需的绘图,我们需要使用 matplotlib 的plot_surface 来绘制 Z(X,Y) 曲面,然后使用关键字参数facecolors 为每个补丁传递一个新颜色。

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

# create some fake data
x = y = np.arange(-4.0, 4.0, 0.02)
# here are the x,y and respective z values
X, Y = np.meshgrid(x, y)
Z = np.sinc(np.sqrt(X*X+Y*Y))
# this is the value to use for the color
V = np.sin(Y)

# create the figure, add a 3d axis, set the viewing angle
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.view_init(45,60)

# here we create the surface plot, but pass V through a colormap
# to create a different color for each patch
ax.plot_surface(X, Y, Z, facecolors=cm.Oranges(V))

输入图片描述这里

To create the plot you want, we need to use matplotlib's plot_surface to plot Z vs (X,Y) surface, and then use the keyword argument facecolors to pass in a new color for each patch.

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

# create some fake data
x = y = np.arange(-4.0, 4.0, 0.02)
# here are the x,y and respective z values
X, Y = np.meshgrid(x, y)
Z = np.sinc(np.sqrt(X*X+Y*Y))
# this is the value to use for the color
V = np.sin(Y)

# create the figure, add a 3d axis, set the viewing angle
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.view_init(45,60)

# here we create the surface plot, but pass V through a colormap
# to create a different color for each patch
ax.plot_surface(X, Y, Z, facecolors=cm.Oranges(V))

enter image description here

昵称有卵用 2024-12-18 16:36:09

尝试手动设置面部颜色时出现奇怪的错误:

raise ValueError("RGBA sequence should have length 3 or 4")
ValueError: RGBA sequence should have length 3 or 4

我的数据集是 717x26,x、y、z 和 fcolor 的形状是 (26,717)、(26,717)、(26,717) 和 (26,717,4 )。据我了解facecolors属性,形状应该没问题。

此处给出了绘图代码:

# mesh the x-y coodrdinates
x, y = np.meshgrid(x, y)

# Create a 3D plot
ax = fig.add_subplot(projection='3d')

# Creat the mapper for the 4D
mapper = cm.ScalarMappable(norm=norm, cmap='viridis')
mapper.set_array([])

# Map the data to RGBA values
fcolors = mapper.to_rgba(np.transpose(WSS))

# Output the dimensions
print('fcolors: ',fcolors.shape,', x, y, z: ',x.shape,' - ',y.shape,' - ',np.transpose(z).shape)

# Plot the surface incl. the 4th dimension
surf = ax.plot_surface(x, y, np.transpose(z), facecolor=fcolors)

有人知道为什么我在这种情况下会收到 RGBA 错误吗?
上面提供的所有示例都可以正常工作。

I'm getting a weird error when trying to set the facecolors manually:

raise ValueError("RGBA sequence should have length 3 or 4")
ValueError: RGBA sequence should have length 3 or 4

My data set is 717x26 and the shapes of x, y, z, and fcolors are (26,717), (26,717), (26,717), and (26,717,4). As far as I understood the facecolors property, the shape should be fine.

The code for plotting is given here:

# mesh the x-y coodrdinates
x, y = np.meshgrid(x, y)

# Create a 3D plot
ax = fig.add_subplot(projection='3d')

# Creat the mapper for the 4D
mapper = cm.ScalarMappable(norm=norm, cmap='viridis')
mapper.set_array([])

# Map the data to RGBA values
fcolors = mapper.to_rgba(np.transpose(WSS))

# Output the dimensions
print('fcolors: ',fcolors.shape,', x, y, z: ',x.shape,' - ',y.shape,' - ',np.transpose(z).shape)

# Plot the surface incl. the 4th dimension
surf = ax.plot_surface(x, y, np.transpose(z), facecolor=fcolors)

Does anybody know why I'm getting the RGBA error in that case?
All the examples provided above work without problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文