Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot y=1/x as a single graph [duplicate]

Is there an easy way to plot a function which tends to infinity in the positive and negative as a single plot, without the plot joining both ends of the positive and negative?

For example, plotting y=1/x using this code gives the resulting plot:

import numpy as np
import matplotlib.pyplot as plt

def f(x):
    return 1/x
fx_name = r'$f(x)=\frac{1}{x}$'

x=np.setdiff1d(np.linspace(-10,10,100),[0]) #to remove the zero
y=f(x)
plt.plot(x, y, label=fx_name)
plt.legend(loc='upper left')
plt.show()

y=1/x

But I would like this output, which I achieve by plotting two separate domains:

import numpy as np
import matplotlib.pyplot as plt

def f(x):
    return 1/x
fx_name = r'$f(x)=\frac{1}{x}$'

xfn=np.setdiff1d(np.linspace(-10,0,100),[0])
xfp=np.setdiff1d(np.linspace(0,10,100),[0])
yfn=f(xfn)
yfp=f(xfp)

yf = plt.plot(xfn, yfn, label=fx_name)
plt.plot(xfp, yfp, color=yf[0].get_color())
plt.legend(loc='upper left')
plt.show()

y=1/x

Is there are short-cut? Many thanks.

Solution

Include zero in the domain array, and suppress the divide by zero. This forces one element of the returned co-domain array as "inf", and "inf" is not plotted.

import numpy as np
import matplotlib.pyplot as plt

def f(x):
    with np.errstate(divide='ignore', invalid='ignore'):
        return 1/x
fx_name = r'$f(x)=\frac{1}{x}$'

x=np.linspace(-10,10,101)
y=f(x)
plt.plot(x, y, label=fx_name)
plt.legend(loc='upper left')
plt.show()

y=1/x

I prefer this method since it avoids manual manipulation of the array, and can be easily reused for other functions which share the same domain (ex. y=1/(x+2)). Thank you all for contributions.

like image 365
Tim GO Avatar asked May 18 '17 07:05

Tim GO


People also ask

How do I plot a vertical line in Matplotlib?

In matplotlib, the axvline() method is used to add vertical lines to the plot. The above-used parameters are described as below: x: specify position on the x-axis to plot the line. ymin and ymax: specify the starting and ending range of the line.

How to plot y=1/x as a single graph in Python?

How to plot y=1/x as a single graph in Python? Set the figure size and adjust the padding between and around the subplots. Create data points using numpy. Plot x and 1/x data points using plot () method.

Why does x = 0 in the plot?

Show activity on this post. Actually you want to include x = 0 because this results in y = nan, forming a gap in the plot.

How to find the horizontal asymptote of a graph?

2. If n = m n = m, then the horizontal asymptote is the line y = a b y = a b. 3. If n > m n > m, then there is no horizontal asymptote (there is an oblique asymptote ).


3 Answers

Actually you want to include x = 0 because this results in y = nan, forming a gap in the plot.

import numpy as np
import matplotlib.pyplot as plt

def f(x):
    return 1/x
fx_name = r'$f(x)=\frac{1}{x}$'

# using 101 steps results in in array including the value 0
x=np.linspace(-10,10,101)
# f(0) = nan -> a nan value creates a gap
y=f(x)
plt.plot(x, y, label=fx_name)
plt.legend(loc='upper left')
plt.show()
like image 181
Padix Key Avatar answered Oct 17 '22 23:10

Padix Key


Not necessary easier as your workaround, but you could insert a 'nan' element at the index where the sign flips, for example:

idx = np.argmax(np.diff(np.sign(y)))+1

x = np.insert(x, idx, np.nan)
y = np.insert(y, idx, np.nan)

The 'nan' causes Matplotlib to interrupt the line.

like image 42
Rutger Kassies Avatar answered Oct 17 '22 23:10

Rutger Kassies


based on Rutger Kassies ideas:

n_points = 100
x=np.setdiff1d(np.linspace(-10,10,n_points),[0]) #to remove the zero

y=f(x)
y[n_points//2-1:n_points//2+1] = np.nan

use your original plot an set the points around 0 to np.nan. that way too many points get set to None but it's symmetric.

you could also setup your linspace to includ 0 such that f(x) = np.nan: n_points = 101. (this answer and 2 comments stated that right before i did... please credit there).

like image 4
hiro protagonist Avatar answered Oct 17 '22 21:10

hiro protagonist