Home > OS >  How to plot two lists of data with various length in Python (embedding of a time-series in phase spa
How to plot two lists of data with various length in Python (embedding of a time-series in phase spa

Time:05-01

My aim is to transform a one-dimensional time-series into a two-dimensional phase space. Since the time-series is one-dimensional, the phase space will be a pseudo (lag) phase space.

One theoretical approach to transform a time-series into pseudo phase space is as follows:

  1. The original list of data is the full-length time-series x(t).
  2. A subseries of data is the "lagged" version of the original time-series, starting with the second value of the time-series (instead of with its first one, as the original time-series) x(t 1).

Consequently, the subseries will always have one value less in its list. For a 3D phase space, a second subseries would have two values less in its list. This is where my code related problem comes in, since matplotlib does not allow me to plot a two-dimensional plane when the length of two lists is not equal.

Here is my current code:

import numpy as np
import matplotlib.pyplot as plt

# Example time-series
Data = [924, -5, 24, 1, 0, 242, -5, 42, 5, 1, -9, 50, 3, 432, 0, -5, 4, 1, 2, 3]

# Embedding (time-series to phase space)
x_list = Data[:-1]
y_list = Data[1:]

# Plot
plt.plot(x_list, y_list, c="blue", linewidth=0.5)
plt.show()

This code uses the whole length of the time-series except the last value in the list by x_list = Data[:-1] for the x-axis. For the y-axis, the code uses the whole time-series except the very first item in the list by Data[1:].

While this code works, its result is not a real embedding of a time-series into its two-dimensional phase space, since x_list = Data[:-1] does not include the last value of the time-series.

What would be a proper way for coding and plotting the phase space of subseries that increasingly diminish in length compared to the original time-series data?

CodePudding user response:

A simple approach is to use shifted Series

NB. you can also autocorrelation

CodePudding user response:

For a wrap around solution you could use a list comp to shift the data:

Data = list(range(10))

d = 5
multi = [Data[dims:] Data[:dims] for dims in range(d) ]

print(*multi, sep="\n")

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
[2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
[3, 4, 5, 6, 7, 8, 9, 0, 1, 2]
[4, 5, 6, 7, 8, 9, 0, 1, 2, 3]

if you do not want to wrap around, fix it like so:

d = 5
multi = [(Data[dims:] Data[:dims])[:-d 1] for dims in range(d)]

to get

[0, 1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
[2, 3, 4, 5, 6, 7]
[3, 4, 5, 6, 7, 8]
[4, 5, 6, 7, 8, 9]

If you want a hypothetical last value you would have to do some extrapolation of the series you got - if that makes more sense then cutting it short ... dunno.

  • Related