20230222 Python图表绘制

1. Python柱状图

from pyecharts.charts import Bar
from pyecharts import options

# 1.创建图表对象
bar = Bar()

# 2.添加数据
# 2.1 添加x轴上的数据
bar.add_xaxis(['1季度', '2季度', '3季度', '4季度'])
# 2.2添加y轴上的数据(x轴与y轴上的数据内容和顺序需保持一致)
bar.add_yaxis('销售额', [890, 1023, 508, 1500],
              # color='red',
              # bar_width=30,
              bar_max_width=100,
              bar_min_width=40,
              )
bar.add_yaxis('销量', [201, 342, 988, 567])

# 3.柱状图相关配置
# 3.1 全局配置
bar.set_global_opts(
    # 设置标题
    title_opts=options.TitleOpts(
        title='2002年手机销售信息',
        title_link='https://www.baidu.com',
        title_target='blank',
        subtitle='销售额与销量的关系',
        pos_right='300',
        text_align='center'
    ),
    # 设置图例
    legend_opts=options.LegendOpts(
        is_show=True,
        # pos_right=90,
        pos_left=0,
        item_width=20,
        item_height=10
    ),
    # 设置工具箱
    toolbox_opts=options.ToolboxOpts(
        is_show=True,
        pos_left='65%',
    ),
    # 设置区域缩放工具条
    # datazoom_opts=options.DataZoomOpts(
    #     is_show=True
    # ),
    # x、y轴的配置
    xaxis_opts=options.AxisOpts(
        name='时间'
    ),
    yaxis_opts=options.AxisOpts(
        name='销售额(万元)/销量',
        name_gap=-55,
        name_rotate=45,
    )
)
# 3.2 系列配置
bar.set_series_opts(
    label_opts=options.LabelOpts(
        is_show=True,
        position='top',
        rotate=0,
        color='#36ff77'
    ),
    markpoint_opts=options.MarkPointOpts(
        data=[options.MarkPointItem(name='最大值', type_='max')]
    )
)
# 3.3 数据配置
# 数据配置只能在添加数据的时候设置

# 4.渲染图表(生成图表)
bar.render('files/01柱状图.html')

在这里插入图片描述

2. Python折线图

from pyecharts.charts import Line
from pyecharts import options

# 1.创建折线图对象
line = Line()

# 2.添加数据
line.add_xaxis(['1季度', '2季度', '3季度', '4季度'])

line.add_yaxis('服装', [890, 1023, 508, 1500],
               is_smooth=True,)
line.add_yaxis('电子产品', [80, 77, 199, 56])

# 3.图标设置
line.set_global_opts(
    title_opts=options.TitleOpts(
        title='xxx公司各大产品销售情况',
        pos_left='350',
    ),
    legend_opts=options.LegendOpts(
        pos_right=0
    ),
    yaxis_opts=options.AxisOpts(
        name='销售额(万元)'
    ),
    xaxis_opts = options.AxisOpts(
        name='时间'
    )
)
line.set_series_opts(
    label_opts=options.LabelOpts(
        is_show=False,
    ),
    markline_opts=options.MarkLineOpts(
        data=[options.MarkLineItem(name='平均销售额', type_='average')]
    )
)

# 4.渲染图表(生成图表)
line.render('files/02折线图.html')

在这里插入图片描述

3. Python饼图

from pyecharts.charts import Pie
from pyecharts import options

# 1.创建饼图对象
pie = Pie()

# 2.添加数据
pie.add(
    '手机销量',
    [('Apple', 890), ('小米', 666), ('华为', 500), ('oppo', 120), ('魅族', 175)],
    # 分别设置圆的内圆半径和外圆半径
    radius=('30%', '70%'),
    # 让外圆环不规则
    rosetype='radius'
)

# 3.图表设置
pie.set_global_opts(
    legend_opts=options.LegendOpts(
        is_show=False
    ),
    title_opts=options.TitleOpts(
        title='xx商城6月手机销量百分比',
        pos_left='350'
    )
)
pie.set_series_opts(
    label_opts=options.LabelOpts(
        # {b}  -  数据名称、
        # {d}  -  百分比比值
        formatter='{b}({d}%)'
    )
)

# 4.渲染图表(生成图表)
pie.render('files/03饼图.html')

在这里插入图片描述

4. Python地图

from pyecharts.charts import Map
from pyecharts import options

# 一、中国地图========================
# 1. 创建地图对象
map1 = Map()

# 2.添加数据
map1.add(
    '房价',
    [('南海诸岛', 2000), ('重庆市', 19000), ('广东省', 39500), ('四川省', 18000),
     ('北京市', 52000), ('上海市', 65000), ('新疆维吾尔自治区', 4500), ('吉林省', 7000), ('黑龙江省', 3800)],
    # 地图默认大小
    zoom=1.2,
    # 地图最小最大缩放比
    min_scale_limit=1.2,
    max_scale_limit=2
)

# 3.添加配置
map1.set_global_opts(
    visualmap_opts=options.VisualMapOpts(
        is_show=True,
        min_=0,
        max_=70000,
        # 让颜色分段
        is_piecewise=True,
        pieces=[
            {'min': 0, 'max': 9999},
            {'min': 10000, 'max': 19999},
            {'min': 20000, 'max': 29999},
            {'min': 30000, 'max': 39999},
            {'min': 40000, 'max': 49999},
            {'min': 50000, 'max': 59999},
            {'min': 60000, 'max': 79999},
        ],
    ),
    legend_opts=options.LegendOpts(
        is_show=False
    ),
    title_opts=options.TitleOpts(
        title='2023年全国房价情况',
        subtitle='副标题11111111'
    )
)

# 4.渲染地图
map1.render('files/04中国地图.html')

# 二、四川地图========================
map2 = Map()
map2.add(
    '房价',
    [('成都市', 12000), ('乐山市', 6500)],
    maptype='四川'

)
map2.render('files/04四川地图.html')

# 三、成都市地图========================
map3 = Map()
map3.add(
    '房价',
    [('青羊区', 12000), ('武侯区', 6500)],
    maptype='成都'

)
map3.render('files/04成都地图.html')

# 四、世界地图========================
map4 = Map()
map4.add(
    '房价',
    [('China', 10000), ('Russia', 9000)],
    maptype='world'
)
map4.set_global_opts(
    legend_opts=options.LegendOpts(
        is_show=False
    )
)
map4.set_series_opts(
    label_opts=options.LabelOpts(
        is_show=False
    )
)
map4.render('files/04世界地图.html')

在这里插入图片描述

5. Python词云

from pyecharts.charts import WordCloud
from pyecharts import options

# 1.创建词云图表
wc = WordCloud()

# 2.添加数据
wc.add(
    '',
    [('王者荣耀', 88985), ('英雄联盟', 77900), ('开心消消乐', 5896), ('羊了个羊', 758),
     ('飞机大战', 35000), ('刀塔', 3362), ('魔兽世界', 4887), ('红色警戒', 52662),
     ('节奏大师', 99), ('CS1.5', 5555), ('地球帝国', 3685), ('超级玛丽', 620),
     ('街头篮球', 800), ('传奇', 69870), ('魔兽争霸', 1562), ('魂斗罗', 756)],
    shape='star',
    # shape词云的形状,默认是 circle(圆形),可选的参数有:
    # cardioid(心形)
    # diamond(菱形 正方形)
    # triangle-forward 、 triangle(三角形)
    # star(星形)
    # pentagon(五边形)

    # word_size_range=[50, 200]
)

wc.render('files/05词云.html')

在这里插入图片描述

6. 基于Excel数据制作图表

import openpyxl
from pyecharts.charts import Bar, Pie

# 1.提取Excel中的数据
wb = openpyxl.open('files/全国大学.xlsx')
sheet = wb.active
data = {}
max_r = sheet.max_row
for r in range(2, max_r+1):
    value = sheet.cell(r, 3).value
    if value not in data:
        data[value] = 1
    else:
        data[value] += 1
# print(data)

# 2.创建柱状图
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.render('files/06全国大学柱状图示例.html')

# 3.创建饼图
pie_data = []
for x in data:
    pie_data.append((x, data[x]))

pie = Pie()
pie.add('数量', pie_data)
pie.render('files/06全国大学饼图示例.html')

在这里插入图片描述
在这里插入图片描述

7. 利用cpca提取省市区

import cpca

data = '徐汇区虹漕路461号58号楼5楼64号'

place_list = list()
place_list.append(data)
print(place_list)
integrity_place = cpca.transform(place_list)
# print(integrity_place)
# 省 市 区  地址 adcode
# 0 广东省  广州市  None  新溪项目河涌整治景观提升设计招标  440100
print(integrity_place['省'][0] + integrity_place['市'][0] + integrity_place['区'][0] + integrity_place['地址'][0] + ',区号' + integrity_place['adcode'][0])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值