为了让人更加清晰深入的了解数据,在数据分析中经常会涉及到数据可视化操作,之前就是用到了随便网上找找,并没有形成一个完整的概念,现在利用少有的时间整理一下数据可视化常用的工具。
箱型图
data = np.random.normal(size=(100, 4), loc=0.0, scale=1.0)
labels = ['A','B','C','D']
plt.boxplot(data, labels=labels)
plt.show()
能力分布图
plt.style.use('ggplot')
#font = FontProperties(fname='', size=20)
ability_size = 6
ability_label = ['Attack', 'Defend', 'Dribbling', 'Speed', 'Power', 'Shooting']
ax1 = plt.subplot(221, projection='polar')
ax2 = plt.subplot(222, projection='polar')
ax3 = plt.subplot(223, projection='polar')
ax4 = plt.subplot(224, projection='polar')
player = {
'M': np.random.randint(size=ability_size, low=60, high=99),
'H': np.random.randint(size=ability_size, low=60, high=99),
'P': np.random.randint(size=ability_size, low=60, high=99),
'Q': np.random.randint(size=ability_size, low=60, high=99)
}
theta = np.linspace(0, 2*np.pi, 6, endpoint=False)
theta = np.append(theta, theta[0])
player['M'] = np.append(player['M'], player['M'][0])
ax1.plot(theta, player['M'], color='r')
ax1.fill(theta, player['M'], color='r', alpha=0.3)
ax1.set_xticks(theta)
ax1.set_yticks([20, 40, 60, 80, 100])
ax1.set_xticklabels(ability_label)
ax1.set_title('Player1', position=(0.5, 1), color='r')
player['H'] = np.append(player['H'], player['H'][0])
ax2.plot(theta, player['H'], color='g')
ax2.fill(theta, player['H'], color='g', alpha=0.3)
ax2.set_xticks(theta)
ax2.set_yticks([20, 40, 60, 80, 100])
ax2.set_xticklabels(ability_label)
ax2.set_title('Player2', position=(0.5, 1), color='g')
player['P'] = np.append(player['P'], player['P'][0])
ax3.plot(theta, player['P'], color='b')
ax3.fill(theta, player['P'], color='b', alpha=0.3)
ax3.set_xticks(theta)
ax3.set_yticks([20, 40, 60, 80, 100])
ax3.set_xticklabels(ability_label)
ax3.set_title('Player3', position=(0.5, 1), color='b')
player['Q'] = np.append(player['Q'], player['Q'][0])
ax4.plot(theta, player['Q'], color='y')
ax4.fill(theta, player['Q'], color='y', alpha=0.3)
ax4.set_xticks(theta)
ax4.set_yticks([20, 40, 60, 80, 100])
ax4.set_xticklabels(ability_label)
ax4.set_title('Player4', position=(0.5, 1), color='y')
plt.show()
饼状图
labels = 'A','B','C','D'
fracs = [15,30,45,10]
explode = [0,0,0.05,0]
plt.pie(x = fracs,labels = labels,autopct='%.0f%%',explode=explode,shadow=True)
plt.show()
散点图
N = 1000
x = np.random.randn(N)
y2 = x + np.random.randn(N) * 0.5
plt.scatter(x,y2)
plt.show()
直方图
N=5
y=[20,10,30,25,15]
index = np.arange(N)
p1 = plt.bar(x=index, height=y,width=0.5,bottom=100,color='red')
plt.show()
子图分布
x=np.arange(1,100)
plt.subplot(221) #总行数,总列数,子图所在位置
plt.plot(x,x)
plt.subplot(222)
plt.plot(x,-x)
plt.subplot(223)
plt.plot(x,x*x)
plt.subplot(224)
plt.plot(x,np.log(x))
plt.show()
区域填充
x = np.linspace(0,5*np.pi,1000)
y1 = np.sin(x)
y2 = np.sin(2*x)
#plt.plot(x,y1)
#plt.plot(x,y2)
plt.fill(x,y1,'b',alpha=0.3)
plt.fill(x,y2,'r',alpha=0.3)
fig = plt.figure()
ax = plt.gca()
ax.plot(x,y1,color='red')
ax.plot(x,y2,color='blue')
#由于x的取值离散的,所以需要用到interpolate进行连续的填充
ax.fill_between(x,y1,y2,where=y1>y2,facecolors='yellow',interpolate=True)
ax.fill_between(x,y1,y2,where=y1<y2,facecolors='green',interpolate=True)
plt.show()
极坐标
r=np.arange(1, 6, 1)
theta =[0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi]
ax=plt.subplot(111, projection ='polar')
ax.plot(theta, r, color ='r', linewidth=3)
ax.grid(True)
plt.show()