Python Forum
Graphic line plot with matplotlib, text file in pytho
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Graphic line plot with matplotlib, text file in pytho
#1
hello, I want to display a graph using data from a text file with matplotlib and I am having some problems. Data is sent and received via sockets and stored in a text file.

here is my code:
import matplotlib.pyplot as plt



X = []
Y = []




fichier = open('orde.txt', 'r')


for row in fichier:
    row=row.split(' ')
    if len(row)>0:
     X.append(row[0])
     Y.append(row[-1])



plt.xlabel('Time' , fontsize = 12)
plt.ylabel('Amplitude', fontsize = 12)
plt.plot(X, Y ,color = 'red', label = 'signal')

plt.title('Courbe', fontsize = 20)
plt.show()
Larz60+ write Aug-12-2022, 05:24 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
Looks like your data is in a text file, but you are treating it like the file contains floats. You need to read the value strings from the file and convert them to float. You could do this yourself, but I suggest using the csv library or pandas.

csv_reader will remove newlines and split entries by delimiter automatically. It is a cleaner way of doing what you are currently doing, but it does not convert the values to float.
https://docs.python.org/3/library/csv.html

pandas read_csv does all the things csv_reader does and it converts the values from strings to floats.
https://pandas.pydata.org/pandas-docs/st...d_csv.html
Reply
#3
(Aug-12-2022, 06:18 PM)deanhystad Wrote: Looks like your data is in a text file, but you are treating it like the file contains floats. You need to read the value strings from the file and convert them to float. You could do this yourself, but I suggest using the csv library or pandas.

csv_reader will remove newlines and split entries by delimiter automatically. It is a cleaner way of doing what you are currently doing, but it does not convert the values to float.
https://docs.python.org/3/library/csv.html

pandas read_csv does all the things csv_reader does and it converts the values from strings to floats.
https://pandas.pydata.org/pandas-docs/st...d_csv.html



Ah Okay, i will try the link. Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  matplotlib x-axis text move bottom upward jacklee26 3 999 May-31-2023, 04:28 AM
Last Post: jacklee26
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,591 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Getting last line of each line occurrence in a file tester_V 1 878 Jan-31-2023, 09:29 PM
Last Post: deanhystad
  How to read csv file update matplotlib column chart regularly SamLiu 2 1,074 Jan-21-2023, 11:33 PM
Last Post: SamLiu
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,135 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,693 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,388 Sep-27-2022, 01:38 PM
Last Post: buran
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,701 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Skipping line in text without Restarting Loop IdMineThat 4 1,499 Apr-05-2022, 04:23 AM
Last Post: deanhystad
  Print to a New Line when Appending File DaveG 0 1,229 Mar-30-2022, 04:14 AM
Last Post: DaveG

Forum Jump:

User Panel Messages

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