YanShuu 发表于 2023-3-4 23:51:43

生成可视化图形的一点问题,我用的是VS2022

from pyecharts.charts import Line # 导入图表模块
from pyecharts.options import TitleOpts # 图表标题设置模块
line = Line() # 创建一个折线图对象
line.add_xaxis(["中国","美国","英国"]) # 折线图添加X轴数据
line.add_yaxis("GDP",) # 折线图添加Y轴数据
line.render() # 通过render方法生成图像
line.set_global_opts(
    title_opts=TitleOpts(title="GDP展示",pos_left="center")
    )

请问下这个代码的图表为什么没有标题呢,能正常运行

isdkz 发表于 2023-3-4 23:58:05

本帖最后由 isdkz 于 2023-3-5 00:03 编辑

这段代码在渲染图表之后添加了标题设置,需要在渲染图表前调用 set_global_opts 方法来设置图表的全局配置,包括标题等。

因此,需要将 line.set_global_opts 方法的代码放在 line.render 方法之前,才能正确设置图表的标题。

正确的代码如下:

from pyecharts.charts import Line # 导入图表模块
from pyecharts.options import TitleOpts # 图表标题设置模块

line = Line() # 创建一个折线图对象
line.add_xaxis(["中国","美国","英国"]) # 折线图添加X轴数据
line.add_yaxis("GDP",) # 折线图添加Y轴数据

line.set_global_opts(
    title_opts=TitleOpts(title="GDP展示",pos_left="center")
)

line.render() # 通过render方法生成图像
这样就能够正确生成带有标题的折线图了。

YanShuu 发表于 2023-3-5 00:33:21

isdkz 发表于 2023-3-4 23:58
这段代码在渲染图表之后添加了标题设置,需要在渲染图表前调用 set_global_opts 方法来设置图表的全局配置 ...

谢谢了{:7_146:}
页: [1]
查看完整版本: 生成可视化图形的一点问题,我用的是VS2022