在python中绘制4D图

时间:2018-05-22 12:14:17

标签: python matplotlib plot

我知道我正在打死马,但我找不到合适的答案。

我想从大数据制作一个情节:

X-Coord   Y-Coord   Z-Coord   Value
20'000 rows

当我调用

X, Y = np.meshgrid(X, Y)

我收到错误:

  Traceback (most recent call last):
  File "new 1.py", line 17, in <module>
    X, Y = np.meshgrid(X, Y)  # <-- returns a 2D grid from initial 1D arrays
  File "C:\Program Files (x86)\lib\site-packages\numpy\lib\function_base.py", line 4698, in meshgrid
    output = [x.copy() for x in output]
  File "C:\Program Files (x86)\lib\site-packages\numpy\lib\function_base.py", line 4698, in <listcomp>
    output = [x.copy() for x in output]
MemoryError

实际上我只想从X Y Z坐标获得一个3D表面,并在此坐标中绘制值Value的值。请给我一个建议

UPD: 数据的例子

          X                       Y                       Z                     Value
-3.6296815834229800E+13 9.0179395964544800E+13  4.3243022996875400E+13  2.3293827867020395e-03
-3.6546185417114900E+13 8.9339697982272400E+13  4.3845054348437700E+13  2.2153085734286245e-03
-3.6546185417114900E+13 9.2024057357272400E+13  4.3845054348437700E+13  2.6335681277863542e-03
-3.6795554999999900E+13 8.8500000000000000E+13  4.4447085699999900E+13  2.2448110225069475e-03
-3.6795554999999900E+13 9.1184359374999900E+13  4.4447085699999900E+13  2.3661800082893664e-03
-3.6795554999999900E+13 9.3868718749999900E+13  4.4447085699999900E+13  3.1766708204588683e-03
-3.6296815834229800E+13 9.0179395964544800E+13  4.3243022996875400E+13  2.3293827867020395e-03
-3.6546185417114900E+13 9.2024057357272400E+13  4.3845054348437700E+13  2.6335681277863542e-03
-3.6337452147547400E+13 9.0857342861310100E+13  4.3341127722985300E+13  2.4227047423936087e-03
-3.6795554999999900E+13 9.3868718749999900E+13  4.4447085699999900E+13  3.1766708204588683e-03
-3.6586821730432500E+13 9.2702004254037600E+13  4.3943159074547600E+13  2.8272105071883709e-03
-3.6378088460865000E+13 9.1535289758075300E+13  4.3439232449095200E+13  2.5588155147357474e-03
-3.6378088460865000E+13 9.1535289758075300E+13  4.3439232449095200E+13  2.6341575316456271e-03
-3.6586821730432500E+13 9.2702004254037600E+13  4.3943159074547600E+13  2.8628186103490019e-03
-3.6491956423152800E+13 9.3434980585604900E+13  4.3714133992590100E+13  3.2129310022084552e-03
-3.6795554999999900E+13 9.3868718749999900E+13  4.4447085699999900E+13  3.1710978217960057e-03
-3.6700689692720300E+13 9.4601695081567300E+13  4.4218060618042400E+13  3.5478526100425675e-03
-3.6605824385440600E+13 9.5334671413134600E+13  4.3989035536084900E+13  3.9315260648101454e-03
-3.6196152400000000E+13 8.8500000000000000E+13  4.3000000000000000E+13  5.1978166985800038e-03
-3.6495853699999900E+13 8.8500000000000000E+13  4.3723542849999900E+13  4.8689291409766524e-03

UPD2: 感谢@ImportanceOfBeingErnest的优秀解决方案

最后在散射旋转期间加速数据处理我减少了数组并使用了另一个后端。还为结果添加了一个颜色栏。

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

import matplotlib
matplotlib.use('svg')

x,y,z,v = np.loadtxt("datafile.txt", skiprows=2, unpack=True)
if len(x) > 400000:
  x = x[::50] 
  y = y[::50] 
  z = z[::50]
  v = v[::50] 
elif len(x) > 200000:
  x = x[::20] 
  y = y[::20] 
  z = z[::20]
  v = v[::20] 
elif len(x) > 100000:
  x = x[::10] 
  y = y[::10] 
  z = z[::10]
  v = v[::10]   

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


ax.scatter(x,y,z,c=v, s=10, cmap=cm.rainbow)

m = cm.ScalarMappable(cmap=cm.rainbow)

m.set_array(v)
cbar = plt.colorbar(m)

plt.show()

1 个答案:

答案 0 :(得分:0)

您可以使用散点图根据表格的最后一列绘制带颜色的点;然后可以使用plot_trisurf来了解表面的概念。

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

x,y,z,v = np.loadtxt("datafile.txt", skiprows=1, unpack=True)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(x,y,z, edgecolor="gray", color="None")
ax.scatter(x,y,z,c=v, s=100)

enter image description here