Plot graphs from pandas with a url somewhere on the plot

366 Views Asked by At

I'm trying to find a way to plot many graphs in python with a clickable url corresponding to each graph (could be in the title?) At the minute I'm working with pandas dataframes, using below code:

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()

Unfortunately, I can't seem to find a way to adapt this code to make my graph titles clickable links to open this url.

Is there a way of doing what I want to do?

edit: This code does what I want to do:

    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")

However I haven't managed to implement it into my code successfully:

# # 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()
1

There are 1 best solutions below

0
On

You should definitely take a look at plotly graphs and plotly dash that allows you to make interoperable pages all from Python.

Dash is the API that allows you to create HTML DOM objects from python and connect them to Data fetchers and Plotly Figures to render the data.

I would recommend taking a look at this video which helped me a lot at getting started.