pygal图表不显示Jupyter/IPython笔记本中的工具提示

Pyd*_*man 2 python svg ipython pygal jupyter-notebook

经过大量研究,我终于设法在pygal中使用工具提示:

Config = pygal.Config()
Config.js = ['http://kozea.github.io/pygal.js/2.0.x/pygal-tooltips.js']
bar_chart = pygal.Bar(Config)                                      # Then create a bar graph object
bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])  # Add some values
bar_chart.render_to_file('bar_chart.svg', force_uri_protocol='https') 
Run Code Online (Sandbox Code Playgroud)

在生成的.svg中,工具提示现在可以正常工作,但只有在浏览器中打开文件时才能正常工作.

当图表直接显示在Jupyter中时(IPython.core.display.SVG(filename="bar_chart.svg")或者简单地显示bar_chart),工具提示和样式不存在.

这是一个已知的限制吗?还是可以实现?

小智 7

我的解决方法包括使用必要的javascript和图表svg设置HTML,如下所示:

import pygal
from IPython.display import display, HTML

base_html = """
<!DOCTYPE html>
<html>
  <head>
  <script type="text/javascript" src="http://kozea.github.com/pygal.js/javascripts/svg.jquery.js"></script>
  <script type="text/javascript" src="https://kozea.github.io/pygal.js/2.0.x/pygal-tooltips.min.js""></script>
  </head>
  <body>
    <figure>
      {rendered_chart}
    </figure>
  </body>
</html>
"""

def galplot(chart):
    rendered_chart = chart.render(is_unicode=True)
    plot_html = base_html.format(rendered_chart=rendered_chart)
    display(HTML(plot_html))

bar_chart = pygal.StackedBar()
bar_chart.add('Bars', [1,2,3,4,5])

galplot(bar_chart)
Run Code Online (Sandbox Code Playgroud)

不是最漂亮的解决方案,但它确实有效