Python Forum
URGENT: How to plot data from text file. Trying to recreate plots from MATLAB
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
URGENT: How to plot data from text file. Trying to recreate plots from MATLAB
#1
Exclamation 
I have the following data saved from a MATLAB code and they have the following form:
Quote:0,0.001,0.002,0.003,0.004,0.005,0.006,0.007,0.008,0.009,0.01,0.011,0.012,0.013,0.014,0.015,0.016,0.017,0.018,..
and I am trying to recreate my plot from MATLAB that looks like:
matlabplot


So I have written the following python code that is not working:
XX, YY =[], []
for line in open('RhoMacCormack_sigpt05_cflpt5.txt','r'):
    values = [float(s) for s in line.split()]
    XX.append(values[0])
    YY.append(values[1])
    plt.plot(XX,YY)
    plt.show()
Gives the error:
ValueError: could not convert string to float: '0,0.001,0.002,0.003,0.004...
Could someone help me get this working?
Reply
#2
You should provide separator. By default it's whitespace:

>>> s = '0,0.001,0.002,0.003,0.004,0.005'
>>> s.split()
['0,0.001,0.002,0.003,0.004,0.005']                # no whitespace
>>> s.split(',')
['0', '0.001', '0.002', '0.003', '0.004', '0.005']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(May-08-2021, 08:03 PM)perfringo Wrote: You should provide separator. By default it's whitespace:

>>> s = '0,0.001,0.002,0.003,0.004,0.005'
>>> s.split()
['0,0.001,0.002,0.003,0.004,0.005']                # no whitespace
>>> s.split(',')
['0', '0.001', '0.002', '0.003', '0.004', '0.005']
If I do that I get the plot but it's empty? no data is plotted Huh Huh Huh Huh
python
Reply
#4
If you're not getting points on your plot you must not have data in either XX or YY. Your text of the file makes it seem like it's one line. That might get you XX values but I bet YY is empty. Have you looked??? Try printing values after line 3 and make sure you have valid data to plot.
Reply
#5
Urgent?? There is lots of info on matplotlib around.

Here is a simple example to get you started.

import numpy
import matplotlib.pyplot as plt
# for locating minor ticks
from matplotlib.ticker import (MultipleLocator, AutoMinorLocator)

def myApp():    
    x = np.arange(0.01, 10.0, 0.01)
    y = x
    z = 2*x    
    fig, ax = plt.subplots(num='My Plot') # name of the output window
    plt.rcParams["figure.figsize"] = [5, 5] # set the size of the output window
    plt.ion() # to make plot non-blocking, i.e. if multiple plots are launched
    ax.plot(x, y, color = 'g', label='y = x')
    ax.plot(x, z, color = 'r', label='y = 2 * x')    
    ax.set_xlim(0, 10)  # increasing time
    ax.set_xlabel('increasing time (s)')
    ax.set_ylabel('voltage (mV)')
    ax.set_title('Crazy stuff ...')
    ax.grid(True)
    ax.legend()
    plt.show()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fit straight line to pandas time series data with semilog plot schniefen 2 1,502 Mar-10-2023, 01:08 PM
Last Post: jefsummers
  Plot time series data schniefen 3 1,271 Mar-04-2023, 04:22 PM
Last Post: noisefloor
Exclamation Python Homework (Urgent help needed!!) chickenseizuresalad 6 4,217 Oct-11-2021, 01:59 AM
Last Post: Underscore
Exclamation urgent , Python homework alm 2 2,257 May-09-2021, 11:19 AM
Last Post: Yoriz
  urgent I got a syntax errors alm 2 5,793 Feb-28-2021, 02:54 PM
Last Post: alm
Heart Urgent homework help needed Medou 4 2,659 Nov-24-2020, 09:28 AM
Last Post: buran
  Working with text data APK 4 2,429 Aug-22-2020, 04:48 AM
Last Post: buran
  [Urgent] build code GodMaster 2 1,768 Mar-23-2020, 12:25 AM
Last Post: jefsummers
  Bifid Genkey (Urgent) Laura123 2 2,016 Mar-09-2020, 08:09 PM
Last Post: micseydel
  Read text file, process data and print specific output Happythankyoumoreplease 3 2,851 Feb-20-2020, 12:19 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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