日记(用pyecharts作图)

1、使用pip install pyecharts安装库

 2、D:\...\Python\Python310\Lib\site-packages\pyecharts\render\engine.py 中

from collections import Iterable  

改为

from collections.abc import Iterable

 

3、如果报错: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
  解决方法:在代码开头插入如下语句,即可关闭警告

  from pyecharts.globals import WarningType 
  WarningType.ShowWarning = False

 

4、作图代码如下:

# 从pyecharts.charts导入Map
from pyecharts.charts import Map

# 从pyecharts导入options,简称为opts
from pyecharts import options as opts
#关闭报错警告
from pyecharts.globals import WarningType 
WarningType.ShowWarning = False

# 使用import导入openpyxl模块
import openpyxl

# 将文件路径,赋值给path
path = r"\**.xlsx"

# 使用openpyxl.load_workbook()读取文件,赋值给wb
wb = openpyxl.load_workbook(path)

# 使用中括号打开工作表"各省份付费用户数",赋值给sheet_user
sheet_user = wb["sheet1"]

# 新建列表usersList
usersList = []

# for循环遍历range()函数生成的2-32的数字
for n in range(2,19):

    # 使用sheet_user[n]读取每一行的数据,赋值给province
    province = sheet_user[n]

    # 索引province的第一项和第二项
    # .value属性获取单元格值
    # 以元组的格式组合,赋值给data
    data = (province[0].value, province[1].value)

    # 使用append()函数将data添加进usersList
    usersList.append(data)

# 创建Map对象赋值给变量mapChart
mapChart = Map()

# 调用add()函数,添加参数series_name,参数值为空
# 添加参数data_pair,参数值为usersList
# 添加参数maptype,参数值为"china"
mapChart.add(
    series_name="",
    data_pair=usersList,
    maptype="湖北"
    )

# 使用set_global_opts()添加视觉映射配置项
# 添加参数visualmap_opts,参数值为opts.VisualMapOpts()
# 将最大值设置为 100
# 添加参数title_opts,参数值为opts.TitleOpts()
# 设置标题
mapChart.set_global_opts(
    visualmap_opts=opts.VisualMapOpts(max_=1000),
    title_opts=opts.TitleOpts(title="各市州灾情次数")
)

# 使用render()函数保存并命名地图
# 保存路径为
mapChart.render(r"\map.html")

图形显示:

posted @ 2022-09-05 14:12  EROEG  阅读(97)  评论(0编辑  收藏  举报