Let's say I want to plot two solid lines that cross each other and I want to plot line2 dashed only if it is above line 1. The lines are on the same x-grid. What is the best/simplest way to achieve this? I could split the data of line2 into two corresponding arrays before I plot, but I was wondering if there is a more direct way with some kind of conditional linestyle formatting?
Minimal Example:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0,5,0.1)
y1 = 24-5*x
y2 = x**2
plt.plot(x,y1)
plt.plot(x,y2)#dashed if y2 > y1?!
plt.show()
There were related questions for more complex scenarios, but I am looking for the easiest solution for this standard case. Is there a way to do this directly inside plt.plot()?
You could try something like this: