罗宏昆大数据科学可视化的作业
前端Web可视化
代码部分
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.faker import Faker
c = (
Pie()
.add(
"",
[list(z) for z in zip(Faker.choose(), Faker.values())],
radius=["40%", "55%"],
label_opts=opts.LabelOpts(
position="outside",
formatter="{a|{a}}{abg|}\n{hr|}\n {b|{b}: }{c} {per|{d}%} ",
background_color="#eee",
border_color="#aaa",
border_width=1,
border_radius=4,
rich={
"a": {"color": "#999", "lineHeight": 22, "align": "center"},
"abg": {
"backgroundColor": "#e3e3e3",
"width": "100%",
"align": "right",
"height": 22,
"borderRadius": [4, 4, 0, 0],
},
"hr": {
"borderColor": "#aaa",
"width": "100%",
"borderWidth": 0.5,
"height": 0,
},
"b": {"fontSize": 16, "lineHeight": 33},
"per": {
"color": "#eee",
"backgroundColor": "#334455",
"padding": [2, 4],
"borderRadius": 2,
},
},
),
)
.set_global_opts(title_opts=opts.TitleOpts(title="Pie-富文本示例"))
.render("pie_rich_label.html")
)
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType
c = (
Geo()
.add_schema(
maptype="china",
itemstyle_opts=opts.ItemStyleOpts(color="#ddb6b6", border_color="#111"),
)
.add(
"",
[("广州", 55), ("北京", 66), ("杭州", 77), ("重庆", 88)],
type_=ChartType.EFFECT_SCATTER,
color="white",
)
.add(
"geo",
[("广州", "上海"), ("广州", "北京"), ("广州", "杭州"), ("广州", "重庆")],
type_=ChartType.LINES,
effect_opts=opts.EffectOpts(
symbol=SymbolType.ARROW, symbol_size=6, color="blue"
),
linestyle_opts=opts.LineStyleOpts(curve=0.2),
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="Geo-Lines-background"))
.render("geo_lines_background.html")
)
最后的可视化效果
科学可视化
对于飞机的三维点云进行可视化展示
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df=df=pd.read_excel('D:\OneDrive - cumt.edu.cn\桌面\数据集\飞机点云数据集.xlsx')
arr=df.values
fig = plt.figure()
bxs = arr[:,0]
bys = arr[:,1]
bzs = arr[:,2]
bx = fig.add_subplot(111, projection='3d')
bx.scatter(bxs, bys, bzs, c=c, marker=m)
bx.set_xlabel('X Label')
bx.set_ylabel('Y Label')
bx.set_zlabel('Z Label')
# 点在 3 个坐标平面上的投影
bx.scatter(0, bys, bzs, zdir='z', c='green', s=bzs*28 ) # 投影在 yz 平面
bx.scatter(bxs, 0, bzs, zdir='z', c='gray', s=bzs*28 ) # 投影在 xz 平面
bx.scatter(bxs, bys, 0, zdir='z', c='cyan', s=bzs*28 ) # 投影在 xy 平面
# 调整视角
bx.view_init(elev=120, # 仰角
azim=45 # 方位角
)
plt.show()
基于pyechart的三维地图可视化
各个省份奶制品的消费量
from pyecharts import options as opts
from pyecharts.charts import Map3D
from pyecharts.globals import ChartType
from pyecharts.commons.utils import JsCode
example_data = [
("黑龙江", [127.9688, 45.368, 10.6]),
("内蒙古", [110.3467, 41.4899, 24.8]),
("吉林", [125.8154, 44.2584, 11.6]),
("辽宁", [123.1238, 42.1216, 17.7]),
("河北", [114.4995, 38.1006, 16.5]),
("天津", [117.4219, 39.4189, 16.8]),
("山西", [112.3352, 37.9413, 17.9]),
("陕西", [109.1162, 34.2004, 14.7]),
("甘肃", [103.5901, 36.3043, 15.7]),
("宁夏", [106.3586, 38.1775, 13.5]),
("青海", [101.4038, 36.8207, 16.7]),
("新疆", [87.9236, 43.5883, 18.7]),
("西藏", [91.11, 29.97, 8.5]),
("四川", [103.9526, 30.7617, 10]),
("重庆", [108.384366, 30.439702, 14]),
("山东", [117.1582, 36.8701, 17.6]),
("河南", [113.4668, 34.6234, 13.5]),
("江苏", [118.8062, 31.9208, 15.4]),
("安徽", [117.29, 32.0581, 11.3]),
("湖北", [114.3896, 30.6628, 7.7]),
("浙江", [119.5313, 29.8773, 14.7]),
("福建", [119.4543, 25.9222, 11.7]),
("江西", [116.0046, 28.6633, 11.2]),
("湖南", [113.0823, 28.2568, 7.4]),
("贵州", [106.6992, 26.7682, 5.1]),
("广西", [108.479, 23.1152, 5.6]),
("海南", [110.3893, 19.8516, 4.9]),
("上海", [121.4648, 31.2891, 23.1]),
]
c = (
Map3D()
.add_schema(
itemstyle_opts=opts.ItemStyleOpts(
color="rgb(5,101,123)",
opacity=1,
border_width=0.8,
border_color="rgb(62,215,213)",
),
map3d_label=opts.Map3DLabelOpts(
is_show=False,
formatter=JsCode("function(data){return data.name + " " + data.value[2];}"),
),
emphasis_label_opts=opts.LabelOpts(
is_show=False,
color="#fff",
font_size=10,
background_color="rgba(0,23,11,0)",
),
light_opts=opts.Map3DLightOpts(
main_color="#fff",
main_intensity=1.2,
main_shadow_quality="high",
is_main_shadow=False,
main_beta=10,
ambient_intensity=0.3,
),
)
.add(
series_name="bar3D",
data_pair=example_data,
type_=ChartType.BAR3D,
bar_size=1,
shading="lambert",
label_opts=opts.LabelOpts(
is_show=False,
formatter=JsCode("function(data){return data.name + ' ' + data.value[2];}"),
),
)
.set_global_opts(title_opts=opts.TitleOpts(title="Map3D-Bar3D"))
.render("map3d_with_bar3d.html")
)
运行代码会生成map3d_with_bar3d.html文件,用浏览器打开即可
部分211大学12月百度指数动态图
数据集
数据集是我花5块钱买的211大学的百度指数
默认效果
import pandas as pd
import bar_chart_race as bcr
import matplotlib
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 显示中文
# 为了坐标轴负号正常显示。matplotlib默认不支持中文,设置中文字体后,负号会显示异常。需要手动将坐标轴负号设为False才能正常显示负号。
matplotlib.rcParams['axes.unicode_minus'] = False
df = pd.read_csv('D:\OneDrive - cumt.edu.cn\桌面\大数据可视化\data.csv', index_col=["time"])
# 生成 GIF 图像
bcr.bar_chart_race(df,
"211_schools_rank.gif",
)
代码1
import pandas as pd
import bar_chart_race as bcr
import matplotlib
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 显示中文
# 为了坐标轴负号正常显示。matplotlib默认不支持中文,设置中文字体后,负号会显示异常。需要手动将坐标轴负号设为False才能正常显示负号。
matplotlib.rcParams['axes.unicode_minus'] = False
df = pd.read_csv('D:\OneDrive - cumt.edu.cn\桌面\大数据可视化\data.csv', index_col=["time"])
# 生成 GIF 图像
bcr.bar_chart_race(df,
"211_schools_rank.gif",
fixed_max=True,
steps_per_period=30,
period_length=100,
title='部分大学2021年12月百度指数动态变化图',
bar_kwargs={'alpha': .2, 'ec': 'black', 'lw': 3}
)
结果1
代码2(python)
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Bar, Timeline
import random
# 读取数据
df = pd.read_csv(r"D:\code\python code\动态水平柱状图参考案例-疫情分析\data_1.csv")
# 建立时间轴
l = df["time"].size
date = []
errordate = ["2020-07-10", "2020-01-23", "2020-07-28"] # 去除异常数据
for i in range(l - 1, -1, -1):
if df["time"][i] not in date and df["time"][i] not in errordate:
date.append(df["time"][i]) # 建立时间轴
color_dir = {} # 存储颜色字典
color = [ # 颜色库
"#c1232b",
"#27727b",
"#fcce10",
"#e87c25",
"#b5c334",
"#fe8463",
"#9bca63",
"#fad860",
"#f3a43b",
"#60c0dd",
"#d7504b",
"#c6e579",
"#f4e001",
"#f0805a",
"#26c0c0"
]
tl = Timeline() # 建立时间序列图
f = 0
for i in date:
if i == "2020-03-15":
f = 1
# 排序
df_sub = df[df["time"] == i].sort_values(by="value", ascending=False)
df_sub = df_sub[df_sub["value"] != 0]
df_new = df_sub[0:1]
# 选择前10的国家喝数量
school = [df_new.iat[0, 1]]
counts = [float(df_new.iat[0, 2])]
l = df_sub["time"].size
# 提取第i天的数据
for j in range(l):
if df_sub.iat[j, 1] not in school:
if df_sub.iat[j, 0] == "中国" and f:
if df_sub.iat[j, 1] != "中国":
continue
school.append(df_sub.iat[j, 1])
counts.append(float(df_sub.iat[j, 2]))
if school[-1] not in color_dir.keys():
color_dir[school[-1]] = random.choice(color)
if len(school) >= 12:
break
school = school[::-1]
counts = counts[::-1]
# 填充颜色
y = []
for j in range(len(school)):
if j == len(school) - 1:
y.append(
opts.BarItem(
name=school[j],
value=counts[j],
itemstyle_opts=opts.ItemStyleOpts(color="#c1232b"),
)
)
else:
y.append(
opts.BarItem(
name=school[j],
value=counts[j],
itemstyle_opts=opts.ItemStyleOpts(color=color_dir[school[j]]),
)
)
bar = (
Bar(init_opts=opts.InitOpts(bg_color='rgb(133, 133, 173)',
width='1000px',
height='600px',
page_title='page',
))
.add_xaxis(school)
.add_yaxis("", y, label_opts=opts.LabelOpts(position="top", font_weight="bold", ), category_gap=20,
markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(name="", value=school[11], coord=[counts[11], school[11]],
itemstyle_opts=opts.ItemStyleOpts(color="#c1232b",
border_color='#f2d643')),
opts.MarkPointItem(name="", value=school[10], coord=[counts[10], school[10]],
itemstyle_opts=opts.ItemStyleOpts(color=color_dir[school[10]],
border_color='grey')),
opts.MarkPointItem(name="", value=school[9], coord=[counts[9], school[9]],
itemstyle_opts=opts.ItemStyleOpts(color=color_dir[school[9]],
border_color='#000000')),
]
))
.reversal_axis()
.set_global_opts(
title_opts=opts.TitleOpts("211大学百度指数排名(时间: {} )".format(i), pos_right=350, padding=[30, 20]),
xaxis_opts=opts.AxisOpts(max_=int(max(counts) * 1.05))
)
)
tl.add(bar, "{}".format(i))
tl.add_schema(
play_interval=400,
is_loop_play=True,
axis_type="category",
orient="horizontal",
# 是否自动播放
is_auto_play=True,
# 控制时间轴的宽高
width=500,
) # 播放间隔,是否循环播放
tl.render("aaa.html")
代码2(html)
/*主要样式设置*/
<style>
#mp3 {
width: 30px;
height: 30px;
position: absolute;
left: 200px;
bottom: 350px;
}
#mp4 {
width: 30px;
height: 30px;
position: absolute;
left: 1000px;
top: 10px;
}
#text {
font-size: 50px;
color: rgb(243, 223, 46);
position: absolute;
left: 30px;
bottom: 200px;
border-width: 10px;
border-style: groove;
border-color: rgb(8, 232, 240);
}
#author {
font-size: 50px;
color: rgb(243, 223, 46);
position: absolute;
left: 1250px;
bottom: 50px;
border-width: 10px;
border-style: groove;
border-color: rgb(8, 232, 240);
font-family: LiSu;
}
</style>
<body>
<div id="Layer1" style="position:fixed; left:0px; top:0px; width:100%; height:100%;opacity:0.8">
<img src="pic1.jpg" width="100%" height="100%"/>
</div>
<div id="30754297562d43ae9633a3caa723e521" class="chart-container" style="width:900px; height:500px;">
</div>
<div id="mp3">
<audio controls="controls" loop="loop" autoplay="autoplay">
<source src="voice.mp3" type="audio/mp3"></source>
</audio>
</div>
<div id="mp4">
<video controls="controls" loop="loop">
<source src="video.mp4" type="video/mp4"></source>
</video>
</div>
<div id="text">
2021年12月部分211大学百度指数排名
</div>
<div id="author">
作者:测绘工程20-1班罗宏昆
</div>