1

我已经在网上搜索了近三个小时,现在需要一个关于 numpy 的问题的答案。

我正在将 .csv 文件中的数据读取到 numpy 数组中,以在不使用 matplotlib 的 3d 曲面图中绘制数据。数据采集​​是通过 python 的 csv 模块完成的。标题被读入一个数组,第一列被读入另一个数组,属于这两个轴值的其余数据被读入第三个数组。

首先,我监督我必须将“Y”数组转换为浮点数才能正确显示它,而无需混合“字符串”和“浮点数”数组,这是固定的。现在我的程序正确转换 Y 数组中的字符串,直到找到像“2.633e-12”这样的值,现在它说

ValueError:float() 的无效文字:2.673e-

当我在转换之前从数组中打印相应的值时,它会像上面显示的那样打印。

请参阅下面的完整程序代码...

我会很感激你能提供的任何帮助,所以提前谢谢...

蒂姆

编辑:

追溯:

Traceback (most recent call last):
  File "Velocities.py", line 49, in <module>
    surf = ax.plot_surface(X, Y, avel, rstride=1, cstride=1, cmap=cm.jet)
  File "/usr/bin/epd_free/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 1453, in plot_surface
    self.auto_scale_xyz(X, Y, Z, had_data)
  File "/usr/bin/epd_free/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 392, in auto_scale_xyz
    self.xy_dataLim.update_from_data_xy(np.array([x, y]).T, not had_data)
  File "/usr/bin/epd_free/lib/python2.7/site-packages/matplotlib/transforms.py", line 854, in update_from_data_xy
    path = Path(xy)
  File "/usr/bin/epd_free/lib/python2.7/site-packages/matplotlib/path.py", line 112, in __init__
    vertices = np.asarray(vertices, np.float_)
  File "/usr/bin/epd_free/lib/python2.7/site-packages/numpy/core/numeric.py", line 235, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: invalid literal for float(): 2.673e-

从我的输入文件产生错误的行之前和之后的行:

2.57e-12;-243.476064488;-243.476064488;-243.476064488;-243.476064488;-243.476064488;-243.476064488;-243.476064488;-243.476064488;-243.476064488;-243.476064488;-243.476064488;-243.476064488;-243.476064488;-243.476064488;-243.476064488;-243.476064488;-243.476064488;-243.476064488;-243.476064488;-243.476064488;-243.476064488;-243.476064488
2.673e-12;-243.747334317;-243.747334317;-243.747334317;-243.747334317;-243.747334317;-243.747334317;-243.747334317;-243.747334317;-243.747334317;-243.747334317;-243.747334317;-243.747334317;-243.747334317;-243.747334317;-243.747334317;-243.747334317;-243.747334317;-243.747334317;-243.747334317;-243.747334317;-243.747334317;-243.747334317
2.772e-12;-244.067736958;-244.067736958;-244.067736958;-244.067736958;-244.067736958;-244.067736958;-244.067736958;-244.067736958;-244.067736958;-244.067736958;-244.067736958;-244.067736958;-244.067736958;-244.067736958;-244.067736958;-244.067736958;-244.067736958;-244.067736958;-244.067736958;-244.067736958;-244.067736958;-244.067736958

我仍然会很感激任何答案,帮助我解决这个问题......

编辑2:

这是我发现的完整代码,转换为浮点数没有问题,而是表面图本身会导致回溯......

#!/usr/bin/epd_free/bin/python2.7
# -*- coding: utf-8 -*-

##all the necessary imports
import matplotlib as mpl
import matplotlib.pyplot as plt
#import math as m
import csv
import numpy as np
from matplotlib import cm
#from matplotlib.ticker import LinearLocator, FormatStrFormatter
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.ticker as ticker

##reading the data
#reading the velocity profiles from the "velProfiles"
eingabe = "velProfiles_kum.csv"

dat = csv.reader(open(str(eingabe), 'rb'), delimiter=';')

X=dat.next()
X.pop(0)
X=np.array(X)

Y=[]
avel=[]
for i,row in enumerate(dat):
    if i%100 == 0:
        Y.append(row.pop(0))
        avel.append([float(item) for item in row])#if item is not ''
del dat
#Y=np.array(Y)
Y=np.array(Y).astype(np.float64)
avel=np.array(avel) # converting the list into an array to be able to process it with the 3D plotting engine.

##creating the graph and the mesh needed to plot the data. (get the dimensions right!!!)
fig = plt.figure(frameon=False)
ax = fig.gca(projection='3d', lod=True)
X, Y = np.meshgrid(X, Y)

print("X: " + str(X.shape) + "| Y: " + str(Y.shape) + "| Z: " + str(avel.shape)) # to stdout to check the arrays' dimensions

#plotting the surface
surf = ax.plot_surface(X, Y, avel, rstride=1, cstride=1, cmap=cm.jet)

#setting the title and the labels
plt.xlabel("x")
plt.ylabel("t")
plt.title("The simulation's velocity profiles plotted over time and space")

#layout refinements
#plt.tight_layout()
#fig.colorbar(surf, shrink=.8, aspect=12) #create the colorbar after the tight_layout() command to prevent it from being drawn inside the figure.

#show it!
plt.show()
4

0 回答 0