关于matplotlib模块常用的可视化功能模块整理

为了让人更加清晰深入的了解数据,在数据分析中经常会涉及到数据可视化操作,之前就是用到了随便网上找找,并没有形成一个完整的概念,现在利用少有的时间整理一下数据可视化常用的工具。

箱型图

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()

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值