简体   繁体   中英

How do I import data in scientific notation for a plot?

So far I have been trying to import poorly formated scientific notation data into a plot using python. I have tried variants of

import matplotlib.pyplot as plt
import numpy as np

data = np.genfromtxt("/home/rex/Documents/ModLab/PhotoElec/Exp2Data.csv",delimiter=',', unpack=True, names=True)

plt.axis([-1,32, 0 , 203])

plt.title('All Aperture')
plt.xlabel('Voltage')
plt.ylabel('Current')
plt.plot(data, 'ro')

plt.show()

The data is in a csv file and looks like this but far longer.

I2,V2,I2,V2,I2,V2
0,-0.5,0,-1,0,-0.9
2.00E-011,0.5,1.00E-010,0,3.50E-010,0.1
5.00E-011,1.5,3.00E-010,1,1.55E-009,1.1

Also when I run the assign data file I get this weird error.

SyntaxError: EOL while scanning string literal

Any help would be appreciated.

I think your problem is in the actual import - it may have been imported as a string, not a number.

I'd suggest using pandas to handle the import. If you're doing scientific computing, you'll find pandas very useful. Your problem then becomes:

import pandas
data = pandas.read_csv('Exp2Data.csv')
i2 = data.I2
v2 = data.V2
# ... plot as needed

There may also be ways for pandas to handle the plotting as well!

I think that the problem comes from your header. Your header (first line) is a string and obviously, matplotlib can't plot this kind of data.

You have to make :

data = np.genfromtxt("/home/rex/Documents/ModLab/PhotoElec/Exp2Data.csv",
                     delimiter=',', 
                     unpack=True, 
                     names=True, 
                     skip_header = 1)
# skip_header let to go directly to your data and pass the header

I didn't run the script but it should work ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM