Day13-pyecharts的使用作业

该代码示例展示了如何使用pyecharts库从Excel文件中读取数据,创建柱状图、饼图和地图来展示全国大学的分类数量以及中国各省份的生产总值。通过openpyxl处理Excel文件,然后利用pyecharts进行数据可视化,生成HTML报告。
摘要由CSDN通过智能技术生成

Day13-pyecharts的使用作业

import openpyxl
from pyecharts.charts import Bar
from pyecharts import options

wb = openpyxl.open('files/全国大学.xlsx')
sheet = wb.active
m_r = sheet.max_row
data = {}
for r in range(2, m_r + 1):
    val = sheet.cell(r, 3).value
    if val not in data:
        data[val] = 1
    else:
        data[val] += 1

x_data = []
y_data = []
for key in data:
    x_data.append(key)
    y_data.append(data[key])

bar = Bar()
bar.add_xaxis(x_data)
bar.add_yaxis('数量', y_data)

bar.set_global_opts(
    title_opts=options.TitleOpts(
        title='全国大学的分类及数量',
        pos_right='350'
    ),
    legend_opts=options.LegendOpts(
        is_show=False
    ),
    xaxis_opts=options.AxisOpts(
        name='分类'
    ),
    yaxis_opts=options.AxisOpts(
        name='数量'
    )
)

bar.set_series_opts(
    label_opts=options.LabelOpts(
        position='top'
    )
)

bar.render('files/全国大学的分类数量柱状图.html')

111

import openpyxl
from pyecharts.charts import Pie
from pyecharts import options

wb = openpyxl.open('files/全国大学.xlsx')
sheet = wb.active
m_r = sheet.max_row
data = {}
for r in range(2, m_r + 1):
    val = sheet.cell(r, 3).value
    if val not in data:
        data[val] = 1
    else:
        data[val] += 1

pie = Pie()
pie_data = []
for key in data:
    pie_data.append((key, data[key]))

pie.add('数量', pie_data, radius=(50, 100))

pie.set_global_opts(
    legend_opts=options.LegendOpts(
        is_show=False
    ),
    title_opts=options.TitleOpts(
        title='全国大学的分类及数量',
        pos_right='350'
    )
)

pie.render('files/全国大学的分类数量饼图.html')

222

from pyecharts.charts import Map
from pyecharts import options
import xlrd, xlwt

wb = xlrd.open_workbook('files/分省年度数据.xls')
sheet = wb.sheet_by_name('分省年度数据')
m_r = sheet.nrows - 1
data = []
for r in range(4, m_r):
    val1 = sheet.cell(r, 0).value
    val2 = float(sheet.cell(r, 1).value)
    data.append((val1, val2))

map1 = Map()
map1.add('地区生产总值(亿元)', data, zoom=1.5, min_scale_limit=1, max_scale_limit=3)

map1.set_global_opts(
    visualmap_opts=options.VisualMapOpts(
        is_show=True,
        is_piecewise=True,
        pieces=[
            {'min': 0, 'max': 9999},
            {'min': 10000, 'max': 14999},
            {'min': 15000, 'max': 29999},
            {'min': 30000, 'max': 59999},
            {'min': 60000, 'max': 99999},
            {'min': 100000, 'max': 150000}
        ]
    ),
    legend_opts=options.LegendOpts(
        is_show=False
    ),
    title_opts=options.TitleOpts(
        title='中国2022年各省份生产总值(亿元)',
        pos_right='350'
    )
)

map1.render('files/分省年度数据地图.html')

333

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值