python深度解析matplotlib-绘制各种基础图形

下载matplotlib库
在这里插入图片描述

#导包
import matplotlib.pyplot as plt

#定义一个绘图类
class TestPlot(object):

#定义初始化方法
def __init__(self,plt):
    self.plt = plt


#定义内部属性
#解决中文乱码问题(第二种,第一种在d6中)
# 'font.sans-serif' 是matplotlib的内置key
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['font.family']='sans-serif'
#指定编码
plt.rcParams['axes.unicode_minus'] = False

#定义柱状图
def my_bar(self):
    my_plt = self.plt

    #定义数据
    GDP = [12404.1,13908.57,9293.98,9002.2]
    #传入数据  alpha 透明度
    my_plt.bar(['北京','上海','深圳','重庆'],GDP,align='center',color='steelblue',alpha=0.8)
    #添加标签
    my_plt.ylabel('生产总值')
    #添加标题
    my_plt.title('四个直辖市GDP大比拼')
    #刻度范围
    my_plt.ylim([6000,16000])
    #绘制
    my_plt.show()

在这里插入图片描述

#定义饼图方法
def my_pie(self):
    my_plt = self.plt
    #定义数据
    beijing = [17,17,23,43]
    #定义标签
    label = ['2-3年','3-4年','4-5年','五年以上']
    color = ['red','green','steelblue','purple']
    
    #将数值最大的突出展示
    indic = []
    # for item in beijing:
    #     if item == max(beijing):
    #         indic.append(0.1)
    #     else:
    #         indic.append(0)
         
    #使用enumerate方法来添加索引  for item in range(len(beijing))
    for index,item in enumerate(beijing):
        if item == max(beijing):
            indic.append(0.15)
        elif index == 1:
            indic.append(0.07)
        else:
            indic.append(0)



    #将数据传入
    my_plt.pie(
        #数据
        beijing,
        #标签
        labels = label,
        #颜色
        colors=color,
        #角度
        startangle=90,
        #阴影
        shadow=True,
        #突出显示
        explode=tuple(indic),
        #格式化数字
        autopct='%1.1f%%'

    )

    #设置标题
    my_plt.title('饼图示例-统计北京程序员的工龄占比')
    #绘制
    my_plt.show()

在这里插入图片描述

#定义面积图方法
def my_area(self):
    #定义日期区间
    date = ['2019-03-10','2019-04-10','2019-01-10','2019-07-10','2019-12-10']
    #定义数据
    #收入
    earn = [156, 237,189,444,111]
    #支出
    pay = [[15,30,20,22,26],[10,14,34,64,20]]
    #将数据传入方法
    self.plt.stackplot(date,earn,pay,colors=['green','yellow','orange'])
    #生成图裂
    self.plt.plot([],[],color='green',label='收入')
    self.plt.plot([],[],color='yellow',label='午餐')
    self.plt.plot([],[],color='orange',label='晚餐')

    #设置标题
    self.plt.title('面积图样例 - 统计五天收入支出')
    #显示图例
    self.plt.legend()
    #绘制
    self.plt.show()

在这里插入图片描述

#定义Contour 环形图
    def my_t(self):
        x,y = np.ogrid[-1:1:5j,-1:1:5j]
        f = x**2+y**2
        plt.contour(f,colors=list('gbr'))
        plt.xlabel('X')
        plt.xlabel('Y')
        plt.title('plt test')
        plt.legend()
        plt.show()

在这里插入图片描述

#程序入口调用方法
if __name__ == "__main__":
    #实例化一个对象
    testplot = TestPlot(plt)
    #面积图方法
    # testplot.my_area()
    #柱状图方法
    # testplot.my_bar()
    #饼图方法
    testplot.my_pie()
	#Contour 环形图方法
	#testplot.my_t()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值