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

标签 python svg ipython jupyter-notebook pygal

经过大量研究,我终于设法让工具提示在 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') 

在生成的 .svg 中,工具提示现在可以很好地工作,但仅当文件在浏览器中打开时

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

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

最佳答案

我的解决方法包括使用必要的 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)

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

关于python - pygal 图表不在 Jupyter/IPython 笔记本中显示工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36322683/

相关文章:

ipython - IPython 提示号有什么用?

python - 如果 IPython 存在,则可将 IPython 与代码模块互换使用

python - 如何在django数组字段中存储文件

ios - SVGKit 性能以及它是否应该优于 PNG?

javascript - svg 折线的 svg 路径

css - 溢出:在 SVG 上可见

python - IPython 控制台卡在 "Connecting to kernel..."(Spyder v3.2.6、Py 3.6、Windows 10、64x)

python - 字符串递归问题

python - 函数内的变量

python - 如何将 "subprocess.call"的输出捕获到文件中?