Omit joining lines in matplotlib plot e.g. y = tan(x)

前端 未结 4 1068
囚心锁ツ
囚心锁ツ 2020-12-04 01:48

I have the graph y = tan(x) and I want to remove the vertical lines (see below).

Here is my code:

import numpy as np
import matplotlib.p         


        
4条回答
  •  心在旅途
    2020-12-04 02:32

    One could use the definition of the tangent to filter out those points where the cosine of x is close enough to 0.

    import numpy as np
    import matplotlib.pyplot as plt
    
    x = np.linspace(0, 4*np.pi, 666)
    y = np.tan(x)
    
    y[np.abs(np.cos(x)) <= np.abs(np.sin(x[1]-x[0]))] = np.nan
    
    plt.plot(x, y)
    plt.ylim(-3,3)
    
    plt.show()
    

    This works for equally spaced data only.

提交回复
热议问题