【问题标题】:Plot graphs from pandas with a url somewhere on the plot在绘图的某处绘制带有 url 的 pandas 图表
【发布时间】: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


【解决方案1】:

您绝对应该看看 plotly graphsplotly dash,它们允许您使用 Python 制作可互操作的页面。

Dash 是允许您从 python 创建 HTML DOM 对象并将它们连接到 Data fetchers 和 Plotly Figures 以呈现数据的 API。

我建议您查看this video,这对我入门有很大帮助。

【讨论】:

    猜你喜欢
    • 2019-10-04
    • 2020-11-10
    • 2021-09-29
    • 2018-05-13
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    相关资源
    最近更新 更多