首页 > 解决方案 > 是否可以在标题中创建带有 href 的绘图(matplotlib)?

问题描述

我想让这个链接互动,不幸的是它不显示任何 html 内容。有没有办法让这个标题成为一个href?

    from matplotlib import pyplot as plt
    import numpy as np

    fig, axs1 = plt.subplots(1)
    import numpy as np
    x = np.linspace(0, 2 * np.pi, 400)
    y = np.sin(x ** 2)
    axs1.plot(x)
    axs1.set_title("<a href=vk.com>vk.com</a>")
    plt.show()

链接

标签: pythonmatplotlib

解决方案


您可以使用以下方法将标题写为文本,而不是使用set_title

import numpy as np
from matplotlib import pyplot as plt
from IPython.display import set_matplotlib_formats
set_matplotlib_formats("svg")

fig, axs1 = plt.subplots(figsize=(6, 4))
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
axs1.plot(x, y)
text = axs1.annotate("vk.com", xy=(0.5,1), xytext=(0.5,0.96), 
                     xycoords='figure fraction',
                     verticalalignment='top',
                     url='vk.com', fontsize=16)
plt.show()

推荐阅读