利用matplotlib对数据进行可视化展示

绘制饼状图

import matplotlib.pyplot as plt

edu = ['0.2515','0.3724','0.3336','0.0368','0.0057']
labels = ['中专','大专','本科','硕士','其他']
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']#使图像中可以显示中文
explode =[0,0,0,0.1,0]#将硕士块突出,拉离中心0.1
plt.pie(x=edu,explode = explode,labels = labels,autopct='%.2f%%')
plt.show()

在这里插入图片描述

绘制柱状图

1、绘制普通柱状图

import pandas as pd

GDP = pd.read_csv('/Users/eileen/Documents/PYlearn/datas/GDP.txt',sep=' ')
GDP = GDP.drop(labels=['ID','Unnamed: 3'],axis =1).rename(columns = {
   'GDP(亿元)':'GDP','省份':'province'})
GDP.GDP = round(GDP.GDP/10000,1)

plt.style.use('ggplot')#增加绘图风格
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
plt.bar(x=range(GDP.shape[0]),height = GDP.GDP,tick_label = GDP.province, color = 'steelblue')
#tick_label将横坐标替换为中文标签
plt.ylabel('GDP(亿万元)')#添加y轴标签
plt.title('全国前十二GDP排行')#添加图像标题

for x,y in enumerate(GDP.GDP):
   plt.text(x,y+0.1,'%s' %y,ha='center')#为图像添加文本
plt.show()

在这里插入图片描述
2、对数据排序
DataFrame.sort_values(by=‘ ’,axis=0,ascending=True, inplace=False, na_position=‘last’)

参数 说明
by 指定列名(axis=0或’index’)或索引值(axis=1或’columns’)
axis 若axis=0或’index’,则按照指定列中数据大小排序;若axis=1或’columns’,则按照指定索引中数据大小排序,默认axis=0
ascending 是否按指定列的数组升序排列,默认为True,即升序排列
inplace 是否用排序后的数据集替换原来的数据,默认为False,即不替换
na_position {‘first’,‘last’},设定缺失值的显示位置

3、水平柱状图

GDP.sort_values(by= 'GDP',ascending = False,inplace = True)
plt.barh(y=range(GDP.shape[0]), width = GDP.GDP, tick_label = GDP.province, color = 'indianred')
plt.xlabel('GDP(亿万元)')
plt
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值