python可视化-pyecharts实现3D图、散点图和雷达图
程序员文章站
2022-07-12 23:27:03
...
3D图
from pyecharts import options as opts
from pyecharts.charts import Bar3D
import csv
# 读数据
def readDbar():
contentList=[]
with open('video.csv','r',encoding='utf-8') as file:
csvReader = csv.reader(file)
for item in csvReader:
contentList.append(item)
return contentList
# 生成对应的图表
def make3Dbar(contentList):
bar = Bar3D(init_opts=opts.InitOpts(width='1200px',height='800px'))
# Java h5 python 大数据 其他
types = ['h5','java','python','大数据','其他']
# 年限
# 三个年限列表
playCount7 = [0,0,0,0,0]
playCount8 = [0,0,0,0,0]
playCount9 = [0,0,0,0,0]
# 根据读取的数据 将每一年的每一学科的播放量进行统计
for item in contentList:
type = item[1]
time = item[5].split('/')[0]
#播放量
playCount = int(item[3])
if type == 'h5':
if time == '2017':
playCount7[0]=playCount7[0]+playCount
elif time == '2018':
playCount8[0]=playCount8[0]+playCount
if time == '2019':
playCount9[0] = playCount9[0] + playCount
elif type == 'java':
if time == '2017':
playCount7[1]=playCount7[0]+playCount
elif time == '2018':
playCount8[1]=playCount8[0]+playCount
if time == '2019':
playCount9[1] = playCount9[0] + playCount
elif type == 'python':
if time == '2017':
playCount7[2]=playCount7[2]+playCount
elif time == '2018':
playCount8[2]=playCount8[2]+playCount
if time == '2019':
playCount9[2] = playCount9[2] + playCount
elif type == '大数据':
if time == '2017':
playCount7[3]=playCount7[3]+playCount
elif time == '2018':
playCount8[3]=playCount8[3]+playCount
if time == '2019':
playCount9[3] = playCount9[3] + playCount
elif type == '其他':
if time == '2017':
playCount7[4]=playCount7[4]+playCount
elif time == '2018':
playCount8[4]=playCount8[4]+playCount
if time == '2019':
playCount9[4] = playCount9[4] + playCount
print(playCount7,playCount8,playCount9)
# 学科对应的年限 年 学科 播放量 ==定义一个二位列表 让不同年份和播放分开
result= []
for i in range(len(types)):
item = []
item.append(types[i])
item.append('2017')
item.append(playCount7[i])
result.append(item)
for i in range(len(types)):
item = []
item.append(types[i])
item.append('2018')
item.append(playCount8[i])
result.append(item)
for i in range(len(types)):
item = []
item.append(types[i])
item.append('2019')
item.append(playCount9[i])
result.append(item)
bar.add('学科播放量统计',result)
bar.render('学科播放量统计3D图.html')
if __name__ == '__main__':
contentList = readDbar()
make3Dbar(contentList)
代码中文档:video.csv
格式如下:之作部分展示
【教育】web前端 1000集全套(学完可就业/持续更新~),h5,274:53:22,393000,11000,2019/3/11,逆风微笑的代码狗,http://www.bilibili.com/video/av45from=search&seid=11791596652186258788
【程序员】2019Python全套****,python,201:20:27,7601,260,2019/8/2,教育,http://www.bilibili.com/video/av6from=search&seid=11791596652186258788
【教育】Python 900集(学完可就业/2019版),python,350:51:44,41000,848,2019/9/27,教育,http://www.bilibili.com/video/from=search&seid=11791596652186258788
【教育】Web安全 kali Linux渗透攻防项目实战,其他,12:16:31,3778,9,2019/8/3,逆狗,http://www.bilibili.com/video/av62065508?from=sea
【(教育)课外拓展】Spring Security oAut
结果:
散点图
from pyecharts import options as opts
from pyecharts.charts import Scatter
import pandas as pd
from pyecharts.commons.utils import JsCode
def scatter_render():
df = pd.DataFrame(
{
'年龄':[18,23,28,30,32,40,19,45],
'信用积分':[630,710,730,740,680,700,650,800],
'姓名':['小明','彦祖','德华','大聪明','rose','tom','小丽丽','刘能']
}
)
# 年龄的排序
df.sort_values('年龄',inplace=True,ascending=True)
c = (
Scatter()
.add_xaxis(df.年龄.values.tolist())
.add_yaxis(
'信用积分',
df[['信用积分','姓名']].values.tolist(),
label_opts=opts.LabelOpts(
formatter=JsCode(
# js中函数的写法 通过定义js的回调函数自定义组合的标签显示
"function(params){return params.value[2];}"
)
)
)
.set_global_opts(
title_opts=opts.TitleOpts(title='散点图图例'),
# 设置数据的范围和数据的连续性
xaxis_opts=opts.AxisOpts(
type_='value',
min_=18
),
yaxis_opts=opts.AxisOpts(
min_=600
)
)
)
return c
scatter_render().render('散点图显示.html')
结果:
雷达图
from pyecharts import options as opts
from pyecharts.charts import Radar
v1 = [[8.5,100000,15000,5000,13000,5000]]
v2 = [[8.1,120000,16000,7000,15000,7000]]
def radar_base() ->Radar:
c = (
Radar()
.add_schema(
schema=[
opts.RadarIndicatorItem(name='KDA',max_=10),
opts.RadarIndicatorItem(name='输出', max_=140000),
opts.RadarIndicatorItem(name='经济', max_=20000),
opts.RadarIndicatorItem(name='生存', max_=10000),
opts.RadarIndicatorItem(name='推进', max_=20000),
opts.RadarIndicatorItem(name='刷野', max_=10000),
]
)
.add(
'鲁班',v1,
color='green',
#通过颜色属性 将其填充
areastyle_opts=opts.AreaStyleOpts(
opacity=0.5,
color='green'
),
)
.add(
'后裔',v2,
color='red',
areastyle_opts=opts.AreaStyleOpts(
opacity=0.5,
color='red'
),
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title='英雄成长属性对比'))
)
return c
radar_base().render("雷达图.html")
结果: