Python matlplotlib add hyperlink to text

前端 未结 3 2068
南方客
南方客 2020-12-11 05:57

I\'ve created a plot in Python using matplotlib. After annotating each line, I\'d like to make the label a hyperlink (or alternatively, make the line itself a hyperlink). Th

3条回答
  •  执念已碎
    2020-12-11 06:33

    This is possible with pgf backend:

    #!/usr/bin/env python3
    
    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")
    

提交回复
热议问题