【发布时间】:2020-08-22 04:30:58
【问题描述】:
我正在尝试找到一种方法在 python 中绘制许多图形,每个图形对应一个可点击的 url(可能在标题中?) 目前我正在使用以下代码处理熊猫数据框:
with PdfPages("output.pdf") as pdf:
for df in filtered_dfs:
# get url:
my_url = df['url'].unique()[0]
df.plot(x='time', y='value', kind='line', title=my_url)
# add a page to the pdf
pdf.savefig()
plt.close()
不幸的是,我似乎无法找到一种方法来调整此代码以使我的图表标题可点击链接以打开此 url。
有没有办法做我想做的事?
编辑: 这段代码做我想做的事:
import matplotlib
matplotlib.use("pgf")
pgf_with_custom_preamble = {
"text.usetex": True,
"pgf.preamble": [
r"\usepackage{hyperref}"
]
}
matplotlib.rcParams.update(pgf_with_custom_preamble)
from matplotlib import pyplot as plt
x = range(5)
y = range(5)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y, "r-", label=r"Hyperlink: \url{http://google.com}")
ax.legend()
fig.savefig("mwe.pdf")
但是我还没有成功地将它实现到我的代码中:
# # result plotting
# plt.rcParams.update({'font.size': 3})
pgf_with_custom_preamble = {
"text.usetex": True,
"pgf.preamble": [
r"\usepackage{hyperref}"
]
}
with PdfPages("output.pdf") as pdf:
for df in filtered_dfs:
# get url:
my_url = df['url'].unique()[0]
df.plot(x='time', y='value', kind='line', label=r"Hyperlink: \url("+my_url+"}")
# add a page to the pdf
pdf.savefig()
plt.close()
【问题讨论】:
-
这能回答你的问题吗? Python matlplotlib add hyperlink to text
-
我遇到了之前链接的示例,但假设它已过时,因为我无法在没有 'AttributeError: module 'matplotlib.mlab' has no attribute 'bivariate_normal' 发生的情况下运行它
-
我找到了一个有效的 pyplot 示例,但我无法在我的代码中使用它:不确定是否是不兼容的 pdfpages?
标签: python pandas matplotlib pdfpages