Python Forum
Simple plotting from txt file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple plotting from txt file
#1
I have a very simple code to plot. The data file consists of seven columns with values (not integers), and I wish to plot their division. This is the code:

import pandas as pd
data = pd.read_csv('Co_foil_2.data',sep='\s+',header=None)
data = pd.DataFrame(data)

import matplotlib.pyplot as plt
y = data[0]
a = (data[1]/data[2])
b = (data[2]/data[2])
c = (data[3]/data[2])
d = (data[4]/data[2])
e = (data[5]/data[2])
f = (data[6]/data[2])

plt.plot(a, y,'r--')
plt.plot(b, y,'b--')
plt.plot(c, y,'g--')
plt.plot(d, y,'y--')
plt.plot(e, y,'m--')
plt.plot(f, y,'k--')
plt.legend()
plt.show()
However, this returns an error:

TypeError: unsupported operand type(s) for /: 'str' and 'str'
From what I understand, the columns can't be divided for some reason; am I simply using a wrong command, or is there something more prominently wrong in the code?
Reply
#2
After you read_csv, what is in data? After you convert to DataFrame what is in data? If you type print(data[0], type(data[0])) what does it say? Something is preventing your data from being interpreted as numbers. Find out where that is happening and why.
Reply
#3
Thanks, I found the error: the first column(header) didn't include numbers so division couldn't be performed.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Plotting simple graph (AttributeError) Laplace12 0 1,344 Jul-28-2021, 10:58 AM
Last Post: Laplace12
  [Solved] Plotting data from txt file Laplace12 1 1,776 Jul-06-2021, 07:14 AM
Last Post: Laplace12
  Plotting sum of data files using simple code Laplace12 3 2,992 Jun-16-2021, 02:06 PM
Last Post: BashBedlam
  Plotting “simple” diagram with gridlines at specified intervals schniefen 1 2,374 Dec-03-2020, 05:54 PM
Last Post: schniefen
  Using a .bat file to execute simple anaconda commands in Windows 10 gacg1 0 4,621 Nov-30-2020, 03:24 PM
Last Post: gacg1
  Simple Read File Issue blackjesus24 4 2,684 Feb-09-2020, 12:07 AM
Last Post: blackjesus24
  Looking for simple help - text file stripping DerekK 1 2,093 Mar-08-2019, 10:06 AM
Last Post: Larz60+
  Plotting distribution of a variable on the map with a shape file fyec 0 2,014 Jun-22-2018, 09:43 PM
Last Post: fyec

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020