Python-matplotlib用infiniite f(x)值绘制

发布于 2025-01-31 17:46:55 字数 541 浏览 1 评论 0原文

我正在尝试在matplotlib中绘制棕褐色函数。 当插入的值为pi/2时,tan函数将变为无穷大。问题在于我不知道如何处理matplotlib中的无限数字,或者又是无限的小范围来绘制它。问题之一是降低的行,试图连接上一个绘制顶部的点,而第一个pi/2

import matplotlib.pyplot as plt
import numpy

x_values = numpy.linspace(-5,5,num=1000)
y_values = numpy.tan(x_values)
plt.plot(x_values, y_values)
plt.show()

这是输出:

i.sstatic.net/hg2st.png“ rel =“ nofollow noreferrer 是因为此功能获得1000点绘制的点,而不是无限点,但它并没有创建我想要的图像,我想要像图形计算器会产生的图像一样。 我能做些什么?

I am trying to graph the tan function in matplotlib.
The tan function goes to infinity when the value inserted is pi/2. The problem is that I dont know how to deal with infinite numbers in Matplotlib or in turn, infinitly small ranges to be able to plot it. One of the problems is the line that goes down, to try to connect the last dot drawn up top, with the first after pi/2

import matplotlib.pyplot as plt
import numpy

x_values = numpy.linspace(-5,5,num=1000)
y_values = numpy.tan(x_values)
plt.plot(x_values, y_values)
plt.show()

This is the output:

Image created by Matplotlib

This is because this function gets 1000 points to plot, not infinite points, but it doesn't create the image that I want, I want an image like the one that a graphic calculator would produce.
What can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我太投入 2025-02-07 17:46:55

如何绘制不连续的函数

import numpy as np
from numpy import pi, nan
import matplotlib.pyplot as plt

start, stop, num, eps = -5, 5, 200, 0.003
data = np.linspace(start, stop, num)

shift = (data + pi/2) % pi
data = data[(eps < shift) & (shift < pi - eps)]
asymptotes = np.r_[start:stop:pi] - (start + pi/2) % pi + pi

xx = np.r_[data, asymptotes]
yy = np.r_[np.tan(data), [nan]*len(asymptotes)]
order = xx.argsort()   

xlim = [start, stop]
ylim = [-25, 25]

plt.figure(figsize=(8,12))
plt.xlim(xlim)
plt.ylim(ylim)

plt.plot([asymptotes]*2, 
         [[i]*len(asymptotes) for i in ylim], 
         'k--', linewidth=1)

plt.plot(xx[order], yy[order])
plt.show()
  • 渐变函数的不连续点 - code
  • > eps - 不连续点的不连续点的半径
  • shift> shift shift - 距离 - 距离从数据到左

”在此处输入图像说明”

How to plot a discontinuous function

import numpy as np
from numpy import pi, nan
import matplotlib.pyplot as plt

start, stop, num, eps = -5, 5, 200, 0.003
data = np.linspace(start, stop, num)

shift = (data + pi/2) % pi
data = data[(eps < shift) & (shift < pi - eps)]
asymptotes = np.r_[start:stop:pi] - (start + pi/2) % pi + pi

xx = np.r_[data, asymptotes]
yy = np.r_[np.tan(data), [nan]*len(asymptotes)]
order = xx.argsort()   

xlim = [start, stop]
ylim = [-25, 25]

plt.figure(figsize=(8,12))
plt.xlim(xlim)
plt.ylim(ylim)

plt.plot([asymptotes]*2, 
         [[i]*len(asymptotes) for i in ylim], 
         'k--', linewidth=1)

plt.plot(xx[order], yy[order])
plt.show()
  • asymptotes - discontinuity points of the tangent function
  • eps - a radius of the excluded neighborhood around the discontinuity points
  • shift - distance from data to nearest breakpoints on the left

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文