如何在饼图中创建超链接
我想在matplotlib中做一个饼图。
这个饼图将用来表示两个变量:男性和女性。
这很简单 :)
接下来我想做的事情,我甚至不确定在matplotlib中是否能做到,我想让这两个变量可以点击,也就是说如果我点击男性,就能看到一个新页面,里面有关于男性的信息,女性也是一样。
使用图像地图不是一个解决方案,因为这些变量将来可能会改变。
有没有人知道怎么做到这一点?在matplotlib中可以实现吗,或者你会推荐哪个程序?
谢谢!
2 个回答
1
你可以通过图像地图或者用JavaScript/jQuery控制的HTML元素覆盖来实现这个功能。
简单来说,就是把你的图表数据和图表图片一起发送到网页上,然后用JavaScript来根据数据的要求创建带链接的元素。
这比我之前做的条形图要复杂一些,但应该没问题。
5
虽然现在这个东西还不太稳定,但你可以看看这个html5 canvas后端的matplotlib。反正看起来挺有意思的,未来可能会是做这种事情(在网页上显示matplotlib图表)的最佳方式。
与此同时,正如@Mark所建议的,动态生成一个饼图每个部分的图像地图其实并不难。
这里有一个粗略的例子,我相信你可以根据自己使用的网页框架进行调整。
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()
补充:我刚意识到你可能不是在说把图嵌入网页...(我从你提到的“显示另一个页面”那部分猜的。)如果你想要一个更像桌面应用的东西,而不想搞一个“完整”的图形界面工具包,你可以这样做:
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()
这样会打开一个浏览器窗口,搜索与饼图部分标签相关的谷歌图片...