zoukankan      html  css  js  c++  java
  • 人工智能-画图形(2)

    Matplotlib Python 的绘图库。 它可与 NumPy 一起使用,提供了一种有效的 MatLab 开源替代方案。 它也可以和图形工具包一起使用,如 PyQt wxPython

    Windows 系统安装 Matplotlib

    进入到 cmd 窗口下,执行以下命令:

    python -m pip install -U pip setuptools python -m pip install matplotlib

    Linux 系统安装 Matplotlib

    可以使用 Linux 包管理器来安装:

    • Debian / Ubuntu

    sudo apt-get install python-matplotlib

    • Fedora / Redhat

    sudo yum install python-matplotlib

    Mac OSX 系统安装 Matplotlib

    Mac OSX 可以使用 pip 命令来安装:

    sudo python -mpip install matplotlib

    安装完后,你可以使用 python -m pip list 命令来查看是否安装了 matplotlib 模块。

    $ python -m pip list | grep matplotlib matplotlib (1.3.1)

    今天要使用类方法画柱状图,饼图,面积图。
     
     
     
    # 导包
    import matplotlib.pyplot as plt



    # 定义一个绘图类
    class TestPlot(object):
      # 初始化方法
      def __init__(self,plt):
        self.plt = plt
      # 定义内部属性
      # 解决中文乱码问题[第二套]
      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.5,9386.5,9143.6]
        #传入数据
        my_plt.bar(['北京','上海','深圳','重庆'],GDP,align='center',color='steelblue',alpha=0.8)
        #添加标签
        my_plt.ylabel('生产总值')
        #添加标题
        my_plt.title('四个直辖市GDP大比拼')
        #刻度范围
        my_plt.ylim([5000,16000])
        #绘制
        my_plt.show()
     



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



        # for item in beijing:
          # if item == max(beijing):
            # indic.append(0.1)
          # else:
            # indic.append(0)
            # 将数据传入
            my_plt.pie(
                # 数据
                beijing,
                # 标签
                labels = label,
                # 颜色
                colors=color,
                # 定义3D图角度
                startangle=90,
                # 阴影
                shadow=True,
                # 突出显示
                explode=tuple(indic),
                # 格式化数子
                autopct='%1.1f%%',
     
            )
          # 设置标题
          my_plt.title('并图绘制-统计北京程序员的工龄占比')
          my_plt.show()
     
     
     
     
     
     
     
     



      # 定义面积图方法
      def my_area(self):
        # 定义日期
        date = ['2019-03-07','2019-03-08','2019-03-09','2019-03-010','2019-03-011']
        # 定义数据
        # 收入
        earn = [156,234,324,354,456]
        # 支出
        pay = [[15,30,27,43,20],[10,20,25,132,28]]
        # 将数据传入方法
        my_lit = self.plt
        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('面积图样例-统计5天收入支出')
        # 显示图例
        self.plt.legend()
        # 绘制图形
        self.plt.show()
    if __name__ == "__main__":
      # 实例化对象
      testplot = TestPlot(plt)
      testplot.my_bar()
      # testplot.my_area()
      # testplot.my_pie()
      print(123)
     
    总结:
     
      解决中文乱码的两种方法
      第一种:

        #导入字体库

        from matplotlib.font_manager import FontProperties

        #设置本机字体

        font = FontProperties(fname="C:/Windows/Fonts/simkai.ttf",size=15)

         使用时直接调用:

        FontProperties=font

     
    第二种:
      # 定义内部属性
      # 解决中文乱码问题[第二种]
      plt.rcParams['font.sans-serif'] = ['SimHei']
      plt.rcParams['font.family']='sans-serif'
      #指定编码
      plt.rcParams['axes.unicode_minus'] = False
  • 相关阅读:
    MyEclipse配置Tomcat 并编写第一个JSP程序
    ubuntu安装postgresql与postgis
    ubuntu12.10升级至14.04
    ubuntu 12.10无法用apt-get安装软件 Err http://us.archive.ubuntu.com quantal-updates/main Sources 404 Not
    hive0.13网络接口安装
    hive报错 Another instance of Derby may have already booted the database
    前端开发者进阶之函数柯里化Currying
    js中的事件委托
    while 和 for 对比
    小图标文字对齐的终极解决方案
  • 原文地址:https://www.cnblogs.com/chengdongzi/p/10494801.html
Copyright © 2011-2022 走看看