文章目录:
-
一.Matplotlib可视化分析
1.绘制曲线图
2.绘制散点图
3.绘制柱状图
4.绘制饼图
5.绘制3D图形
-
二.Pandas读取文件可视化分析
1.绘制折线对比图
2.绘制柱状图和直方图
3.绘制箱图
-
三.ECharts可视化技术初识
1.ECharts
2.简单示例
-
四.总结
-
https://github.com/eastmountyxz/Python-zero2one
-
[Python从零到壹] 十一.数据分析之Numpy、Pandas、Matplotlib和Sklearn入门知识万字详解
-
[Python从零到壹] 十三.机器学习之聚类算法四万字总结(K-Means、BIRCH、树状聚类、MeanShift)
-
[Python从零到壹] 十七.可视化分析之Matplotlib、Pandas、Echarts入门万字详解
作者新开的“娜璋AI安全之家”将专注于Python和安全技术,主要分享Web渗透、系统安全、人工智能、大数据分析、图像识别、恶意代码检测、CVE复现、威胁情报分析等文章。虽然作者是一名技术小白,但会保证每一篇文章都会很用心地撰写,希望这些基础性文章对你有所帮助,在Python和安全路上与大家一起进步。
一.Matplotlib可视化分析
1.绘制曲线图
# -*- coding: utf-8 -*-
#By:Eastmount CSDN
import numpy as np
import matplotlib.pyplot as plt
X = np.arange(0,4)
print(X)
plt.plot(X, X*0.5, label="y=x*0.5")
plt.plot(X, X*1.5, label="y=x*1.5")
plt.plot(X, X*3.0, label="y=x*3.0")
plt.legend()
plt.show()
# -*- coding: utf-8 -*-
#By:Eastmount CSDN
import numpy as np
import matplotlib.pyplot as plt
X = np.arange(0,4)
print(X)
plt.plot(X, X*0.5, "r-", label="y=x*0.5")
plt.plot(X, X*1.5, "y--", label="y=x*1.5")
plt.plot(X, X*3.0, "g:", label="y=x*3.0")
plt.legend()
plt.show()
# -*- coding: utf-8 -*-
#By:Eastmount CSDN
import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(-np.pi,np.pi,256,endpoint=True)
C = np.cos(X)
S = np.sin(X)
plt.plot(X, C, color="blue", linewidth=2.0, linestyle="-", label="$sin(x)$")
plt.plot(X, S, color="red", linewidth=2.0, linestyle="--", label="$cos(x)$")
plt.legend()
plt.show()
# -*- coding: utf-8 -*-
#By:Eastmount CSDN
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-8 , 8, 1024)
y1 = 0.618*np.abs(x) - 0.8* np.sqrt(64-x**2)
y2 = 0.618*np.abs(x) + 0.8* np.sqrt(64-x**2)
plt.plot(x, y1, color = 'r')
plt.plot(x, y2, color = 'r')
plt.show()
2.绘制散点图
# -*- coding: utf-8 -*-
#By:Eastmount CSDN
import numpy as np
import matplotlib.pyplot as plt
#构造数据
x = np.random.randn(200)
y = np.random.randn(200)
print(x[:10])
print(y[:10])
#绘制散点图
plt.scatter(x, y)
plt.show()
[-0.94086693 -0.92910167 -0.83885859 -0.50927277 2.12230463 0.45695791
-0.59766636 -0.62862962 0.28245908 1.46415206]
[ 0.43828148 0.76547797 1.18670217 0.31996158 0.00350372 1.02620566
3.04573837 -0.59712547 0.45061506 -1.63996253]
# -*- coding: utf-8 -*-
#By:Eastmount CSDN
import numpy as np
import matplotlib.pyplot as plt
#构造数据
x = np.random.randn(200)
y = np.random.randn(200)
print(x[:10])
print(y[:10])
size = 50*np.random.randn(200)
colors = np.random.rand(200)
#绘制散点图
plt.scatter(x, y, s=size, c=colors)
plt.show()
# -*- coding: utf-8 -*-
#By:Eastmount CSDN
import numpy as np
import matplotlib.pyplot as plt
#随机产生90个二维数组
x = np.random.rand(90,2)
print(x)
#numpy中ones()用来构造全一矩阵
label = list(np.ones(40))+list(2*np.ones(30))+list(3*np.ones(20)) #类标label为1、2、3
label = np.array(label)
print(label)
print(type(label))
idx1 = np.where(label == 1)
idx2 = np.where(label == 2)
idx3 = np.where(label == 3)
#绘图 参数:x值、y值、点样式、颜色、类标、粗细
p1 = plt.scatter(x[idx1,0], x[idx1,1], marker = 'x', color = 'r', label='1', s = 40)
p2 = plt.scatter(x[idx2,0], x[idx2,1], marker = '+', color = 'b', label='2', s = 30)
p3 = plt.scatter(x[idx3,0], x[idx3,1], marker = 'o', color = 'c', label='3', s = 20)
plt.legend(loc = 'upper right')
plt.show()
3.绘制柱状图
# -*- coding: utf-8 -*-
#By:Eastmount CSDN
import numpy as np
import matplotlib.pyplot as plt
#随机产生4个整数(0到100之间)
data = np.random.randint(0,100,4)
print(data)
ind = np.arange(4) #四个用户
print(ind)
width = 0.35 #设置宽度
x = ['UserA', 'UserB', 'UserC', 'UserD']
plt.bar(ind, data, width, color='green', label='Data')
plt.xlabel("Username")
plt.ylabel("Consumption")
plt.title("Compare four user monthly consumption data")
plt.legend()
plt.xticks(ind+width/2, x, rotation=40) #旋转40度
plt.show()
# -*- coding: utf-8 -*-
#By:Eastmount CSDN
import matplotlib.pyplot as plt
import numpy as np
num = np.array([1342, 6092, 4237, 8219]) #数量
ratio = np.array([0.75, 0.76, 0.72, 0.75]) #男性占比
men = num * ratio
women = num * (1-ratio)
x = ['学习',u'旅游',u'看剧',u'聊天']
plt.rc('font', family='SimHei', size=13) #中文字体显示
width = 0.5
idx = np.arange(4)
plt.bar(idx, men, width, color='red', label='男性用户')
plt.bar(idx, women, width, bottom=men, color='yellow', label='女性用户')
plt.xticks(idx+width/2, x, rotation=40)
plt.legend()
plt.show()
4.绘制饼图
# -*- coding: utf-8 -*-
#By:Eastmount CSDN
import matplotlib.pyplot as plt
#每一块占得比例,总和为100
mm = [45, 30, 25]
n = mm[0]+mm[1]+mm[2]
a = (mm[0]*1.0*100/n)
b = (mm[1]*1.0*100/n)
c = (mm[2]*1.0*100/n)
fracs = [a, b, c]
print(a, b, c, n)
#离开整体的距离
explode=(0, 0, 0.08)
labels = 'A', 'B', 'C'
#绘制图形
plt.pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90, colors = ("c", "r", "y"))
plt.show()
5.绘制3D图形
import matplotlib.pyplot as plt #绘图用的模块
from mpl_toolkits.mplot3d import Axes3D #绘制3D坐标的函数
fig1=plt.figure() #创建一个绘图对象
ax=Axes3D(fig1) #用这个绘图对象创建一个Axes对象
plt.show() #显示模块中所有绘图对象
# -*- coding: utf-8 -*-
#By:Eastmount CSDN
from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D # 绘制3D坐标的函数
fig = plt.figure() #创建一个绘图对象
ax = Axes3D(fig) #用这个绘图对象创建一个Axes对象
X = np.arange(-2, 2, 0.25) #X轴-2到2之间
Y = np.arange(-2, 2, 0.25) #Y轴-2到2之间
print(Y)
X, Y = np.meshgrid(X, Y) #用两个坐标轴上的点在平面上画格
R = np.sqrt(X**2 + Y**2) #X和Y的平方和开根号
Z = np.sin(R) #计算sin函数赋值为Z坐标
#具体函数方法可用 help(function) 查看,如:help(ax.plot_surface)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')
#给三个坐标轴注明
ax.set_xlabel('x label', color='r')
ax.set_ylabel('y label', color='g')
ax.set_zlabel('z label', color='b')
plt.show()
二.Pandas读取文件可视化分析
year Beijing Shanghai Guiyang Wuhan Changsha
2002 4764 4134 1643 1928 1802
2003 4737 5118 1949 2072 2040
2004 5020.93 5855 1801.68 2516.32 2039.09
2005 6788.09 6842 2168.9 3061.77 2313.73
2006 8279.51 7196 2372.66 3689.64 2644.15
2007 11553.26 8361 2901.63 4664.03 3304.74
2008 12418 8195 3149 4781 3288
2009 13799 12840 3762 5329 3648
2010 17782 14464 4410 5746 4418
2011 16851.95 14603.24 5069.52 7192.9 5862.39
2012 17021.63 14061.37 4846.14 7344.05 6100.87
2013 18553 16420 5025 7717 6292
2014 18833 16787 5608 7951 6116
1.绘制折线对比图
# -*- coding: utf-8 -*-
#By:Eastmount CSDN
import pandas as pd
import matplotlib.pyplot as plt
#读取文件并显示前6行数据 index_col用作行索引的列名
data = pd.read_csv("data.csv",index_col='year')
print(data.shape)
print(data.head(6))
plt.rcParams['font.sans-serif'] = ['simHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号
data.plot()
plt.savefig(u'test.png', dpi=500)
plt.show()
>>>
(13, 5)
Beijing Shanghai Guiyang Wuhan Changsha
year
2002 4764.00 4134.0 1643.00 1928.00 1802.00
2003 4737.00 5118.0 1949.00 2072.00 2040.00
2004 5020.93 5855.0 1801.68 2516.32 2039.09
2005 6788.09 6842.0 2168.90 3061.77 2313.73
2006 8279.51 7196.0 2372.66 3689.64 2644.15
2007 11553.26 8361.0 2901.63 4664.03 3304.74
>>>
# -*- coding: utf-8 -*-
#By:Eastmount CSDN
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv("data.csv",index_col='year')
plt.rcParams['font.sans-serif'] = ['simHei']
plt.rcParams['axes.unicode_minus'] = False
#获取贵阳数据集并绘图
gy = data['Guiyang']
print(gy, type(gy))
gy.plot()
data['Beijing'].plot(color='r')
plt.show()
2.绘制柱状图和直方图
# -*- coding: utf-8 -*-
#By:Eastmount CSDN
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv("data.csv", index_col='year')
plt.rcParams['font.sans-serif'] = ['simHei']
plt.rcParams['axes.unicode_minus'] = False
#在图表中创建子图 4个子图
p1 = plt.subplot(221)
data['Beijing'].plot(color='r', kind='bar')
plt.sca(p1)
p2 = plt.subplot(222)
data['Guiyang'].plot(color='y', kind='barh')
plt.sca(p2)
p3 = plt.subplot(223)
data.Shanghai.plot(kind='line')
plt.sca(p3)
p4 = plt.subplot(224)
data['Changsha'].plot(kind='kde')
plt.sca(p4)
plt.show()
# -*- coding: utf-8 -*-
#By:Eastmount CSDN
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv("data.csv", index_col='year')
data.plot(kind='bar', stacked=True)
plt.show()
data.plot(color='y', kind='barh', subplots=True)plt.show()
# -*- coding: utf-8 -*-
#By:Eastmount CSDN
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = np.random.normal(5.0, 3.0, 1000) # mean=5.0 rms=3.0
pData = pd.DataFrame(data)
print(pData)
print(type(pData))
p1 = pData.hist(histtype='stepfilled',bins=30,normed=True)
plt.show()
3.绘制箱图
# -*- coding: utf-8 -*-
#By:Eastmount CSDN
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv("data.csv",index_col='year')
gy = data['Guiyang']
gy.plot(kind='box')
plt.show()
# -*- coding: utf-8 -*-
#By:Eastmount CSDN
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
data = pd.read_csv("data.csv",index_col='year')
data = data.corr()
sns.heatmap(data)
plt.show()
三.ECharts可视化技术初识
1.ECharts
2.简单示例
<html>
<head>
<meta charset="utf-8">
<!-- 引入 ECharts 文件 -->
<script src="echarts.min.js"></script>
</head>
</html>
<script src="echarts.min.js"></script>
<html>
<head>
<meta charset="utf-8">
<title>ECharts</title>
<!-- 引入 echarts.js -->
<script src="echarts.min.js"></script>
</head>
<body>
<!-- 为echarts设置一个宽度为600、高度为400的区域,用于绘图 -->
<div id="main" style="width: 600px;height:400px;"></div>
<script type="text/javascript">
// 基于准备好的区域,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
var option = {
title: {
text: 'ECharts 入门示例'
},
tooltip: {},
legend: {
data:['数量']
},
xAxis: {
data: ["贵阳市","遵义市","凯里市","六盘水市","都匀市","毕节市"]
},
yAxis: {},
series: [{
name: '数量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
</script>
</body>
</html>
四.总结
简单纪念下,CSDN阅读量即将破千万,全网粉丝近30万。十年啊,近700篇文章,确实可以说一句:这就是我20到30岁的青春,这里既有技术博客,也有娜璋珞一家的故事,我们的爱情史,也见证了一个自幼受贵州大山熏陶的学子慢慢成长,让我认识了许许多多的博友。如图的苏老师,受尽挫折,博士毕业,回到家乡玉林成为了一名大学老师,今天更是自费建成了化学实验室,只想将自己的所学所感传递给他的学生。十年,我在CSDN认识了许多这样的博友、老师和大佬,我们从未谋面,我们天南地北,但相互鼓励,苔花如米小,也学牡丹开。
最后,感谢CSDN,这些年让我骗了很多礼物,更感谢每一位阅读过娜璋故事,每一位给我技术博客点赞的读者。也希望大家记住一个叫Eastmount的分享者,对,不是什么专家,也不是什么大佬,就是一个默默撰写博客的技术分享者,因为爱所以写(今年太忙写得很少很少)。我还将在CSDN写二十年,三十年,一辈子,也将记录我们一家的故事。好想继续抒写我们的故事,但太忙太忙,毕业后再好好写吧。
希望能早日毕业,回到家乡贵州继续当个教书匠,感觉好多要分享的博客,好多要上的课程,好多要开源的代码,好多要学习的知识,期待再次站在讲台前的那一天。继续沉下心去学习,虽菜但勤,感恩遇见,继续加油,晚安娜!
(By:娜璋之家 2022-07-02 夜于武汉)
-
[1] 杨秀璋. 专栏:知识图谱、web数据挖掘及NLP - CSDN博客[EB/OL]. (2016-09-19)[2017-11-07]. http://blog.csdn.net/column/details/eastmount-kgdmnlp.html.
-
[2] matplotlib. Matplotlib官网[EB/OL]. (2002-2017)[2017-11-10]. http://matplotlib.org.
-
[3] scikit-learn. Scikit-Learn官网[EB/OL]. (2017)[2017-11-10].http://scikit-learn.org/ stable/.
-
[4] pandas. Pandas官网[EB/OL]. (2017)[2017-11-10]. http://pandas.pydata.org/.
-
[5] 张良均,王路,谭立云,苏剑林. Python数据分析与挖掘实战[M]. 北京:机械工业出版社,2016.
-
[6] (美)Wes McKinney著. 唐学韬等译. 利用Python进行数据分析[M]. 北京:机械工业出版社,2013.
-
[7] 杨秀璋.[Python数据挖掘课程] 六.Numpy、Pandas和Matplotlib包基础知识[EB/OL] . (2016-11-17)[2017-11-15]. http://blog.csdn.net/eastmount/article/details/53144633.
-
[8] 杨秀璋. [Python数据挖掘课程] 七.PCA降维操作及subplot子图绘制[EB/OL]. (2016-11-26)[2017-11-18]. http://blog.csdn.net/eastmount/article/details/53285192.
-
[9] 杨秀璋. [python数据挖掘课程] 十.Pandas、Matplotlib、PCA绘图实用代码补充[EB/OL]. (2017-03-07)[2017-11-18]. http://blog.csdn.net/eastmount/article/details/60675865
-
[10] powerbaby. 用Pandas作图[EB/OL]. (2016-02-23)[2017-11-18]. http://www.360doc.com/ content/16/0223/21/7249274_536782559.shtml.
-
[11] Seaborn. Seaborn:statistical data visualization[EB/OL]. (2017)[2017-11-18]. http://seaborn.pydata.org/.
-
[12] echarts.Echarts中文官网[EB/OL].(2017)[2017-11-18].http://echarts.baidu.com/index.html.
-
[13] 杨秀璋. [Echarts可视化] 二.php和ajax连接数据库实现动态数据可视化[EB/OL]. (2017-06-02)[2017-11-18]. http://blog.csdn.net/eastmount/article/details/72847067.
原文始发于微信公众号(娜璋AI安全之家):[Python从零到壹] 十七.可视化分析之Matplotlib、Pandas、Echarts入门万字详解
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论