from pyecharts import options as opts from pyecharts.charts import Bar from pyecharts.faker import Faker c = ( Bar() .add_xaxis(Faker.choose()) .add_yaxis("商家A", Faker.values()) .add_yaxis("商家B", Faker.values()) .set_global_opts( title_opts=opts.TitleOpts(title="Bar-Brush示例", subtitle="我是副标题"), brush_opts=opts.BrushOpts(), ) .render("bar_with_brush.html") )请你改写这段代码,不要生成html,而是直接在编译器里显示出图形来
时间: 2024-03-04 11:48:49 浏览: 157
要在编译器中显示图形,可以使用Jupyter Notebook或者在PyCharm中使用pyecharts的`render_notebook()`函数。在这里,我将展示在PyCharm中使用`render_notebook()`的方法。
首先,安装`pyecharts`的扩展库`jupyter-echarts-pypkg`:
```python
!pip install jupyter-echarts-pypkg
```
然后,在代码中添加以下行:
```python
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot as driver
from IPython.display import Image
```
并将代码末尾的`.render("bar_with_brush.html")`修改为`.render_notebook()`。最后,添加以下代码:
```python
make_snapshot(driver, c.render(), "bar_with_brush.png")
Image(filename="bar_with_brush.png")
```
这将生成一个名为"bar_with_brush.png"的图像文件,并在编译器中显示出图形。
完整修改后的代码如下:
```python
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot as driver
from IPython.display import Image
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-Brush示例", subtitle="我是副标题"),
brush_opts=opts.BrushOpts(),
)
)
make_snapshot(driver, c.render(), "bar_with_brush.png")
Image(filename="bar_with_brush.png")
```
阅读全文