gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:18:32 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com