import re from collections import Counter import pyecharts.options as opts from pyecharts.charts import Bar # 读取所有章节的文本 full_text = "" for i in range(1, 14): with open(f"动物农场/第{i}章.txt", encoding="utf-8") as f: text = f.read() full_text += text # 获取文本中的对话 dialogues = re.findall(r"“(.*?)”", full_text) # 获取主要人物 characters = ["拿破仑", "雪球", "拳师", "拖拉机", "侏儒", "波克斯", "明格尔"] # 统计主要人物出场次数 character_counter = Counter(re.findall("|".join(characters), full_text)) # 使用 Pyecharts 生成柱状图 bar = ( Bar() .add_xaxis(list(character_counter.keys())) .add_yaxis("", list(character_counter.values())) .set_global_opts( title_opts=opts.TitleOpts(title="动物农场主要人物出场次数"), xaxis_opts=opts.AxisOpts(name="主要人物"), yaxis_opts=opts.AxisOpts(name="出场次数"), ) ) bar.render_notebook()无法生成柱状图
时间: 2023-11-06 15:08:30 浏览: 42
根据你提供的代码,我发现可能是最后一行代码有问题,应该是 `bar.render_notebook()`,这个方法可以在 Jupyter Notebook 等环境中直接显示图表,但是如果你在其他环境中运行代码,可能会遇到无法显示图表的问题。
如果你想在其他环境中显示图表,可以尝试将最后一行代码改为 `bar.render("chart.html")`,这样会在当前目录下生成一个名为 `chart.html` 的文件,你可以用浏览器打开这个文件,就能看到你生成的柱状图了。
如果你不想在本地生成文件,也可以将最后一行代码改为 `bar.render_notebook()`,但是这个方法只适用于 Jupyter Notebook 等环境。如果你在其他环境中运行代码,可能会遇到无法显示图表的问题。
相关问题
from pyecharts.charts import Bar from pyecharts.faker import Faker from pyecharts import options as opts import pandas as pd import openpyxl from collections import Counter df_tb = pd.read_csv('住宿攻略.csv') x=df_tb['name'].tolist() y=df_tb['num'].tolist() bar=( Bar(init_opts=opts.InitOpts(width="1800px", height="800px")) .add_xaxis(x) .add_yaxis(300,y) ) bar.render("zhusu.html") 给这个柱形图加上横坐标和纵坐标的名称以及标题的代码
可以使用opts库中的AxisOpts和TitleOpts来设置坐标轴和标题的名称,代码如下:
```
from pyecharts.charts import Bar
from pyecharts.faker import Faker
from pyecharts import options as opts
import pandas as pd
import openpyxl
from collections import Counter
df_tb = pd.read_csv('住宿攻略.csv')
x = df_tb['name'].tolist()
y = df_tb['num'].tolist()
bar = (
Bar(init_opts=opts.InitOpts(width="1800px", height="800px"))
.add_xaxis(x)
.add_yaxis("数量", y)
.set_global_opts(
title_opts=opts.TitleOpts(title="住宿攻略"),
xaxis_opts=opts.AxisOpts(name="住宿名称"),
yaxis_opts=opts.AxisOpts(name="预订数量")
)
)
bar.render("zhusu.html")
```
这个代码会在画出的柱形图上方加上标题“住宿攻略”,横坐标名称为“住宿名称”,纵坐标名称为“预订数量”。同时,y轴的数据系列会被命名为“数量”。
import pyecharts.options as opts from pyecharts.charts import Line import pandas as pd import openpyxl from collections import Counter df_tb = pd.read_csv('目的地前15.csv') x=df_tb['地址'].tolist() y=df_tb['累计游玩人数'].tolist() line=( Line(init_opts=opts.InitOpts(width="10000px", height="1000px")) .add_xaxis(x) .add_yaxis(10,y) .set_global_opts( title_opts=opts.TitleOpts(title="日本前10目的地人数统计"), xaxis_opts=opts.AxisOpts(name="景点名称"), yaxis_opts=opts.AxisOpts(name="游玩人数(人)") ) ) line.render("目的地前15.html")给这个折线图规定x轴长度
可以通过设置 xaxis_opts 的 axislabel_opts 来调整 x 轴标签的显示长度,示例如下:
```
xaxis_opts=opts.AxisOpts(
name="景点名称",
axislabel_opts=opts.LabelOpts(rotate=-45, font_size=12) # 设置标签旋转角度和字体大小
),
```
其中,rotate 为标签旋转角度,font_size 为标签字体大小,可以根据实际需要进行调整。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)