数据可视化总结

数据可视化总结

1. 配置Python环境变量

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\Scripts
C:\Users\Administrator\AppData\Local\Programs\Python\Python37

2. 下载pyecharts

pip install pyecharts==0.5.11

3. 安装地图文件包
pip install echarts-countries-pypkg 
pip install echarts-china-provinces-pypkg
pip install echarts-china-cities-pypkg
4.python绘图
#subplot划分子区域
import matplotlib.pyplot as plt
plt.subplot(442)
plt.subplot(446)
plt.show()



#subplot划分子区域并显示所有子图
import matplotlib.pyplot as plt
fig=plt.figure()
fig1=fig.add_subplot(3,3,1)
fig2=fig.add_subplot(3,3,2)
fig3=fig.add_subplot(3,3,3)
fig4=fig.add_subplot(3,3,4)
fig5=fig.add_subplot(3,3,5)
fig6=fig.add_subplot(3,3,6)
fig7=fig.add_subplot(3,3,7)
fig8=fig.add_subplot(3,3,8)
fig9=fig.add_subplot(3,3,9)
plt.show()




#用NumPy库和matplotlib库绘制图形
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(10) # 取值依次为0-9的等差数列
y = np.sin(x)
z = np.cos(x)
#w=np.tan(x)
#plt.plot(x,w)
plt.plot(x, y, marker="*", linewidth=3, linestyle="--", color="red")#marker设置数据点样式,linewidth设置线宽,linestyle设置线型样式,color设置颜色
plt.plot(x, z)
plt.title("matplotlib")
plt.xlabel("x")
plt.ylabel("y")
plt.legend(["Y","Z"], loc="upper right")# 设置图例
plt.grid(True)
plt.show()




#用matplotlib库绘制线型图
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties 
font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=20)#导入宋体字体文件
dataX = [1,2,3,4]
dataY = [2,4,4,2]
#封闭线条
#dataX = [1,2,3,4,1]
#dataY = [2,4,4,2,2]
plt.plot(dataX,dataY)
plt.title("绘制直线",FontProperties=font_set);
plt.xlabel("x轴",FontProperties=font_set);
plt.ylabel("y轴",FontProperties=font_set);
plt.show()



#用matplotlib库绘制柱状图
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties 
font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=15)#导入宋体字体文件
x = [0,1,2,3,4,5]
y = [1,2,3,2,4,3]
plt.bar(x,y)#竖的条形图
plt.title("柱状图",FontProperties=font_set); #图标题
plt.xlabel("x轴",FontProperties=font_set);
plt.ylabel("y轴",FontProperties=font_set);
plt.show()



#用matplotlib库和NumPy库绘制随机出现的柱状图
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import numpy as np
font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=15)#导入宋体字体文件
x = np.arange(10)
y = np.random.randint(0,20,10)#20表示柱状图的高度,10表示柱状图的个数
plt.bar(x, y)
plt.show()


#用Matplotlib库绘制直方图
import matplotlib.pyplot as plt
import numpy as np
mean, sigma = 0, 1 #均值、标准差
x = mean + sigma * np.random.randn(10000)
plt.hist(x,50,normed=1,histtype='bar',facecolor='red',alpha=0.75)
plt.show()


#用matplotlib绘制散点图
import matplotlib.pyplot as plt
import numpy as np
x = np.random.rand(100)
y = np.random.rand(100)
plt.scatter(x,y)
plt.show()



#用matplotlib绘制极坐标图
import matplotlib.pyplot as plt
import numpy as np
theta=np.arange(0,2*np.pi,0.02)
ax1 = plt.subplot(121, projection='polar')
ax1.plot(theta,theta/6,'--',lw=2)
plt.show()


#用matplotlib库绘制饼图
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei'] #设置字体
plt.title("饼图");#设置标题
labels = '计算机系','机械系','管理系','社科系'
sizes = [45,30,15,10] #设置每部分大小
explode = (0,0.0,0,0) #设置每部分凹凸
counterclock = False#设置顺时针方向
plt.pie(sizes,explode=explode,labels=labels,autopct='%1.1f%%',shadow=False,startangle=90) #设置饼图的起始位置,startangle=90表示开始角度为90度
plt.show()

5.pyecharts绘图
#绘制柱状图
from pyecharts import Bar
v1 = [70,85,95,64]
str1 = ['数学','物理','化学','英语']
bar1 = Bar('柱形图','分数')
bar1.add('成绩',str1,v1,is_more_utils = False)#is_more_utils 提供更多工具按钮
bar1.render("分数柱形图.html")  
#绘制柱状图
#导入柱状图-Bar
from pyecharts import Bar
#设置行名
columns = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
#设置数据
data1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
data2 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
#设置柱状图的主标题与副标题
bar = Bar("柱状图", "一年的降水量与蒸发量")
#添加柱状图的数据及配置项
bar.add("降水量", columns, data1, mark_line=["average"], mark_point=["max", "min"])
bar.add("蒸发量", columns, data2, mark_line=["average"], mark_point=["max", "min"])
#生成本地文件(默认为.html文件)
bar.render("全年降水量和蒸发量柱状图.html")






from pyecharts import Line
line = Line("折线图","一年的降水量与蒸发量")
#is_label_show是设置上方数据是否显示
line.add("降水量", columns, data1, is_label_show=True)
line.add("蒸发量", columns, data2, is_label_show=True)
line.render("全年降水量和蒸发量折线图.html")





from pyecharts import Line
line = Line("面积图","一年的降水量与蒸发量")
#is_label_show是设置上方数据是否显示
line.add("降水量", columns, data1, is_label_show=True,is_full=True,area_opacity=0.5,is_smooth=True)
line.add("蒸发量", columns, data2, is_label_show=True,is_full=True,area_opacity=0.5)
line.render("全年降水量和蒸发量面积图.html")



from pyecharts import Scatter
scatter = Scatter("散点图", "一年的降水量与蒸发量")
#axis_name是设置横坐标名称,这里由于显示问题,还需要将y轴名称与y轴的距离进行设置
scatter.add("降水量与蒸发量的散点分布", data1,data2,xaxis_name="降水量",yaxis_name="蒸发量",
            yaxis_name_gap=40)
scatter.render("全年降水量和蒸发量散点图.html")



from pyecharts import Overlap
overlap = Overlap()
bar = Bar("柱状图-折线图合并", "一年的降水量与蒸发量")
bar.add("降水量", columns, data1, mark_point=["max", "min"])
bar.add("蒸发量", columns, data2, mark_point=["max", "min"])
overlap.add(bar)
overlap.add(line)
overlap.render("全年降水量和蒸发量双图合并.html")





from pyecharts import Grid
#设置折线图标题位置
line = Line("折线图","一年的降水量与蒸发量",title_top="45%")
line.add("降水量", columns, data1, is_label_show=True)
line.add("蒸发量", columns, data2, is_label_show=True)
grid = Grid()
#设置两个图表的相对位置
grid.add(bar, grid_left="60%")#grid_bottom
grid.add(line, grid_right="60%")#grid_top
grid.render("全年降水量和蒸发量双图.html")




5.世界新冠状病毒疫情地图
## https://zhuanlan.zhihu.com/p/122971494
## 获取全部数据:https://coronavirus-tracker-api.herokuapp.com/all
##
## 获取确诊数据:https://coronavirus-tracker-api.herokuapp.com/confirmed
##
## 获取死亡数据:https://coronavirus-tracker-api.herokuapp.com/deaths
##
## 获取治愈数据:https://coronavirus-tracker-api.herokuapp.com

##import requests
import json
from pyecharts import Map, Geo

##res=requests.get(url="https://coronavirus-tracker-api.herokuapp.com/confirmed")
##fq=open("info_confirmed.txt","w")
##fq.write(res.text)

fq=open("info_confirmed.txt","r")
json_text=json.loads(fq.read())#读取文件
fq.close()
date="12/20/20"
'''
location=json_text["locations"][0]["country"]
data=json_text["locations"][0]["history"]["3/12/20"]
print(str(data[0])+"   "+location)
'''
value=[]
attr=[]
ussum=0
chinasum=0
Canadasum=0
UKsum=0
Australiasum=0
Francesum=0

for i in range(0,len(json_text["locations"])):
 if  json_text["locations"][i]["country"]=="US":#判断美国数据
        ussum=ussum+int(json_text["locations"][i]["history"][date])
 elif json_text["locations"][i]["country"]=="China":#判断中国数据
        #attr.append(json_text["confirmed"]["locations"][i]["province"])
        chinasum=chinasum+int(json_text["locations"][i]["history"][date])
 elif json_text["locations"][i]["country"]=="Canada":#判断加拿大数据
        Canadasum=Canadasum+int(json_text["locations"][i]["history"][date])
 elif json_text["locations"][i]["country"]=="United Kingdom":#判断英国数据
        UKsum=UKsum+int(json_text["locations"][i]["history"][date])
 elif json_text["locations"][i]["country"]=="Australia":#判断澳大利亚数据
        Australiasum=Australiasum+int(json_text["locations"][i]["history"][date])        
 elif json_text["locations"][i]["country"]=="France":#判断法国数据
        Francesum=Francesum+int(json_text["locations"][i]["history"][date])       
 else:
        attr.append(json_text["locations"][i]["country"])
        value.append(json_text["locations"][i]["history"][date])
attr.append("China")
value.append(int(chinasum))
attr.append("United States")
value.append(int(ussum))
attr.append("Canada")
value.append(int(Canadasum))
attr.append("United Kingdom")
value.append(int(UKsum))
attr.append("Australia")
value.append(int(Australiasum))
attr.append("France")
value.append(int(Francesum))
print(value)
print(attr)
map0 = Map("世界地图示例", width=1200, height=600)
map0.add("世界新冠状病毒疫情地图", attr,value, maptype="world",type='heatmap',is_visualmap=True,
         visual_text_color='#000', visual_range=[min(value), max(value)], is_map_symbol_show=False,
         label_text_size=0.5)
map0.render(path="世界新冠状病毒疫情地图.html")

#,is_label_show=True, 展示国家名称
5.中国新冠状病毒疫情地图
import json
from pyecharts import Map, Geo

fq=open("info_confirmed.txt","r")
json_text=json.loads(fq.read())#读取文件
fq.close()

value=[]
attr=[]
date="3/25/20"
for i in range(0,len(json_text["locations"])):
 if json_text["locations"][i]["country"]=="China":#判断中国数据        
        attr.append(json_text["locations"][i]["province"])
        value.append(json_text["locations"][i]["history"][date])

A={'Anhui':"安徽",'Beijing':"北京",'Chongqing':"重庆",'Fujian':"福建", 'Gansu':"甘肃", 'Guangdong':'广东', 'Guangxi':"广西", 'Guizhou':"贵州", 'Hainan':"海南",
   'Hebei':'河北', 'Heilongjiang':"黑龙江", 'Henan':"河南", 'Hong Kong':"香港", 'Hubei':"湖北", 'Hunan':"湖南", 'Inner Mongolia':"内蒙古", 'Jiangsu':"江苏", 'Jiangxi':"江西",
   'Jilin':"吉林", 'Liaoning':"辽宁", 'Macau':"澳门", 'Ningxia':"宁夏", 'Qinghai':"青海", 'Shaanxi':"陕西", 'Shandong':"山东", 'Shanghai':"上海", 'Shanxi':"山西", 'Sichuan':"四川",
   'Tianjin':"天津", 'Tibet':"西藏", 'Unknown':"台湾", 'Xinjiang':"新疆", 'Yunnan':"云南", 'Zhejiang':"浙江"}
attr= [A[attr[i]] for i in range(len(attr))]
print(value)
print(attr)
map0 = Map("中国地图示例", width=1200, height=600)
map0.add("中国新冠状病毒疫情地图", attr,value, maptype="china",type='heatmap',is_visualmap=True,
         visual_text_color='#000', visual_range=[min(value), max(value)],)
map0.render(path="中国新冠状病毒疫情地图.html")

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值