I would like the asymptote on the tg(x) function be draw with a dashed line, but I don't know how to change it in this code:
import matplotlib.ticker as tck
import matplotlib.pyplot as plt
import numpy as np
f,ax=plt.subplots(figsize=(8,5))
x=np.linspace(-np.pi, np.pi,100)
y=np.sin(x)/np.cos(x)
plt.ylim([-4, 4])
plt.title("f(x) = tg(x)")
plt.xlabel("x")
plt.ylabel("y")
ax.plot(x/np.pi,y)
ax.xaxis.set_major_formatter(tck.FormatStrFormatter('%g $\pi$'))
Interesting question. My approach is to look for the discontinuities by examining the derivative of the function, and separating the original function based on the location o these discontinuities.
So for
tan(x)
, since the derivative is always positive (outside of the asymptotes) we look for points wherenp.diff(y) < 0
. Based on all the locations where the previous condition is true, we split up the original function into segments and plot those individually (with the same plot properties so the lines look the same) and then plot black dashed lines separately. The following code shows this working:With a final plot looking like: