在matplotlib中绘制棕褐色图形

neb*_*ffa 3 python graph matplotlib

我有以下代码:

from mpl_toolkits.axes_grid.axislines import SubplotZero
from matplotlib.transforms import BlendedGenericTransform
import matplotlib.pyplot as plt
import numpy

if 1:
    fig = plt.figure(1)
    ax = SubplotZero(fig, 111)
    fig.add_subplot(ax)

    ax.axhline(linewidth=1.7, color="black")
    ax.axvline(linewidth=1.7, color="black")

    plt.xticks([1])
    plt.yticks([])

    ax.text(0, 1.05, 'y', transform=BlendedGenericTransform(ax.transData, ax.transAxes), ha='center')
    ax.text(1.05, 0, 'x', transform=BlendedGenericTransform(ax.transAxes, ax.transData), va='center')

    for direction in ["xzero", "yzero"]:
        ax.axis[direction].set_axisline_style("-|>")
        ax.axis[direction].set_visible(True)

    for direction in ["left", "right", "bottom", "top"]:
        ax.axis[direction].set_visible(False)

    x = numpy.linspace(-1, 1, 10000)
    ax.plot(x, numpy.tan(2*(x - numpy.pi/2)), linewidth=1.2, color="black")

    plt.ylim(-5, 5)
    plt.savefig('graph.png')
Run Code Online (Sandbox Code Playgroud)

生成此图表: 图形

正如您所看到的,不仅绘制了tan图,而且还添加了一部分线来连接tan图的渐近区域,其中渐近线通常是渐近线.

是否有一些内置的方法可以跳过该部分?或者我是否会绘制由渐近线限定的棕褐色的不相交域(如果你理解我的意思)?

jsa*_*nen 6

您可以尝试的东西:设置有限阈值并修改您的函数以在这些点之后提供非有限值.实用代码修改:

yy = numpy.tan(2*(x - numpy.pi/2))
threshold = 10000
yy[yy>threshold] = numpy.inf
yy[yy<-threshold] = numpy.inf
ax.plot(x, yy, linewidth=1.2, color="black")
Run Code Online (Sandbox Code Playgroud)

结果是:

结果