Although there are many topics on this error I cant find any that have a solution that will help me. I have headers labelled bottom, left, and right in my .csv Excel file, when I try to plot them I get a could not convert string to
text error due to these headers. How could I solve this?
import matplotlib.pyplot as plt
import numpy as np
# Read the input data only once
Bottom, Left, Right = np.loadtxt ("C:Data 2.csv", delimiter=",", skiprows=1, unpack=True)
# Plot in the first axis
ax1.plot(Bottom, Left, label='Pressure/area', color='b')
plt.show()
This is what the file looks like:
one of the other approches that you can take is to read the csv file throught pandas using pd.read_csv. This will help you solve your problem.
for example:-
if my filepath is 'example/path/pathtofile'
import pandas as pd
filepath = 'example/path/pathtofile'
data = pd.read_csv(filepath)
Related
I've exported an Excel into a CSV where all the columns and entires look correct and normal. However, when I put it into a data frame and print the head, the structure becomes very messy and unreadable due to columns being unstructured.
As you can see in the image, the values are not neatly under user_id.
https://imgur.com/a/gbWaTwi
I'm using the following code:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import warnings
warnings.filterwarnings("ignore")
then
df1 = pd.read_csv('../doc.csv', low_memory=False)
df1.head
Do this --- print the invocation of head. Just saying .head isn't enough.
print(df1.head())
I have .dat file that I want to use in my script which draws scatter graph with data input from that .dat file. I have been manually converting .dat files to .csv for this purpose but I find it not satisfactory.
This is what I am using currently.
import pandas as pd import matplotlib.pyplot as plt import numpy as np
filename=raw_input('Enter filename ')
csv = pd.read_csv(filename)
data=csv[['deformation','stress']]
data=data.astype(float)
x=data['deformation']
y=data['stress']
plt.scatter(x,y,s=0.5)
fit=np.polyfit(x,y,15)
p=np.poly1d(fit)
plt.plot(x,p(x),"r--")
plt.show()
Programmer friend told me that it would be more convenient to convert it to JSON and use it as such. How would I go about this?
try using the numpy read feature
import numpy as np
yourArray = np.fromfile('YourData.dat',dtype=dtype)
yourArray = np.loadtxt('YourData.dat')
loadtxt is more flexible than fromfile
What i am trying to do is slightly basic, however i am very new to python, and am having trouble.
Goal: is to plot the yellow highlighted Row(which i have highlighted, however it will not be highlighted when i need to read the data) on the Y-Axis and plot the "Time" Column on the X-Axis.
Here is a photo of the Data, and then the code that i have tried along with its error.
Code
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
#Reading CSV and converting it to a df(Data_Frame)
df1 = pd.read_csv('Test_Sheet_1.csv', skiprows = 8)
#Creating a list from df1 and labeling it 'Time'
Time = df1['Time']
print(Time)
#Reading CSV and converting it to a df(Data_Frame)
df2 = pd.read_csv('Test_Sheet_1.csv').T
#From here i need to know how to skip 4 lines.
#I need to skip 4 lines AFTER the transposition and then we can plot DID and
Time
DID = df2['Parameters']
print(DID)
Error
As you can see from the code, right now i am just trying to print the Data so that i can see it, and then i would like to put it onto a graph.
I think i need to use the 'skiplines' function after the transposition, so that python can know where to read the "column" labeled parameters(its only a column after the Transposition), However i do not know how to use the skip lines function after the transposition unless i transpose it to a new Excel Document, but this is not an option.
Any help is very much appreciated,
Thank you!
Update
This is the output I get when I add print(df2.columns.tolist())
I have been using OpenPyxl for creating Excel workbooks using data from other CSV files.
Currently I want to insert a histogram into the worksheet based on a numerical list that I have as variable x below.
I cannot find an efficient way to generate the histogram, the option I opted for was generate the histogram in matplotlib save and then place in worksheet, however this seems combersome and I feel like I am missing some synthax to directly pass the plt to the img.
The option using Reference seems imperfect also as I have 10^6 length vectors and would rather not write them to this file.
import numpy as np
import openpyxl
import matplotlib.pyplot as plt
wb = openpyxl.Workbook()
ws = wb.active
x = np.random.rand(100)
plt.clf()
plt.hist(x, bins=10)
plt.savefig('temp1.png')
img = openpyxl.drawing.image.Image('temp1.png',size=(300,300))
img.anchor(ws.cell('A1'))
ws.add_image(img)
plt.clf()
plt.plot(x)
plt.savefig('temp2.png')
img = openpyxl.drawing.image.Image('temp2.png',size=(300,300))
img.anchor(ws.cell('A15'))
ws.add_image(img)
wb.save("trial.xlsx")
As you can see this generates two .png files and overall seems unclean. I do not think the preformance is taking much of a hit but undoubtedly will have better solutions and optimization is valued here.
I would treat answers of the form: "Swap from using OpenPyxl to ..." as a last resort only.
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 ;)