matplotlib XKCD图中的透明突出显示?

oknwwptz  于 2023-03-03  发布在  其他
关注(0)|答案(1)|浏览(130)

我想在我的博客上使用一些XKCD风格的图。下面是一个示例图:

import matplotlib.pyplot as plt
import numpy as np

with plt.xkcd():

    fig = plt.figure()
    ax = fig.add_axes((0.1, 0.2, 0.8, 0.7))
    ax.spines[['top', 'right']].set_visible(False)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.set_ylim([-30, 15])
    # Plot of function
    x = np.linspace(-5, 5, num=100)
    y = x ** 3 - 10 * x
    ax.plot(x, y, color='y')
    # Tangent space
    ax.plot(x, 2 * x - 16, color='m')
    ax.text(3, -10, r'$T_pC$', color='m')
    ax.scatter(2, -12, color='m')
    ax.text(2, -12, r'$p$', color='m')

    plt.tight_layout()
    plt.savefig('/first_example_tangent_space.png', transparent=True, dpi=300)
plt.close()

问题是它在暗模式下看起来不太好,显示白色高光:
灯光模式:

暗模式:

曲线和文本周围的高光很好,但是方框让人分心。有没有办法为那些方框把白色高光改成透明高光?

fv2wmkja

fv2wmkja1#

您是否需要x轴/y轴?不幸的是,您似乎无法使用savefigtransparent=True完全隐藏书脊(ax.spines['top'].set_visible(False))。
ax.axis('off')将删除整个框。设置bbox_inches='tight',您将能够自己调整填充:

import matplotlib.pyplot as plt
import numpy as np

with plt.xkcd():

    fig = plt.figure()
    ax = fig.add_axes((0.1, 0.2, 0.8, 0.7))
    ax.spines[['top', 'right']].set_visible(False)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.set_ylim([-30, 15])
    # Plot of function
    x = np.linspace(-5, 5, num=100)
    y = x ** 3 - 10 * x
    ax.plot(x, y, color='y')
    # Tangent space
    ax.plot(x, 2 * x - 16, color='m')
    ax.text(3, -10, r'$T_pC$', color='m')
    ax.scatter(2, -12, color='m')
    ax.text(2, -12, r'$p$', color='m')

    ax.axis('off')
    plt.savefig('./first_example_tangent_space.png', transparent=True, dpi=300, bbox_inches='tight', pad_inches=0.1)

相关问题