x=["瓜子", "鸡腿", "海苔", "麦片", "饼干", "辣条", "果干"] y1=[114, 55, 27, 101, 125, 27, 105] y2=[57, 130, 122, 129, 120, 60, 49] 利用pyechart实现柱状图,
时间: 2023-07-19 19:44:23 浏览: 224
好的,我可以帮你实现这个功能。首先需要安装pyecharts这个库,你可以使用以下命令进行安装:
```
pip install pyecharts
```
安装完成后,你可以使用以下代码实现柱状图的绘制:
```python
from pyecharts.charts import Bar
from pyecharts import options as opts
x=["瓜子", "鸡腿", "海苔", "麦片", "饼干", "辣条", "果干"]
y1=[114, 55, 27, 101, 125, 27, 105]
y2=[57, 130, 122, 129, 120, 60, 49]
bar = (
Bar()
.add_xaxis(x)
.add_yaxis("销售量1", y1)
.add_yaxis("销售量2", y2)
.set_global_opts(title_opts=opts.TitleOpts(title="商品销售量"))
)
bar.render("bar.html")
```
这段代码会生成一个HTML文件,你可以在浏览器中打开它来查看柱状图的结果。如果你想要将图表直接显示在Jupyter Notebook中,可以使用以下代码:
```python
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.render import NotebookRenderable
NotebookRenderable.init_jupyter()
%load_ext pyecharts.rendering
%pylab inline
x=["瓜子", "鸡腿", "海苔", "麦片", "饼干", "辣条", "果干"]
y1=[114, 55, 27, 101, 125, 27, 105]
y2=[57, 130, 122, 129, 120, 60, 49]
bar = (
Bar()
.add_xaxis(x)
.add_yaxis("销售量1", y1)
.add_yaxis("销售量2", y2)
.set_global_opts(title_opts=opts.TitleOpts(title="商品销售量"))
)
bar.load_javascript()
bar.render_notebook()
```
这样就可以在Jupyter Notebook中直接显示柱状图了。
阅读全文
相关推荐













