防止matplotlib中重叠的标签和图形

时间:2013-07-16 02:58:28

标签: python matplotlib

我有以下代码:

from mpl_toolkits.axes_grid.axislines import SubplotZero
from matplotlib.transforms import BlendedGenericTransform
from matplotlib import patches
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="k")
    ax.axvline(linewidth=1.7, color="k")

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

    ax.text(0, 1.05, r'$y$', transform=BlendedGenericTransform(ax.transData, ax.transAxes), ha='center')
    ax.text(1.05, 0, r'$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.499999999, 5, 1000000)

    yy = numpy.log(2*x + 3)/2 + 3

    ax.plot(x, yy, linewidth=1.2, color="black")

    plt.ylim(-2, 5)
    plt.xlim(-5, 5)

    plt.text((numpy.e**(-6) - 3)/2, 0, r'$(\frac{1}{2} (e^{-6} - 3), 0)$', position=((numpy.e**(-6) - 3)/2 + 0.1, 0.1))
    plt.plot((numpy.e**(-6) - 3)/2, 0, 'ko')

    plt.text(0, numpy.log(3)/2 + 3, r'$(0, \frac{1}{2} \log_e{\left (3 \right )} + 3)$', position=(0.1, numpy.log(3)/2 + 3 + 0.1))
    plt.plot(0, numpy.log(3)/2 + 3, 'ko')

    plt.savefig('AnswersSA1a.png')

生成此图表:

graph

两个标记的轴截距与轴以及图形本身有显着的重叠。我知道这可以通过手动将文本放在一个单独的点来解决。但是 - 有没有办法让matplotlib巧妙地放置标签,使其不重叠?

0 个答案:

没有答案