How to plot the inverse of a timeseries

Question:

I have a timeseries that looks like this:

import matplotlib.pyplot as plt
import numpy as np

data = [
        0.0320312479743734,
        0.029813902801834047,
        0.029573831125162542,
        0.018995469145011157,
        0.027841876726597548,
        0.041286583291366696,
        0.04001532797701657,
        0.036066913162358105,
        0.05553811206482351,
        0.019244458235334605,
        0.0350094516761601,
        0.025880977045744658,
        0.00444492106908001,
        0.021624276996590197,
        0.024681835202500224,
        0.020811115973629057,
        0.022745881869923323,
        0.03943057672586292,
        0.025860359892249107,
        0.020410736033227295
    ]
plt.plot(data)

enter image description here

How, in python, can I create the inverse of this line and plot it? By inverse, I  mean something like this (please ignore the y-axis, it should be identical to the first one but I just vertically flipped the image for demonstration):

enter image description here

I do not mean the plot the line that is the opposite absolute distance, like this:

enter image description here

Asked By: connor449

||

Answers:

Not sure if there is a nifty numpy function for this, but you can do:

inverse = np.array(data)
inverse = inverse.max() - (inverse - inverse.min())
plt.plot(inverse)

Over the original data:

enter image description here

Answered By: Tom

Here is a solution using only matplotlib, so we don’t need to compute another array.

Plot the original data twice — first time as normal, second time on an inverted secondary y-axis:

fig, ax = plt.subplots()

# original data
ax.plot(data, label='Normal')

# original data on inverted secondary y-axis
ax2 = ax.twinx()
ax2.invert_yaxis()
ax2.set_yticks([])
ax2.plot(data, linestyle='--', label='Inverted')

# legend for all axes (fig.legend, not plt.legend)
fig.legend(loc="upper left", bbox_to_anchor=(0, 1), bbox_transform=ax.transAxes)
plt.show()

figure output

Answered By: holvold