【问题标题】:Plot Wireframe out of data not working用数据绘制线框不起作用
【发布时间】:2018-03-13 18:13:22
【问题描述】:

我尝试为 matplotlib 编写一个脚本,其中数据点被绘制为线框。我在 gnuplot 中做过一次(这是有效的)。不过,我正在将我的一些脚本迁移到 matplotlib,因为我现在更相信它了。

我在圆柱坐标系中计算了一些数据。对于固定的 z,我根据 phi 角计算了 x 和 y 坐标。画完一个完整的圆后,计算下一个 z 步长等。数据看起来像这样

  6.71570E+01  0.00000E+00  6.00000E+01  4.46028E+01
  6.70761E+01  3.29524E+00  6.00000E+01  4.46028E+01
  6.68336E+01  6.58254E+00  6.00000E+01  4.46028E+01
  6.64301E+01  9.85398E+00  6.00000E+01  4.46028E+01
  6.58666E+01  1.31017E+01  6.00000E+01  4.46028E+01
  6.51444E+01  1.63178E+01  6.00000E+01  4.46028E+01
  6.42653E+01  1.94947E+01  6.00000E+01  4.46028E+01
  6.32313E+01  2.26245E+01  6.00000E+01  4.46028E+01
  [...]
  6.68336E+01 -6.58254E+00  6.00000E+01  4.46028E+01
  6.70761E+01 -3.29524E+00  6.00000E+01  4.46028E+01
  6.71570E+01 -8.51512E-13  6.00000E+01  4.46028E+01

  6.34799E+01  0.00000E+00  5.70000E+01  4.21513E+01
  6.34035E+01  3.11481E+00  5.70000E+01  4.21513E+01
  6.31742E+01  6.22212E+00  5.70000E+01  4.21513E+01
  6.27928E+01  9.31444E+00  5.70000E+01  4.21513E+01
  6.22602E+01  1.23843E+01  5.70000E+01  4.21513E+01
  6.15775E+01  1.54244E+01  5.70000E+01  4.21513E+01
  6.07465E+01  1.84272E+01  5.70000E+01  4.21513E+01
  5.97691E+01  2.13857E+01  5.70000E+01  4.21513E+01
  5.86478E+01  2.42927E+01  5.70000E+01  4.21513E+01
  5.73852E+01  2.71412E+01  5.70000E+01  4.21513E+01
  5.59843E+01  2.99242E+01  5.70000E+01  4.21513E+01
  5.44485E+01  3.26352E+01  5.70000E+01  4.21513E+01
  5.27816E+01  3.52676E+01  5.70000E+01  4.21513E+01
  [...]

需要空白行才能让 gnuplot 绘制线框,此处描述(lowrank.net/gnuplot/datafile-e.html,请参阅“3 维数据”)。只需在 gnuplot 中调用 'splot' 即可绘制我数据的正确线框。

GnuPlot plot of my wireframe

当尝试使用以下代码在 matplotlib 中绘制它时:

import matplotlib.pyplot as plt
import numpy as np

from mpl_toolkits.mplot3d import Axes3D

plt.close('all')
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')

file_in = open('wireframe_data.dat')
with file_in as file:
    data_mani = [[np.double(digit) for digit in line.split()] for line in file]
file_in.close()

data_mani = list(filter(None, data_mani)) # remove blank lines, fastest way
data_mani = np.array(data_mani, dtype='float64')

ax.plot_wireframe(data_mani[:, 0], data_mani[:, 1], data_mani[:, 2], rcount = 10, ccount = 10, linewidth=0.7)

它只在固定 z 坐标处绘制圆,并在 z 方向上沿着圆绘制一条线,即不是一个完整的线框。结果是这样的

Matplotlib plot of my wireframe (different data, but same structure)

我试图弄清楚如何在 python 中正确绘制它,但我无法让它与不同的网格网格调用一起工作。目前我什至不确定是否需要网格网格,因为表面数据已经存在。也许有人有一个想法,可以在这里帮助我?我现在真的很沮丧。

期待您的回答。

最好的祝愿, 橘子人

【问题讨论】:

    标签: python matplotlib grid gnuplot wireframe


    【解决方案1】:

    好的,我找到了解决方案。我必须通过以下方式重塑我的数据:

    [...]
    num_z = len(np.unique(data_mani[:, 2]))
    points_per_z = len(data_mani) / num_z
    
    XX = np.reshape(data_mani[:, 0], (num_z, points_per_z))
    YY = np.reshape(data_mani[:, 1], (num_z, points_per_z))
    ZZ = np.reshape(data_mani[:, 2], (num_z, points_per_z))
    
    ax.plot_wireframe(XX, YY, ZZ)
    

    现在看起来像this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-20
      • 2016-06-04
      相关资源
      最近更新 更多