I'd like to plot some data from LTspice with Python and matplotlib, and I'm searching for a solution to import the exported plot data from LTspice in Python.
I found no way to do this using Pandas, since the format of the data looks like this:
5.00000000000000e+006\t(2.84545891331278e+001dB,8.85405282381414e+001°)
Is there a possibility to import this with Pandas (e.g. with an own dialect) or does someone know a simple workaround (like reading the file line-by-line and extracting the values)?
To make things worse, when exporting the plot of multiple steps, the data is separated by lines like
Step Information: L=410n (Run: 2/4)
In Java, I may have used a Scanner object to read the data. Is there a similar function in Python or even a simpler way to get the plot data into Python?
I am not familiar with exported plot data from LTspice, so I am assuming that the formatting of the example lines you provided are valid for all times.
Looking at the IO Tools section of the pandas-0.18 documentation (here), I don't see any ready-to-use parser utility for your data format. The first thing that comes to mind is to do your own parsing and preparing before filling out a pandas dataframe.
I am assuming the crucial part of your problem is to parse the data file, it's been a while since I played with pandas and matplotlib so expect mistakes relating to those.
Example
Here is a quick & dirty python3 script to parse your data into a list of dictionaries, build a pandas dataframe with it and plot it using the DataFrame's
plotmethod. I tried to explain the steps in the comments :You can copy this code to a text editor and save it as
ltspice.pyand run it withpython3 ltspice.py yourdata.datfrom your terminal.Note that,
parse_linefunction actually returns a tuple of 2-tuples in the form of ('key', value) where 'key' represents the column name. This value is then used to build the list of dictionaries in thecreate_dataframefunction.Extra
I wrote another script to test the behaviour: