0

I am trying to change the color of a serie when the X Date axis > TODAY. Fact is that I don't know how to do it so :

I Know how to segment the curve like that :

inxval = mdates.date2num(DatesArray)
points = np.array([inxval, ValueArray]).T.reshape(-1,1,2)
segments = np.concatenate([points[:-1],points[1:]], axis=1)

But I don't know how to do the same with a condition

enter image description here

TourEiffel
  • 4,034
  • 2
  • 16
  • 45
  • Could you post an MRE? I think the simplest option would be to call plt twice; once for the left and once for the right section. – Tempman383838 Mar 08 '22 at 14:29

1 Answers1

2

How about this? (inspired by python: how to plot one line in different colors)

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from datetime import datetime

xy = (np.random.random((10, 1)) - 0.5).cumsum(axis=0)
xy = pd.DataFrame(xy,columns=["Y"])
xy['Date'] = pd.date_range(start="03/01/2022",end="03/10/2022")

fig, ax = plt.subplots()


today=datetime.today().strftime('%Y-%m-%d')
ax.plot(xy[xy["Date"]<=today]['Date'],xy[xy["Date"]<=today]['Y'], color="red")
ax.plot(xy[xy["Date"]>=today]['Date'],xy[xy["Date"]>=today]['Y'], color="blue")
plt.xlabel(loc= "vertical")
plt.show()
LocoGris
  • 4,432
  • 3
  • 15
  • 30