【问题标题】:pygal charts not displaying tooltips in Jupyter / IPython notebookpygal 图表未在 Jupyter / IPython 笔记本中显示工具提示
【发布时间】:2016-07-19 06:34:51
【问题描述】:

经过大量研究,我终于设法让工具提示在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),工具提示和样式不存在。

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

【问题讨论】:

  • 您的问题解决了吗?我这里也有(即:pygal + jupyter 没有工具提示)

标签: python svg ipython jupyter-notebook pygal


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    在 Google 搜索中找到 this,这让我想到了这个问题。如果您阅读 pygal 文档或查看它生成的示例 SVG,您会看到它包含 pygal.js,而您的笔记本中缺少该文件。

    【讨论】:

      猜你喜欢
      • 2019-03-17
      • 2015-09-22
      • 2016-03-06
      • 2020-07-22
      • 2018-02-26
      • 2014-10-19
      • 1970-01-01
      • 1970-01-01
      • 2018-04-04
      相关资源
      最近更新 更多