数据可视化

#绘制饼图pie(x,labels,colors,explode,autopct)
#labels---饼图各部分的标签
#colors---饼图各部分的颜色
#explod---需要突出的块状序列
#autopct---饼图占比的显示格式,%.2f--保留两位小数
import numpy
import matplotlib
import matplotlib.pyplot as plt
from pandas import read_csv
df=read_csv('F:/shuju/rz20.csv',sep=',')
df
gb=df.groupby(by=['band'],as_index=False)['num'].agg({'price':numpy.size})
gb
font={'family':'SimHei'}#为了能在图表中显示中文
matplotlib.rc('font',**font)
plt.pie(gb['price'],labels=gb['band'],autopct='%.2f%%')#画饼图  
plt.show()
#散点图
import numpy
import matplotlib
import matplotlib.pyplot as plt
from pandas import read_csv
df=read_csv('F:/shuju/rz20.csv',sep=',')
df
font={'family':'SimHei'}#为了能在图表中显示中文
matplotlib.rc('font',**font)
plt.plot(df['price'],df['num'],'*') # 定义 x,y 用*标记点
plt.xlabel('price') #x轴 表示价格       
plt.ylabel('num')# y轴 表示数字
plt.grid(True) 
plt.show()
#绘制折线图
import matplotlib
import matplotlib.pyplot as plt
from pandas import read_excel
df=read_excel('F:/shuju/rz4.xlsx',sep=',')
df
#提取学号的后三位
def right3(df,a):
    list0=[]
    list1=list(df[a])
    for i in list1:
        i=str(i)
        list2=i[-3:]
        list0.append(list2)
    return list0
df_ri=right3(df,'学号')
df_ri
df_sum=list(df['总分'])#提取总分,并转化成列表
df_sum
def dic3(a,b):#构成学号后三位与总分相对应的list
    t=[]
    for i in range(len(a)):
        d=(a[i],b[i])#组成数对
        t.append(d)
    t.sort()#排序
    return t
df_di=dic3(df_ri,df_sum)
df_di  
list1=[df_di[i][0] for i in range(len(df_di))]
list2=[df_di[i][1] for i in range(len(df_di))]
font={'family':'SimHei'}#为了能在图表中显示中文
matplotlib.rc('font',**font)
plt.plot(list1,list2,'-')
plt.title('学号与总分的折线图')
plt.show()
#绘制柱形图
import matplotlib
import matplotlib.pyplot as plt
from pandas import read_excel
df=read_excel('F:/shuju/rz4.xlsx',sep=',')
gb=df.groupby(by=['学号'])['总分'].agg({'总分':numpy.sum})
gb
index=numpy.arange(gb['总分'].size)
index
font={'family':'SimHei'}#为了能在图表中显示中文
matplotlib.rc('font',**font)
plt.title('竖向柱形图:学号-学分')
plt.bar(index,gb['总分'],1,color='G')
plt.xticks(index+1/2,gb.index,rotation=90)
plt.xticks(index+1/2,gb.index,rotation=90)
plt.show()

 

#绘制直方图
import matplotlib
import matplotlib.pyplot as plt
from pandas import read_excel
font={'family':'SimHei'}#为了能在图表中显示中文
matplotlib.rc('font',**font)
plt.hist(df['总分'],bins=20,cumulative=True)
plt.title('总分直方图')
plt.show()

 

posted on 2018-09-09 22:53  lljaway  阅读(253)  评论(0)    收藏  举报

导航