Javascript 如何在piechart中创建超链接

Javascript 如何在piechart中创建超链接,javascript,jquery,python,matplotlib,hyperlink,Javascript,Jquery,Python,Matplotlib,Hyperlink,我想在matplotlib中制作饼图。 此饼图将表示两个变量:男性和女性 这很容易做到:) 接下来我想做什么,我甚至不确定是否可以使用matplotlib,我想让这两个变量可以点击,所以如果我点击male,我会看到另一个页面,上面有关于这方面的信息,对male也是一样 图像映射不是解决方案,因为这些变量将来可能会发生变化 有人知道怎么做吗?如果可以使用matplotlib或您会推荐什么程序 谢谢大家! 您可以通过JavaScript/jQuery控制的imagemap或HTML元素覆盖来实现这一

我想在matplotlib中制作饼图。
此饼图将表示两个变量:男性和女性

这很容易做到:)

接下来我想做什么,我甚至不确定是否可以使用matplotlib,我想让这两个变量可以点击,所以如果我点击male,我会看到另一个页面,上面有关于这方面的信息,对male也是一样

图像映射不是解决方案,因为这些变量将来可能会发生变化

有人知道怎么做吗?如果可以使用matplotlib或您会推荐什么程序


谢谢大家!

您可以通过JavaScript/jQuery控制的imagemap或HTML元素覆盖来实现这一点

本质上,将图表数据与图表图像一起发送到页面,并使用JS根据数据规范创建带有链接的元素


它比我以前做过的条形图要难一些,但应该可以很好地工作。

虽然它还没有真正处于一个可行的稳定状态,但请看一下。不管怎么说,它看起来很有趣,而且可能是将来做这类事情(带有matplotlib绘图的交互式网页)的最佳方式

同时,正如@Mark所建议的,为饼图的楔形动态生成imagemap并不难

,我相信您可以适应所使用的任何web框架

import matplotlib.pyplot as plt

def main():
    # Make an example pie plot
    fig = plt.figure()
    ax = fig.add_subplot(111)

    labels = ['Beans', 'Squash', 'Corn']
    wedges, plt_labels = ax.pie([20, 40, 60], labels=labels)
    ax.axis('equal')

    make_image_map(fig, wedges, labels, 'temp.html')

def make_image_map(fig, wedges, labels, html_filename):
    """Makes an example static html page with a image map of a pie chart.."""
    #-- Save the figure as an image and get image size ------------------------
    # Be sure to explictly set the dpi when saving the figure
    im_filename = 'temp.png'
    fig.savefig(im_filename, dpi=fig.dpi)

    # Get figure size...
    _, _, fig_width, fig_height = fig.bbox.bounds

    #-- Get the coordinates of each wedge as a string of x1,y2,x2,y2... -------
    coords = []
    for wedge in wedges:
        xy = wedge.get_verts() 

        # Transform to pixel coords
        xy = fig.get_transform().transform(xy) 

        # Format into coord string and convert to <0,0> in top left...
        xy = ', '.join(['%0.2f,%0.2f' % (x, fig_height - y) for x, y in xy])
        coords.append(xy)

    #-- Build web page --------------------------------------------------------
    header = """
    <html>
    <body>
    <img src="{0}" alt="Pie Chart" usemap="#pie_map" width="{1}" height="{2}" />
    """.format(im_filename, fig_width, fig_height)

    # Make the image map
    map = '<map name="pie_map">\n'
    for label, xy in zip(labels, coords):
        href = 'http://images.google.com/images?q={0}'.format(label)
        area = '<area shape="poly" coords="{0}" href="{1}" alt="{2}" />'
        area = area.format(xy, href, label)
        map += '    ' + area + '\n'
    map += '</map>\n'

    footer = """
    </body>
    </html>"""

    # Write to a file...
    with file(html_filename, 'w') as outfile:
        outfile.write(header + map + footer)

if __name__ == '__main__':
    main()

这将打开一个浏览器窗口,谷歌图像搜索楔形物被标记为的任何东西。

如果数据总是相同的话,这将非常容易,一张图像地图就可以做到这一点。但数据可能会有所不同,因此必须单独计算。js或jq可以做到这一点吗?是的,您可以使用javascript/jquery以任何可以想象的方式创建和修改元素。我会看看我是否能在稍后提供一个代码示例。谢谢你,Joe,有很多细节和示例。非常好:)我正试图用django和html实现这一点。但是我不太懂你的html。通常我会把链接放在图像或它所在的位置saved@Patricia-好吧,这就是我在第一个例子中所做的。。。它只是将图像保存到磁盘,插入一个
,然后根据饼图中的楔形为保存的图像生成一个图像映射。(Stackoverflow的语法高亮显示有点不稳定,因为所有的字符串都嵌套了
”、
”、
”、
)关键是“
”下的for循环——获取每个楔子的坐标…
”位。我只是抓取每个楔子的顶点,将它们转换为保存图像的像素坐标,并用这些坐标生成一个字符串。希望有帮助!我想我的错误就在那里,因为当我把变量传递给模板时,它不起作用anything@Patricia-第一个示例应创建两个文件。。。“temp.png”,以及您作为文件名传递给函数的任何内容(示例中为“temp.html”)。谢谢Joe。还是做不到,但我要试试:)
import matplotlib.pyplot as plt

def main():
    # Make an example pie plot
    fig = plt.figure()
    ax = fig.add_subplot(111)

    labels = ['Beans', 'Squash', 'Corn']
    wedges, plt_labels = ax.pie([20, 40, 60], labels=labels)
    ax.axis('equal')

    make_picker(fig, wedges)
    plt.show()

def make_picker(fig, wedges):
    import webbrowser
    def on_pick(event):
        wedge = event.artist
        label = wedge.get_label()
        webbrowser.open('http://images.google.com/images?q={0}'.format(label))

    # Make wedges selectable
    for wedge in wedges:
        wedge.set_picker(True)

    fig.canvas.mpl_connect('pick_event', on_pick)

if __name__ == '__main__':
    main()