Code for image above:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import datetime
df = pd.read_csv('TimeSeries.csv').set_index('date')
dt_index = pd.to_datetime(df.index)
fig, ax = plt.subplots()
ax.tick_params(
axis='x',
which='both',
bottom=False,
top=False,
labelbottom=False)
ax.set_xlim([datetime.date(2022, 3, 1), datetime.date(2022, 4, 15)])
ax.plot(dt_index, df['long'], 'o')
I want the chart to look like an oscillator. Like
I believe that the coordinates of the points need to be reflected horizontally relative to zero. But I don't have any idea how to do it.
CodePudding user response:
Maybe something like this can solve your problem:
# extract values from the dataframe
y = df['long'].values
# select values > 0
idx = y > 0
# offset those values
y[idx] -= 1
# select all other values and apply an opposite offset
y[np.logical_not(idx)] = 1
then replace ax.plot(dt_index, df['long'], 'o')
with ax.plot(dt_index, y, 'o')
.
CodePudding user response:
My take: I'm not sure what you want the 'oscillation' to look like, because it currently looks like one. However, here's how I handle it:
By iterating over the data points with np.convolve, you can create a function to smooth them. Curve fitting, data smoothing, n-degree polynomials, and even Lowess(the best approach) might all be used in the function. Here's a great
CodePudding user response:
After importing numpy as import numpy as np
, just substitute the ax.plot
line with
ax.plot(dt_index, df['long']-np.sign(df['long']), 'o')
Sample output:
Explanation: np.sign()
returns elementwise the sign of the number. So, we subtract 1 from positive and add 1 to negative values, centering your values around 0.