I'm trying to draw a piece-wise function in matplotlib. My filtering approach (which is based on Ami's answer here) does not work:
ValueError: x and y must have same first dimension
Could you fix the issue, please?
import numpy as np
import matplotlib.pyplot as plt
gK_inf = 7.06
gK_0 = 0.09
tauN = 0.75
gK_inf1 = 0.09
gK_01 = 7.06
tauN1 = 1.1
def graph(formula, formula1, t_range):
t = np.fromiter(t_range, np.float)
gK = formula(t)
gK1 = formula1(t)
plt.plot(t,gK)
plt.plot(t,gK1)
plt.xlabel(r"$t(msec.)$")
plt.ylabel(r"$g_K$")
plt.show()
def my_formula(t):
if np.all(t>0) and np.all(t<5):
return np.power((np.power(gK_inf,0.25))-((np.power(gK_inf,0.25)-np.power(gK_0,0.25))*np.exp(-t/tauN)),4)
else:
return 0
def my_formula1(t):
if np.all(t>5) and np.all(t<10):
return np.power((np.power(gK_inf1,0.25))-((np.power(gK_inf1,0.25)-np.power(gK_01,0.25))*np.exp(-t/tauN1)),4)
else:
return 0
graph(my_formula, my_formula1, np.arange(0,10,0.1))
Update:
According to @Michael advice, the error is removed, but the result is not what it must be:
Actually, formula and formula1must be plotted in ranges [0,5] and [5,10], respectively.
This is what I need:


np.alland thereturn 0for the blue curve (doesn't go to zero for t>5). Please specify in more detail what you want (besides fixing the error message).