1、说在前面的话
1.1 这个是上课老师让练习的,所以就根据之前的方法改写+网上找各种方法写成的这个学生城市分布热点图
1.2 其中有很多可以优化的,我以后有空了可以优化一部分,当然也希望广大网友提出意见和建议
1.3 代码中有点注释可能是之前的,所以有的注释和当前题目要求不一定对应
2、代码
2.1 py文件
from pyecharts.charts import Geo
from pyecharts import options as opts
import csv
from pyecharts.globals import ChartType
'''
1.1和1.2分别是对取csv文件的数据为列表和字典(这个只用到了1.1和1.3)
1.3列表转换为有数量的字典
'''
#1.1读取csv文件,把性别信息读取出来
def getInfo(filename,index):#index 参数代表从第几列开始读取
lstdata=[]
with open(filename,'r') as fr:
reader=csv.reader(fr)
for i in reader:
lstdata.append(i[index])
return lstdata
#1.2读取csv文件,并把寝室号信息读取出来
def getValues(filename):
lstsex = {}
with open(filename,'r') as f:
reader =csv.reader(f)
for i in reader:
lstsex[i[3]]=i[9]
return lstsex
#1.3列表转换为有数量的字典
def listToDict(lstsex):
wc = dict()#{}
for i in range(len(lstsex)):
word = lstsex[i]
if word not in wc:
wc[word] = 1
else:
wc[word] += 1
return wc
def GeoMapTest(lstsex)->Geo:
wc=listToDict(lstsex)
#模拟几个数据
#wc={'黑龙江省':12,'云南省':2}
#x是字典的keys值,里面包含了所有的城市
x=list(wc.keys())#字典转为列表需要使用list才能转成功
#y是字典的value值,里面包含了每一个城市的学生数
y=list(wc.values())
print(x)
print(y)
#热点图
aa = [list(z) for z in zip(x, y)]
c = (
Geo()
.add_schema(maptype="china")#https://blog.csdn.net/chenxiaodan_danny/article/details/39081071
.add(
"软件18级学生城市热点图", # 图题
# wc,
aa,
type_=ChartType.HEATMAP, # 地图类型
).set_series_opts(label_opts=opts.LabelOpts(is_show=True)) # 设置是否显示标签
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(max_=3), # 设置legend显示的最大值//这里设置为3的目的是为了显示对比效果更明显,当然好像最大也就是3左右了
title_opts=opts.TitleOpts(title="Geo-HeatMap") # 左上角标题
)
).render(path="test_heatmap.html")
return c
GeoMapTest(getInfo("../file/软件18学生详细名单new2.csv", 6))
2.2 file里面的学生信息文件改成对应的文件就好了,这里就不放文件了
3、运行结果
3.1运行结果图片
3.2 HTML文件: 正常情况下把下面的HTML代码复制保存为一个html文件就可以直接运行了。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Awesome-pyecharts</title>
<script type="text/javascript" src="https://assets.pyecharts.org/assets/echarts.min.js"></script>
<script type="text/javascript" src="https://assets.pyecharts.org/assets/maps/china.js"></script>
</head>
<body>
<div id="5dcd7985418b4e1ca4889cc6c2dacec5" class="chart-container" style="width:900px; height:500px;"></div>
<script>
var chart_5dcd7985418b4e1ca4889cc6c2dacec5 = echarts.init(
document.getElementById('5dcd7985418b4e1ca4889cc6c2dacec5'), 'white', {renderer: 'canvas'});
var option_5dcd7985418b4e1ca4889cc6c2dacec5 = {
"animation": true,
"animationThreshold": 2000,
"animationDuration": 1000,
"animationEasing": "cubicOut",
"animationDelay": 0,
"animationDurationUpdate": 300,
"animationEasingUpdate": "cubicOut",
"animationDelayUpdate": 0,
"color": [
"#c23531",
"#2f4554",
"#61a0a8",
"#d48265",
"#749f83",
"#ca8622",
"#bda29a",
"#6e7074",
"#546570",
"#c4ccd3",
"#f05b72",
"#ef5b9c",
"#f47920",
"#905a3d",
"#fab27b",
"#2a5caa",
"#444693",
"#726930",
"#b2d235",
"#6d8346",
"#ac6767",
"#1d953f",
"#6950a1",
"#918597"
],
"series": [
{
"type": "heatmap",
"name": "\u8f6f\u4ef618\u7ea7\u5b66\u751f\u57ce\u5e02\u70ed\u70b9\u56fe",
"coordinateSystem": "geo",
"data": [
{
"name": "\u5317\u4eac\u5e02",
"value": [
116.4,
39.9,
1
]
},
{
"name": "\u9a6c\u978d\u5c71\u5e02",
"value": [
118.5,
31.7,
1
]
},
{
"name": "\u798f\u5dde\u5e02",
"value": [
119.3,
26.08,
1
]
},
{
"name": "\u5f20\u6396\u5e02",
"value": [
100.45,
38.93,
1
]
},
{
"name": "\u4e2d\u5c71\u5e02",
"value": [
113.38,
22.52,
1
]
},
{
"name": "\u5317\u6d77\u5e02",
"value": [
109.12,
21.48,
1
]
},
{
"name": "\u65b0\u4e61\u5e02",
"value": [
113.9,
35.3,
4
]
},
{
"name": "\u4f73\u6728\u65af\u5e02",
"value": [
130.37,
46.82,
4
]
},
{
"name": "\u5f20\u5bb6\u53e3\u5e02",
"value": [
114.88,
40.82,
1
]
},
{
"name": "\u6fee\u9633\u5e02",
"value": [
115.03,
35.77,
2
]
},
{
"name": "\u4fe1\u9633\u5e02",
"value": [
114.07,
32.13,
1
]
},
{
"name": "\u54c8\u5c14\u6ee8\u5e02",
"value": [
126.53,
45.8,
5
]
},
{
"name": "\u4f0a\u6625\u5e02",
"value": [
128.9,
47.73,
1
]
},
{
"name": "\u8bb7\u6cb3\u5e02",
"value": [
124.87,
48.48,
1
]
},
{
"name": "\u4ed9\u6843\u5e02",
"value": [
113.45,
30.37,
2
]
},
{
"name": "\u90f4\u5dde\u5e02",
"value": [
113.02,
25.78,
1
]
},
{
"name": "\u5a04\u5e95\u5e02",
"value": [
112,
27.73,
1
]
},
{
"name": "\u65b0\u4f59\u5e02",
"value": [
114.92,
27.82,
1
]
},
{
"name": "\u8d64\u5cf0\u5e02",
"value": [
118.92,
42.27,
1
]
},
{
"name": "\u547c\u4f26\u8d1d\u5c14\u5e02",
"value": [
119.77,
49.22,
1
]
},
{
"name": "\u5fb7\u5dde\u5e02",
"value": [
116.3,
37.45,
1
]
},
{
"name": "\u6d4e\u5b81\u5e02",
"value": [
116.58,
35.42,
1
]
},
{
"name": "\u970d\u5dde\u5e02",
"value": [
111.72,
36.57,
1
]
},
{
"name": "\u7ef5\u9633\u5e02",
"value": [
104.73,
31.47,
1
]
},
{
"name": "\u5929\u6d25\u5e02",
"value": [
117.2,
39.12,
1
]
},
{
"name": "\u6606\u660e\u5e02",
"value": [
102.72,
25.05,
1
]
},
{
"name": "\u91cd\u5e86\u5e02",
"value": [
106.55,
29.57,
2
]
},
{
"name": "\u90a2\u53f0\u5e02",
"value": [
114.48,
37.07,
1
]
},
{
"name": "\u6d1b\u9633\u5e02",
"value": [
112.45,
34.62,
1
]
},
{
"name": "\u9f99\u5ca9\u5e02",
"value": [
117.03,
25.1,
1
]
},
{
"name": "\u6c55\u5934\u5e02",
"value": [
116.68,
23.35,
1
]
},
{
"name": "\u60e0\u5dde\u5e02",
"value": [
114.42,
23.12,
1
]
},
{
"name": "\u5357\u5b81\u5e02",
"value": [
108.37,
22.82,
1
]
},
{
"name": "\u5927\u65b9\u53bf",
"value": [
105.6,
27.15,
1
]
},
{
"name": "\u7f57\u7538\u53bf",
"value": [
106.75,
25.43,
1
]
},
{
"name": "\u5927\u5e86\u5e02",
"value": [
125.03,
46.58,
2
]
},
{
"name": "\u7ee5\u5316\u5e02",
"value": [
126.98,
46.63,
1
]
},
{
"name": "\u9e21\u897f\u5e02",
"value": [
130.97,
45.3,
1
]
},
{
"name": "\u9f50\u9f50\u54c8\u5c14\u5e02",
"value": [
123.95,
47.33,
1
]
},
{
"name": "\u9ec4\u77f3\u5e02",
"value": [
115.03,
30.2,
1
]
},
{
"name": "\u767d\u5c71\u5e02",
"value": [
126.42,
41.93,
1
]
},
{
"name": "\u677e\u539f\u5e02",
"value": [
124.82,
45.13,
1
]
},
{
"name": "\u626c\u5dde\u5e02",
"value": [
119.4,
32.4,
1
]
},
{
"name": "\u5bbf\u8fc1\u5e02",
"value": [
118.28,
33.97,
1
]
},
{
"name": "\u629a\u5dde\u5e02",
"value": [
116.35,
28,
1
]
},
{
"name": "\u5409\u5b89\u5e02",
"value": [
114.98,
27.12,
1
]
},
{
"name": "\u5b9c\u6625\u5e02",
"value": [
114.38,
27.8,
1
]
},
{
"name": "\u4e0a\u9976\u5e02",
"value": [
117.97,
28.45,
1
]
},
{
"name": "\u94c1\u5cad\u5e02",
"value": [
123.83,
42.28,
1
]
},
{
"name": "\u83cf\u6cfd\u5e02",
"value": [
115.480656,
35.23375,
1
]
},
{
"name": "\u5927\u540c\u5e02",
"value": [
113.3,
40.08,
1
]
},
{
"name": "\u8fd0\u57ce\u5e02",
"value": [
110.98,
35.02,
1
]
},
{
"name": "\u54b8\u9633\u5e02",
"value": [
108.7,
34.33,
1
]
},
{
"name": "\u7709\u5c71\u5e02",
"value": [
103.83,
30.05,
1
]
},
{
"name": "\u7ecd\u5174\u5e02",
"value": [
120.57,
30,
1
]
},
{
"name": "\u8386\u7530\u5e02",
"value": [
119,
25.43,
1
]
}
],
"pointSize": 20,
"blurSize": 20,
"label": {
"show": true,
"position": "top",
"margin": 8
},
"rippleEffect": {
"show": true,
"brushType": "stroke",
"scale": 2.5,
"period": 4
}
}
],
"legend": [
{
"data": [
"\u8f6f\u4ef618\u7ea7\u5b66\u751f\u57ce\u5e02\u70ed\u70b9\u56fe"
],
"selected": {
"\u8f6f\u4ef618\u7ea7\u5b66\u751f\u57ce\u5e02\u70ed\u70b9\u56fe": true
},
"show": true,
"padding": 5,
"itemGap": 10,
"itemWidth": 25,
"itemHeight": 14
}
],
"tooltip": {
"show": true,
"trigger": "item",
"triggerOn": "mousemove|click",
"axisPointer": {
"type": "line"
},
"showContent": true,
"alwaysShowContent": false,
"showDelay": 0,
"hideDelay": 100,
"formatter": function (params) { return params.name + ' : ' + params.value[2]; },
"textStyle": {
"fontSize": 14
},
"borderWidth": 0,
"padding": 5
},
"title": [
{
"text": "Geo-HeatMap",
"padding": 5,
"itemGap": 10
}
],
"visualMap": {
"show": true,
"type": "continuous",
"min": 0,
"max": 3,
"inRange": {
"color": [
"#50a3ba",
"#eac763",
"#d94e5d"
]
},
"calculable": true,
"inverse": false,
"splitNumber": 5,
"orient": "vertical",
"showLabel": true,
"itemWidth": 20,
"itemHeight": 140,
"borderWidth": 0
},
"geo": {
"map": "china",
"roam": true,
"aspectScale": 0.75,
"nameProperty": "name",
"selectedMode": false,
"emphasis": {}
}
};
chart_5dcd7985418b4e1ca4889cc6c2dacec5.setOption(option_5dcd7985418b4e1ca4889cc6c2dacec5);
</script>
</body>
</html>