irl_irl
irl_irl

Reputation: 3975

Changing the graph line's color depending on date

I am plotting formation against dates. Is it possible to change the color of the plotted line depending on the date (which is on the axis)?

Upvotes: 1

Views: 3190

Answers (1)

gcalmettes
gcalmettes

Reputation: 8704

You can define masks and use them to differentiate the "segments" you want for the line.

Below is an example.

import numpy as np
import matplotlib.pyplot as plt

# data
x = np.linspace(-10, 10, 1000)
y = np.sin(x)

# 4 segments defined according to some x properties
segment1 = (x<-5)
segment2 = (x>=-5) & (x<0)
segment3 = (x>=0) & (x<5)
segment4 = (x>=5)

plt.plot(x[segment1], y[segment1], '-k', lw=2)
plt.plot(x[segment2], y[segment2], '-g', lw=2)
plt.plot(x[segment3], y[segment3], '-r', lw=2)
plt.plot(x[segment4], y[segment4], '-b', lw=2)

plt.show()

plot

Upvotes: 7

Related Questions