【问题标题】:matplotlib 3d -- inserting datamatplotlib 3d——插入数据
【发布时间】:2018-01-02 09:17:48
【问题描述】:

enter image description here我是编程初学者。我是 2 个月前开始的,所以请耐心等待我 :-) 所以我想在 python 3.4 上用 matplotlib 制作一个 3d 曲面图。

我看了很多关于这个的教程,但我没有找到完全像我需要做的事情..我希望你能帮助我。 在他们用meshgrid给出的所有视频中,3轴(x,y,z)之间的关系,但我不想要那个。我想做的是: 我有 16 个传感器,它们被放置在 4 排上,每排有 4 个传感器,所以第一排是 1、2、3、4,第二排是 5、6、7、8,依此类推。(传感器的顺序非常重要)。例如传感器编号 4 = 200,从 0 到 800 的 skala ..我认为仅将 x 和 y 轴用于图中的正确位置..例如放置传感器 4(= 800 中的 200)在第四列的第一行...所以..x=4 和 y=1 和 z=200 从 800 像以前一样。所以最后每个传感器只有一个“真实”值..z..

如何使用 matplotlib 为所有 16 个传感器导入此类数据以制作 3d 绘图?我真的很感激任何帮助..

【问题讨论】:

  • 你能试着澄清你的解释吗?我不跟。该行是什么意思:Sensor Number 4 = 200 from a skala from 0 until 800
  • 你首先要了解你有什么样的数据,你有没有三个一维数组,有3个元素的元组,...?
  • 我的意思是 z 可以取 0 到 800 之间的值,在这个例子中是 400。
  • 我希望现在我上传的图片你能明白我的意思!!感谢 cmets

标签: python matplotlib graph 3d


【解决方案1】:

你需要从某个地方开始。因此,假设数据是 16 个值的列表。然后,您可以创建一个二维数组并将该数组显示为图像。

import numpy as np
import matplotlib.pyplot as plt

# input data is a list of 16 values, 
# the first value is of sensor 1, the last of sensor 16
input_data = [200,266,350,480,
              247,270,320,511,
              299,317,410,500,
              360,360,504,632]
# create numpy array from list and reshape it to a 4x4 matrix
z = np.array(input_data).reshape(4,4)
# at this point you can already show an image of the data
plt.imshow(z)
plt.colorbar()

plt.show()

现在,将值绘制为 3D 图中的高度而不是 2D 图中的颜色的选项是使用bar3d 图。

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

# input data is a list of 16 values, 
# the first value is of sensor 1, the last of sensor 16
input_data = [200,266,350,480,
              247,270,320,511,
              299,317,410,500,
              360,360,504,632]

# create a coordinate grid
x,y = np.meshgrid(range(4), range(4))

ax = plt.gcf().add_subplot(111, projection="3d")
#plot the values as 3D bar graph
# bar3d(x,y,z, dx,dy,dz) 
ax.bar3d(x.flatten(),y.flatten(),np.zeros(len(input_data)), 
         np.ones(len(input_data)),np.ones(len(input_data)),input_data)

plt.show()

您也可以绘制曲面图,但在这种情况下,网格将定义曲面图块的边缘。

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

# input data is a list of 16 values, 
# the first value is of sensor 1, the last of sensor 16
input_data = [200,266,350,480,
              247,270,320,511,
              299,317,410,500,
              360,360,504,632]

# create a coordinate grid
x,y = np.meshgrid(range(4), range(4))
z = np.array(input_data).reshape(4,4)

ax = plt.gcf().add_subplot(111, projection="3d")
#plot the values as 3D surface plot 
ax.plot_surface(x,y,z)

plt.show()

【讨论】:

  • 非常感谢!!这对我帮助很大..真的!!但不是使用 bar3d plot..是否也可以使用这些数据制作 3d 曲面图?谢谢
  • 是的,但它不是真的可读,看图,谁能猜到这绘制了 16 个不同的传感器数据?当然这是你的选择。
猜你喜欢
  • 2012-02-27
  • 1970-01-01
  • 2016-10-19
  • 1970-01-01
  • 2020-06-04
  • 2017-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多