常见统计图:单条折线图、多条折线图、直方图、柱状图、饼状图python
#!/usr/bin/python # -*- coding: utf-8 -*- import numpy as np import matplotlib as mpl from matplotlib import pyplot as plt def init(): mpl.rcParams['font.sans-serif'] = [u'SimHei'] # 正常显示中文(FangSong/黑体 FangSong/KaiTi) mpl.rcParams['axes.unicode_minus'] = False # 正常显示负号 # 单线折线图 def zxt1(x,y): plt.figure(facecolor='w') plt.plot(x, y, 'ro-', label='func1', linewidth=2) plt.legend(loc='upper center') plt.xlabel('X',fontsize=16) plt.ylabel('Y',fontsize=16) plt.scatter(4, 0, color='b') # 标注特殊点 plt.text(4,0,(4,0)) # 标注文本 plt.title('单线折线图',fontsize=18) plt.grid(True) # 显示网格 plt.show() # 多线折线图 def zxt2(x,y1,y2): plt.figure(facecolor='w') plt.plot(x, y1, 'ro-', label='func1', linewidth=2) plt.plot(x, y2, 'g*-', label='func2', linewidth=2) plt.legend(loc='upper center') plt.xlabel('X',fontsize=16) plt.ylabel('Y',fontsize=16) plt.title('多线折线图',fontsize=18) plt.grid(True) # 显示网格 plt.show() # 直方图 def zft(x): plt.figure(facecolor='w') plt.hist(x, bins=10, facecolor='green', align='mid', alpha=0.75) plt.xlabel('X') plt.ylabel('Count') plt.title('直方图') plt.show() # 柱状图 def zzt(x,y): rect = plt.bar(x, y, width=0.5, color="green", align="center") # plt.xticks(x, ('低级','中级','高级','顶级')) # 转换x轴坐标文本 plt.xlabel("X-axis") plt.ylabel("Y-axis") plt.title("柱状图") # plt.grid(True) # 显示网格 plt.legend((rect,), ("图例",)) autolabel(rect) plt.show() # 标注柱形图y值的函数 def autolabel(rects): for rect in rects: height = rect.get_height() plt.text(rect.get_x()+rect.get_width()/2., 1.03*height, "%s" % float(height)) # 饼状图 def bzt(x): labels = '大学', '国企', '外资', '民营' explode = [0, 0.1, 0, 0] # 0.1 凸出这部分, plt.axes(aspect=1) # set this , Figure is round, otherwise it is an ellipse plt.pie(x=x, labels=labels, explode=explode, autopct='%3.1f %%', shadow=True, labeldistance=1.1, startangle=90, pctdistance=0.6) plt.show() # 绘图示例 if __name__ == "__main__": init() x = np.linspace(1,10,20) y = (x-4)**2 y1 = (x-4)**2 y2 = 2*x+3 list = [1,1,1,2,5,6,6,7,7,7,7] fracs = [15, 30.55, 44.44, 10] x3 = [1,2,3,4] y3 = [2,4,6,8] zxt1(x,y) # zxt2(x,y1,y2) # zft(list) # zzt(x3, y3) # bzt(fracs)
-------------完-------------函数