机器学习——matplotlib

pip install matplotlib

matplotlib [mæt'plotlib] 是Python 2D绘图领域的基础套件,它让使用者将数据图形化,并提供多样化的输出格式

 

 

给大家举个例子 用的实例化对象 

 

import matplotlib.pyplot as plt
#导入3d模块
from mpl_toolkits.mplot3d.axes3d import Axes3D
import numpy as np
import pandas as pd

 

 

 

#建立一个测试类

 

class TestPlot(object):
    #初始化方法
    def __init__(self,plt):
        self.plt = plt
    
    
    #配置中文字体
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['font.family'] = 'sans-serif'

 

 

 

#柱状图

    def test_zhu(self):
        plt = self.plt
        #定义数据
        GDP = [12404.1,13908.57,9350,8000]
        #填充数据
        plt.bar(['北京','天津','上海','重庆'],GDP,color="green",alpha=0.2)
        #填写标签
        plt.ylabel('生产总值')
        plt.title('四大直辖市GDP比对')
        #刻度范围
        plt.ylim([5000,15000])
        #绘制
        plt.show()

 

 

#绘制条形图
    def test_tiao(self):
        plt = self.plt
        #定义数据
        price = [40,30,60,50]
        #填充数据
        plt.barh(range(4),price,align='center',color="blue",alpha=0.3)
        #设置标签
        plt.xlabel('价格')
        #编辑商品
        plt.yticks(range(4),['三国','水浒','西游','红楼'])
        #设置标题
        plt.title('商品价格表')
        plt.show()

 

 

 #折线图方法
    def test_line(self):
        plt = self.plt
        #定义X轴数据
        x = ['2019-5-1','2019-5-2','2019-5-3','2019-5-4']
        #定义Y轴数据
        y1 = ['30℃','25℃','15℃','25℃']
        y2 = [30,50,60,40]
        #填充数据
        plt.plot(x,y1,label = '温度')
        plt.plot(x,y2,label = '湿度')
        #设置图例
        plt.legend()
        #绘制
        plt.show()

 

 

#绘制散点图
    def test_sandian(self):
        plt = self.plt
        #定义数据
        x = list(range(0,101))
        y = [value * np.random.rand() for value in x]
        #填充数据
        plt.scatter(x,y,s=30,c = 'red')
        #绘制
        plt.show()

 

 

#定义3D散点
    def test_3d(self):
        plt = self.plt
        #定义数据
        x = np.random.randint(0,20,size=20)
        y = np.random.randint(0,20,size=20)
        z = np.random.randint(0,20,size=20)
#         a = np.random.randint(0,20,size=20)
#         b = np.random.randint(0,20,size=20)
#         c = np.random.randint(0,20,size=20)
#         d = np.random.randint(0,20,size=20)
#         e = np.random.randint(0,20,size=20)
        #创建二维对象
        fig = plt.figure()
        #强转
        axes3d = Axes3D(fig)
        #填充数据
        axes3d.scatter3D(x,y,z)
        #绘制
        plt.show()

 

 

 

#定义饼图方法
    def test_bing(self):
        plt = self.plt
        #定义数据
        beijing = [10,18,40,65]
        #定义标签
        label = ['2-3年','3年','5年','8年以上']
        #颜色
        color = ['red','blue','green','pink']
        #做好容器
        indic = []
        for index,item in enumerate(beijing):
            #判断
            if item == max(beijing):
                indic.append(0.1)
            elif index == 1:
                indic.append(0.1)
            else:
                indic.append(0.1)
                
        #填充数据    autopct="%1.1f%%"以小数的形式展示
        plt.pie(beijing,labels=label,colors=color,startangle=160,shadow=True,explode=tuple(indic),autopct="%1.1f%%")
        
        #标题
        plt.title("3D饼图突出展示工龄占比")
        #绘制
        plt.show()

 

 

#定义面积图方法
    def test_mian(self):
        plt = self.plt
        #定义数据
        date = ['2019-5-1','2019-5-2','2019-5-3','2019-5-4']
        #收入
        earn = [156,356,156,70]
        #定义支出
        pay = [[10,30,5,20],[12,20,10,20],[10,15,30,20],[10,20,30,5]]
        #填充数据
        plt.stackplot(date,earn,pay,colors=['green','pink','orange','blue'])
        #生成图例
        plt.plot([],[],color='red',label="收入")
        plt.plot([],[],color='blue',label="早饭")
        plt.plot([],[],color='green',label="中午饭")
        plt.plot([],[],color='pink',label="晚饭")
        plt.legend()
        plt.title('四天支出收入')
        plt.show()

 

 

 

 

if __name__ == "__main__":

 

    #实例化一个对象
    testplot = TestPlot(plt)
    #调用方法
     #折线
     testplot.test_line()
     #散点
     testplot.test_sandian()
     #3D
     testplot.test_3d()
     #条形
     testplot.test_tiao()
     #柱状图
     testplot.test_zhu()
     #3D饼图
     testplot.test_bing()
    #面积图
    testplot.test_mian()

 

 

 

 

总结:

bar 柱状图 

barh 条形图

plot 折线图

scatter 散点图

scatter3D 3D散点图

pie 3D饼图

stackplot 面积图

 

plt.title('给表的标题')

plt.show() 绘制图表

plt.legend() 绘制图例

 

posted @ 2019-05-04 00:50  帅小博  阅读(337)  评论(0编辑  收藏  举报