机器学习之matplotlib绘图


import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d.axes3d import Axes3D  #引入3d

# 定义一个单例模式的装饰器
def singleton(cls):
    #定义实例容器
    instances = {}
    #定义嵌套方法
    def wrapper(*args,**kwargs):
        #判断装饰的类是否是单例
        if cls not in instances:
            instances[cls] = cls(*args,**kwargs)
        return instances[cls]
    return wrapper
	
#建立一个测试类
@singleton
class WorkPlot(object):
    #初始化方法
    def __init__(self,plt):
        self.plt = plt
    # 配置中文字体
    plt.rcParams['font.sans-serif'] = ['SimHei']
   
   #定义柱状图方法
   def test_bar(self):
       plt = self.plt
       #定义数据
       GDP = [12404.1,13908.57,9350,8000]
       plt.bar(['北京','上海','天津','重庆'],GDP,color='steelblue',alpha=0.6)
       plt.ylabel('生产总值')
       plt.title('四大直辖市GDP比较')
       #刻度范围
       plt.ylim([5000,15000])
       plt.show()
       
   #绘制趋势图
   def test_line(self):
       plt = self.plt
       #定义x y轴数据
       x=['5-1','5-2','5-3','5-4']
       y=['27','10','15','23']
       y1=['0','5','6','10']
       plt.plot(x,y,label="wendu")
       plt.plot(x,y1,label="shidu")
       
       plt.legend() #设置图例
       
       plt.show()
       
   #定义散点图
   def sandian(self):
       plt = self.plt
       x = list(range(0,101))
       y = [value * np.random.rand() for value in x]
       plt.scatter(x,y)
       plt.show()
       
   #定义三d散点图
   def test_scatter_3d(self):
       plt = self.plt
       x = np.random.randint(0,10,size=100)
       y = np.random.randint(0,10,size=100)
       z = np.random.randint(0,10,size=100)
       #创建二维对象
       fig = plt.figure()
       #强转
       axes3d = Axes3D(fig)
       
       axes3d.scatter(x,y,z)
       plt.show()
   
   #定义条形图
   def barh(self):
       plt = self.plt
       price = [40,32.8,20,19.6]
       plt.barh(range(4),price,align='center',color='red',alpha=0.5)
       plt.xlabel('价格')
       plt.yticks(range(4),['西游记','水浒传','三国演义','红楼梦'])
       plt.title('四大名著')
       plt.show()
   
   #定义饼图
   def test_pie(self):
       plt = self.plt
       beiJing = [17,18,40,60]
       label = ['2-3年','3-4年','4-5年','5年以上']
       #给饼图自定义颜色
       color = ['red','green','blue','purple']
       #容器
       indic = []  
       for index,item in enumerate(beiJing):
           #判断优先级
           if item == max(beiJing):
               indic.append(0.8) #偏离饼图距离
           elif index == 1:
               indic.append(0.2) 
           else:
               indic.append(0)
       #startangle=90 反转90度    shadow=True 阴影
       plt.pie(beiJing,labels=label,colors=color,startangle=90,shadow=True,explode=tuple(indic),autopct='%1.1ff%%')
       plt.show()
   
   #定义面积图
   def test_area(self):
       plt = self.plt
       #定义日期
       data = ['5-1','5-2','5-3','5-4']
       #定义收入
       earn = [156,356,156,30]
       #定义支出
       zao=[3,16,12,50]
       zhong=[6,20,12,50]
       wan=[0,12,15,50]
       #生成图例
       plt.plot([],[],color='green',label='收入')
       plt.plot([],[],color='red',label='早晨')
       plt.plot([],[],color='orange',label='午饭')
       plt.plot([],[],color='blue',label='晚餐')
       #展示图例
       plt.legend()
       
       plt.stackplot(data,earn,zao,zhong,wan,colors=['green','red','orange','blue'])
       plt.title('四天收入支出面积展示')
       plt.show()
       
if __name__ == "__main__":
	workPlot = WorkPlot(plt)
	a=WorkPlot(plt)
	# id地址的比对 
	print(id(workPlot))
	print(id(a))
	workPlot.test_bar() 
	workPlot.test_pie()
	workPlot.test_area()
	workPlot.test_line()
	workPlot.sandian()
	workPlot.test_scatter_3d()
	workPlot.barh()

效果图如下

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述


导读目录

点击这里

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值