Python数据可视化笔记01--Matplotlib基础
Python数据可视化笔记02--折线图、散点图实战
Python数据可视化笔记03--柱状图、饼状图实战
本文索引:
本文环境:Windows10 + jupyter notebook
一、雷达图
【雷达图】
【雷达图之极坐标系】
【雷达图之polar()函数】
polar(theta,r,**kwargs)
主要参数:
- theta:指极角。
- r:指极径。
import numpy as np
import matplotlib.pyplot as plt
plt.polar(0.25*np.pi,20,'ro',lw=2)
plt.ylim(0,50) # 设置极轴的上下限
plt.show()
这里:
0.25*np.pi = 45°:极角
20:极径
‘ro’:绘极坐标形状为红色圆点
lw = 2:极坐标图形宽度为2
- 如果绘制多个极角和极轴时:
import numpy as np
import matplotlib.pyplot as plt
theta = np.array([0.25,0.5,0.75,1,1.25,1.5,1.75,2])
r = [75,60,50,70,50,85,45,70]
plt.polar(theta*np.pi,r,'ro',lw = 2)
plt.ylim(0,100)
plt.show()
theta:定义了一个ndarray数组存储多个数据
r:定义了一个数组存放极轴的长度,也叫极径
- 则在途中绘制出多个点(0.25*π,75),(0.5*π,60),(0.75*π,50),(1.0*π,70)等。
此时得到图形是这样的:
但是此时曲线并未闭合?
import numpy as np
import matplotlib.pyplot as plt
theta = np.array([0.25,0.5,0.75,1,1.25,1.5,1.75,2,0.25])
r = [75,60,50,70,50,85,45,70,75]
plt.polar(theta*np.pi,r,'ro-',lw = 2)
plt.ylim(0,100)
plt.show()
运行结果:
- fill()函数填充雷达图
import numpy as np
import matplotlib.pyplot as plt
theta = np.array([0.25,0.5,0.75,1,1.25,1.5,1.75,2,0.25])
r = [75,60,50,70,50,85,45,70,75]
plt.polar(theta*np.pi,r,'ro-',lw = 2)
plt.fill(theta*np.pi,r,facecolor='r',alpha=0.25) # 填充
plt.ylim(0,100)
plt.show()
运行结果:
二、三维图
【三维图概述】
from mpl_toolkits.mplot3d import Axes3D
ax = fig.gca(projection = '3d')
ax = plt.subplot(111,projection = '3d')
【 三维曲面绘制方法:p3d.Axes3D.plot_surface() 】
plot_surface(X,Y,Z,*args,**kwargs)
【三维散点图绘制方法:p3d.Axes3D.scatter() 】
p3d.Axes3D.scatter(xs,ys,zs = 0,zdir = 'z',s = 20,c = None,depthshade = True,
*args,**kwargs)
【三维柱状图绘制方法:p3d.Axes3D.bar3d()】
p3d.Axes3D.bar3d(x,y,z,dx,dy,dz,color = None,zsort = 'average',*args,**kwargs)
三维曲线图实战:根据测试数据x、y、z,然后绘制三维曲线,并设置图例字号
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection = '3d')
#测试数据
theta = np.linspace(-4*np.pi,4*np.pi,100)
z = np.linspace(-4,4,100)*0.3
r = z**4 + 1
x = r*np.sin(theta)
y = r*np.cos(theta)
ax.plot(x,y,z,'b^-',label = '3D Picture Test')
mpl.rcParams['legend.fontsize'] = 10
ax.legend()
plt.show()
三维柱状图实战:生成测试数据,绘制三维柱状图,设置每个柱的颜色随机
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d
x = np.random.randint(0,40,10)
y = np.random.randint(0,40,10)
z = 80*abs(np.sin(x + y))
ax = plt.subplot(projection = '3d')
for xx,yy,zz in zip(x,y,z):
color = np.random.random(3)
ax.bar3d(xx,yy,0,dx = 1,dy = 1,dz = zz,color = color)
ax.set_xlabel('X')
ax.set_xlabel('Y')
ax.set_xlabel('Z')
plt.show()
Python数据可视化部分四个小节已全部更新完毕,神知道我是在被隔离期间完成这最后一篇的笔记的,接下来要开始机器学习部分的实战啦,大家一起加油,武汉加油,中国加油!!!