pythonmatplotlibplotlinestyle

Matplotlib/pyplot: easy way for conditional formatting of linestyle?


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()

Minimal Example

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()?


Solution

  • enter image description here You could try something like this:

    import numpy as np
    import matplotlib.pyplot as plt
    
    x  = np.arange(0,5,0.1)
    y1 = 24-5*x
    y2 = x**2
    
    xs2=x[y2>y1]
    xs1=x[y2<=y1]
    plt.plot(x,y1)
    plt.plot(xs1,y2[y2<=y1])
    plt.plot(xs2,y2[y2>y1],'--')#dashed if y2 > y1?!
    plt.show()