0

Recently I have been working on a code and got stuck for days on this error. Basically the program plots a 3D colormap from csv file. I am using Python 3 with anaconda3. https://drive.google.com/drive/folders/1hfL_TbfWwD6uZCgxiOa-xjWT1ChL2PUs?usp=sharing


This is the code:

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

DataX_Y_1D = np.loadtxt("datacsv_1d_xy.csv", delimiter=",")

X, Y = np.meshgrid(DataX_Y_1D[:,0], DataX_Y_1D[:,1])

Z = np.loadtxt("datacsv_2d_Z.csv", delimiter=",")

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

ax.plot_surface(X, Y, Z)

plt.show()

The problem like this:

File "/Users/lenguyen/Desktop/test_3D/plot_3D_4.py", line 1, in <module>
    DataAll1D = np.loadtxt("datacsv_1d.csv", delimiter=",")
NameError: name 'np' is not defined
(base) lenguyen@ntmle2 test_3D % python plot_3D_4.py
Traceback (most recent call last):
  File "/Users/lenguyen/Desktop/test_3D/plot_3D_4.py", line 5, in <module>
    DataAll1D = np.loadtxt("datacsv_1d.csv", delimiter=",")
  File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 1148, in loadtxt
    for x in read_data(_loadtxt_chunksize):
  File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in read_data
    items = [conv(val) for (conv, val) in zip(converters, vals)]
  File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in <listcomp>
    items = [conv(val) for (conv, val) in zip(converters, vals)]
  File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 736, in floatconv
    return float(x)
ValueError: could not convert string to float: '\ufeff9.9'
(base) lenguyen@ntmle2 test_3D % python plot_3D_4.py
Traceback (most recent call last):
  File "/Users/lenguyen/Desktop/test_3D/plot_3D_4.py", line 5, in <module>
    DataAll1D = np.loadtxt("datacsv_1d.csv", delimiter=",")
  File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 1148, in loadtxt
    for x in read_data(_loadtxt_chunksize):
  File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in read_data
    items = [conv(val) for (conv, val) in zip(converters, vals)]
  File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in <listcomp>
    items = [conv(val) for (conv, val) in zip(converters, vals)]
  File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 736, in floatconv
    return float(x)
ValueError: could not convert string to float: '\ufeff9.9'
(base) lenguyen@ntmle2 test_3D % python plot_3D_4.py
Traceback (most recent call last):
  File "/Users/lenguyen/Desktop/test_3D/plot_3D_4.py", line 5, in <module>
    DataAll1D = np.loadtxt("datacsv_1d.csv", delimiter=",")
  File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 1148, in loadtxt
    for x in read_data(_loadtxt_chunksize):
  File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in read_data
    items = [conv(val) for (conv, val) in zip(converters, vals)]
  File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in <listcomp>
    items = [conv(val) for (conv, val) in zip(converters, vals)]
  File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 736, in floatconv
    return float(x)
ValueError: could not convert string to float: '\ufeff9.9'
(base) lenguyen@ntmle2 test_3D % python plot_3D_4.py
Traceback (most recent call last):
  File "/Users/lenguyen/Desktop/test_3D/plot_3D_4.py", line 5, in <module>
    DataAll1D = np.loadtxt("datacsv_1d.csv", dtype= "float", delimiter=",")
  File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 1148, in loadtxt
    for x in read_data(_loadtxt_chunksize):
  File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in read_data
    items = [conv(val) for (conv, val) in zip(converters, vals)]
  File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in <listcomp>
    items = [conv(val) for (conv, val) in zip(converters, vals)]
  File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 736, in floatconv
    return float(x)
ValueError: could not convert string to float: '\ufeff10'
r-beginners
  • 31,170
  • 3
  • 14
  • 32
EL-san
  • 29
  • 4

1 Answers1

0

you need to define np

import numpy as np

I was able to import your file with:

x,y = np.genfromtxt('test.csv', delimiter=',', unpack=True, skip_header=0)

Loading z data:

z_all = np.genfromtxt('datacsv_2d_z.csv', delimiter=',', unpack=True, skip_header=0)

A quick plot:

plt.imshow(z_all)

Gives:
enter image description here

3D colormap graph:

import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np

fig, ax = plt.subplots(subplot_kw={"projection": "3d"},  figsize=(10, 10) )

X, Y = np.genfromtxt('datacsv_1d_xy.csv', delimiter=',', unpack=True, skip_header=0)
X, Y = np.meshgrid(X, Y)

Z = np.genfromtxt('datacsv_2d_z.csv', delimiter=',', unpack=True, skip_header=0)

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)

enter image description here

Chris Seeling
  • 606
  • 4
  • 11