zoukankan      html  css  js  c++  java
  • Data Algorithm learn of Matplotlib

     Author: 楚格

    2018-11-30   08:16:30

    IDE: Pycharm2018.03   Anaconda 3.5.1 Python 3.7   

    KeyWord :  Pandas

    Explain:   更新中

    ---------------------------------------------------------------------------

    --------

      1 # -*- coding: utf-8 -*-
      2 #---------------------------------
      3 '''
      4 # Author  : chu ge 
      5 # Function:
      6 '''
      7 #---------------------------------
      8 '''
      9  --------------------------------
     10 导入模块 
     11 1.系统库
     12 2.第三方库
     13 3.相关定义库
     14 --------------------------------
     15 '''
     16 # 1.系统库
     17 import sys
     18 import os
     19 #2.第三方库
     20 import numpy as np
     21 import matplotlib.pyplot as plt
     22 import math
     23 from mpl_toolkits.mplot3d import Axes3D
     24 import matplotlib.gridspec as gridspec
     25 from matplotlib import animation
     26 '''
     27 ============================================================================
     28 》》》》》》》》》》》》》》》》》》》》》》》》
     29 
     30 Matplotlib
     31 
     32 ----------------------------------------------
     33 简介:
     34     Matplotlib是一个Python 2D绘图库,它可以在各种平台上以各种硬拷贝格式和交互式环境生成出具有出版品质的图形。 
     35     Matplotlib可用于Python脚本,Python和IPython shell,Jupyter笔记本,Web应用程序服务器和四个图形用户界面工具包.
     36     Matplotlib试图让简单的事情变得更简单,让无法实现的事情变得可能实现。 只需几行代码即可生成绘图,直方图,功率谱,
     37     条形图,错误图,散点图等。 有关示例,请参阅示例图和缩略图库。
     38     为了简单绘图,pyplot模块提供了类似于MATLAB的界面,特别是与IPython结合使用时。 
     39 对于高级用户,您可以通过面向对象的界面或MATLAB用户熟悉的一组函数完全控制线条样式,字体属性,轴属性等。
     40 
     41 
     42 ----------------------------------------------
     43 ============================================================================
     44 '''
     45 
     46 '''
     47 # ============================================================================
     48 # Function:  
     49 # Explain :  输入参数   
     50 #         :  输出参数  
     51 # ============================================================================
     52 '''
     53 
     54 # ============================================================================
     55 '''
     56 # ============================================================================
     57 #   测试专用
     58 # ============================================================================
     59 '''
     60 if __name__ == "__main__":
     61     print("123")
     62 
     63     # # name:   根据坐标点绘制
     64     try:
     65         # print("根据坐标点绘制")
     66         # var_x = np.array([1, 3, 5, 7, 9, 11, 13, 15])
     67         # var_y = np.array([1, 1, 1, 1, 1, 1, 1, 1])
     68         # # help(plt.plot)
     69         # plt.plot(var_x, var_y, 'r')         # 折线 1 x 2 y 3 color
     70         # plt.plot(var_x, var_y, 'g', lw=10)  # 4 line w
     71         # # 折线 饼状 柱状
     72         # var_x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
     73         # var_y = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1])
     74         # plt.bar(var_x, var_y, 0.2, alpha=1, color='b')  # 5 color 4 透明度 3 0.9
     75         # plt.show()
     76 
     77         # print("传入参数是numpy数组时的效果")
     78         # for var in range(0, 15):              # 1 柱状图
     79         #     dateOne = np.zeros([2])
     80         #     dateOne[0] = var
     81         #     dateOne[1] = var
     82         #     y = np.zeros([2])
     83         #     y[0] = 10
     84         #     y[1] = 20
     85         #     plt.plot(dateOne, y, 'r', lw=8)
     86         # plt.show()
     87 
     88         # # 从-1-----1之间等间隔采66个数.也就是说所画出来的图形是66个点连接得来的
     89         # # 注意:如果点数过小的话会导致画出来二次函数图像不平滑
     90         # x = np.linspace(-10, 10, 1000)
     91         # # 绘制y=2x+1函数的图像
     92         # y =  (x**3 )**5 + 1
     93         # plt.plot(x, y)
     94         # plt.show()
     95         # # # 绘制x^2函数的图像
     96         # y = x ** 3
     97         # plt.plot(x, y)
     98         # plt.show()
     99 
    100         # x = np.linspace(-10, 10, 10000)
    101         # # figure 1
    102         # y1 = 2 * x - x**2
    103         # plt.figure(num=5, figsize=(5, 5))
    104         # plt.plot(x, y1)
    105         # # figure 2
    106         # y2 = x ** 2
    107         # plt.figure(num=4, figsize=(4, 4))
    108         # plt.plot(x, y2)
    109         # # figure 3,指定figure的编号并指定figure的大小, 指定线的颜色, 宽度和类型
    110         # y2 = x ** 3 + x * 2
    111         # plt.figure(num=3, figsize=(3, 3))
    112         # plt.plot(x, y1)                   # 一个坐标轴上画了两个图形
    113         # plt.plot(x, y2, color='red', linewidth=1.0, linestyle='--')
    114         # plt.show()
    115 
    116         # # 绘制普通图像
    117         # x = np.linspace(-10, 10, 1000)
    118         # y1 = 2 * x + 1
    119         # y2 = x ** 2
    120         # plt.figure()
    121         # plt.plot(x, y1)
    122         # plt.plot(x, y2, color='red', linewidth=1.0, linestyle='--')
    123         # plt.xlim((-10, 10))          # 设置坐标轴的取值范围
    124         # plt.ylim((-10, 10))
    125         # # 设置坐标轴的lable,
    126         # plt.xlabel(u'这是x轴', fontproperties='SimHei', fontsize=14)   # 标签里面必须添加字体变量,不然可能会乱码
    127         # plt.ylabel(u'这是y轴', fontproperties='SimHei', fontsize=14)   # fontproperties='SimHei',fontsize=14。
    128         # # 设置x坐标轴刻度, 之前为0.25, 修改后为0.5
    129         # # 也就是在坐标轴上取5个点,x轴的范围为-1到1所以取5个点之后刻度就变为0.5了
    130         # plt.xticks(np.linspace(-10, 10, 1))
    131         # # 获取当前的坐标轴, gca = get current axis
    132         # ax = plt.gca()
    133         # # 设置右边框和上边框
    134         # ax.spines['right'].set_color('none')
    135         # ax.spines['top'].set_color('none')
    136         # # 设置x坐标轴为下边框
    137         # ax.xaxis.set_ticks_position('bottom')
    138         # # 设置y坐标轴为左边框
    139         # ax.yaxis.set_ticks_position('left')
    140         # # 设置x轴, y周在(0, 0)的位置
    141         # ax.spines['bottom'].set_position(('data', 0))
    142         # ax.spines['left'].set_position(('data', 0))
    143         # for label in ax.get_xticklabels() + ax.get_yticklabels():
    144         #     label.set_fontsize(10)
    145         #     label.set_bbox(dict(facecolor='green', edgecolor='None', alpha=0.7))
    146         # plt.show()
    147 
    148         # # 绘制普通图像
    149         # x = np.linspace(-1, 1, 50)
    150         # y1 = 2 * x + 1
    151         # y2 = x ** 2
    152         # plt.figure()
    153         # plt.plot(x, y1)
    154         # plt.plot(x, y2, color='red', linewidth=1.0, linestyle='--')
    155         # # 设置坐标轴的取值范围
    156         # plt.xlim((-1, 1))
    157         # plt.ylim((0, 3))
    158         # # 设置坐标轴的lable
    159         # # 标签里面必须添加字体变量:fontproperties='SimHei',fontsize=14。不然可能会乱码
    160         # plt.xlabel(u'这是x轴', fontproperties='SimHei', fontsize=14)
    161         # plt.ylabel(u'这是y轴', fontproperties='SimHei', fontsize=14)
    162         # # 设置x坐标轴刻度, 之前为0.25, 修改后为0.5
    163         # # 也就是在坐标轴上取5个点,x轴的范围为-1到1所以取5个点之后刻度就变为0.5了
    164         # plt.xticks(np.linspace(-1, 1, 5))
    165         # # 获取当前的坐标轴, gca = get current axis
    166         # ax = plt.gca()
    167         # # 设置右边框和上边框
    168         # ax.spines['right'].set_color('none')
    169         # ax.spines['top'].set_color('none')
    170         # # 设置x坐标轴为下边框
    171         # ax.xaxis.set_ticks_position('bottom')
    172         # # 设置y坐标轴为左边框
    173         # ax.yaxis.set_ticks_position('left')
    174         # # 设置x轴, y周在(0, 0)的位置
    175         # ax.spines['bottom'].set_position(('data', 0))
    176         # ax.spines['left'].set_position(('data', 0))
    177         # plt.show()
    178 
    179         # # 绘制普通图像
    180         # x = np.linspace(-3, 3, 50)
    181         # y = 2 * x + 1
    182         # plt.figure()
    183         # plt.plot(x, y)
    184         # # 将上、右边框去掉
    185         # ax = plt.gca()
    186         # ax.spines['right'].set_color('none')
    187         # ax.spines['top'].set_color('none')
    188         # # 设置x轴的位置及数据在坐标轴上的位置
    189         # ax.xaxis.set_ticks_position('bottom')
    190         # ax.spines['bottom'].set_position(('data', 0))
    191         # # 设置y轴的位置及数据在坐标轴上的位置
    192         # ax.yaxis.set_ticks_position('left')
    193         # ax.spines['left'].set_position(('data', 0))
    194         # # 定义(x0, y0)点
    195         # x0 = 1
    196         # y0 = 2 * x0 + 1
    197         # # 绘制(x0, y0)点
    198         # plt.scatter(x0, y0, s=50, color='blue')
    199         # # 绘制虚线
    200         # plt.plot([x0, x0], [y0, 0], 'k--', lw=2.5)
    201         # # 绘制注解一
    202         # plt.annotate(r'$2 * x + 1 = %s$' % y0, xy=(x0, y0), xycoords='data', xytext=(+30, -30), 
    203         #              textcoords='offset points', fontsize=16,
    204         #              arrowprops=dict(arrowstyle='->', connectionstyle='arc3, rad = .2'))
    205         # # 绘制注解二
    206         # plt.text(-3, 3, r'$Test text. mu sigma_i, alpha_i$', fontdict={'size': 16, 'color': 'red'})
    207         # plt.show()
    208 
    209         # # 数据个数
    210         # n = 1024
    211         # # 均值为0, 方差为1的随机数
    212         # x = np.random.normal(0, 1, n)
    213         # y = np.random.normal(0, 1, n)
    214         # # 计算颜色值
    215         # color = np.arctan2(y, x)
    216         # # 绘制散点图
    217         # plt.scatter(x, y, s=75, c=color, alpha=0.5)
    218         # # 设置坐标轴范围
    219         # plt.xlim((-1.5, 1.5))
    220         # plt.ylim((-1.5, 1.5))
    221         # # 不显示坐标轴的值
    222         # plt.xticks(())
    223         # plt.yticks(())
    224         # plt.show()
    225 
    226         # # 数据数目
    227         # n = 10
    228         # x = np.arange(n)
    229         # # 生成数据, 均匀分布(0.5, 1.0)之间
    230         # y1 = (1 - x / float(n)) * np.random.uniform(0.5, 1.0, n)
    231         # y2 = (1 - x / float(n)) * np.random.uniform(0.5, 1.0, n)
    232         # # 绘制柱状图, 向上
    233         # plt.bar(x, y1, facecolor='blue', edgecolor='white')
    234         # # 绘制柱状图, 向下
    235         # plt.bar(x, -y2, facecolor='green', edgecolor='white')
    236         # temp = zip(x, y2)
    237         # # 在柱状图上显示具体数值, ha水平对齐, va垂直对齐
    238         # for x, y in zip(x, y1):
    239         #     plt.text(x + 0.05, y + 0.1, '%.2f' % y, ha='center', va='bottom')
    240         # for x, y in temp:
    241         #     plt.text(x + 0.05, -y - 0.1, '%.2f' % y, ha='center', va='bottom')
    242         # # 设置坐标轴范围
    243         # plt.xlim(-1, n)
    244         # plt.ylim(-1.5, 1.5)
    245         # # 去除坐标轴
    246         # plt.xticks(())
    247         # plt.yticks(())
    248         # plt.show()
    249 
    250         # # 定义等高线高度函数
    251         # def f(x, y):
    252         #     return (1 - x / 2 + x ** 5 + y ** 3) * np.exp(- x ** 2 - y ** 2)
    253         # # 数据数目
    254         # n = 256
    255         # # 定义x, y
    256         # x = np.linspace(-3, 3, n)
    257         # y = np.linspace(-3, 3, n)
    258         # # 生成网格数据
    259         # X, Y = np.meshgrid(x, y)
    260         # # 填充等高线的颜色, 8是等高线分为几部分
    261         # plt.contourf(X, Y, f(X, Y), 8, alpha=0.75, cmap=plt.cm.hot)
    262         # # 绘制等高线
    263         # C = plt.contour(X, Y, f(X, Y), 8, colors='black', linewidth=0.5)
    264         # # 绘制等高线数据
    265         # plt.clabel(C, inline=True, fontsize=10)
    266         # # 去除坐标轴
    267         # plt.xticks(())
    268         # plt.yticks(())
    269         # plt.show()
    270 
    271         # # 定义图像数据
    272         # a = np.linspace(0, 1, 9).reshape(3, 3)
    273         # # 显示图像数据
    274         # plt.imshow(a, interpolation='nearest', cmap='bone', origin='lower')
    275         # # 添加颜色条
    276         # plt.colorbar()
    277         # # 去掉坐标轴
    278         # plt.xticks(())
    279         # plt.yticks(())
    280         # plt.show()
    281 
    282         # # 定义figure
    283         # fig = plt.figure()
    284         # # 将figure变为3d
    285         # ax = Axes3D(fig)
    286         # # 数据数目
    287         # n = 256
    288         # # 定义x, y
    289         # x = np.arange(-4, 4, 0.25)
    290         # y = np.arange(-4, 4, 0.25)
    291         # # 生成网格数据
    292         # X, Y = np.meshgrid(x, y)
    293         # # 计算每个点对的长度
    294         # R = np.sqrt(X ** 2 + Y ** 2)
    295         # # 计算Z轴的高度
    296         # Z = np.sin(R)
    297         # # 绘制3D曲面
    298         # ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))
    299         # # 绘制从3D曲面到底部的投影
    300         # ax.contour(X, Y, Z, zdim='z', offset=-2, cmap='rainbow')
    301         # # 设置z轴的维度
    302         # ax.set_zlim(-2, 2)
    303         # plt.show()
    304 
    305         # plt.figure()
    306         # # 绘制第一个图
    307         # plt.subplot(2, 2, 1)
    308         # plt.plot([0, 1], [0, 1])
    309         # # 绘制第二个图
    310         # plt.subplot(2, 2, 2)
    311         # plt.plot([0, 1], [0, 1])
    312         # # 绘制第三个图
    313         # plt.subplot(2, 2, 3)
    314         # plt.plot([0, 1], [0, 1])
    315         # # 绘制第四个图
    316         # plt.subplot(2, 2, 4)
    317         # plt.plot([0, 1], [0, 1])
    318         # plt.show()
    319 
    320         # plt.figure()
    321         # # 绘制第一个图
    322         # plt.subplot(2, 1, 1)
    323         # plt.plot([0, 1], [0, 1])
    324         # # 绘制第二个图
    325         # plt.subplot(2, 3, 4)
    326         # plt.plot([0, 1], [0, 1])
    327         # # 绘制第三个图
    328         # plt.subplot(2, 3, 5)
    329         # plt.plot([0, 1], [0, 1])
    330         # # 绘制第四个图
    331         # plt.subplot(2, 3, 6)
    332         # plt.plot([0, 1], [0, 1])
    333         # plt.show()
    334 
    335         # # 定义figure
    336         # plt.figure()
    337         # # figure分成3行3列, 取得第一个子图的句柄, 第一个子图跨度为1行3列, 起点是表格(0, 0)
    338         # ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3, rowspan=1)
    339         # ax1.plot([0, 1], [0, 1])
    340         # ax1.set_title('Test')
    341         # # figure分成3行3列, 取得第二个子图的句柄, 第二个子图跨度为1行3列, 起点是表格(1, 0)
    342         # ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=1)
    343         # ax2.plot([0, 1], [0, 1])
    344         # # figure分成3行3列, 取得第三个子图的句柄, 第三个子图跨度为1行1列, 起点是表格(1, 2)
    345         # ax3 = plt.subplot2grid((3, 3), (1, 2), colspan=1, rowspan=1)
    346         # ax3.plot([0, 1], [0, 1])
    347         # # figure分成3行3列, 取得第四个子图的句柄, 第四个子图跨度为1行3列, 起点是表格(2, 0)
    348         # ax4 = plt.subplot2grid((3, 3), (2, 0), colspan=3, rowspan=1)
    349         # ax4.plot([0, 1], [0, 1])
    350         # plt.show()
    351 
    352         # # 定义figure
    353         # plt.figure()
    354         # # 分隔figure
    355         # gs = gridspec.GridSpec(3, 3)
    356         # ax1 = plt.subplot(gs[0, :])
    357         # ax2 = plt.subplot(gs[1, 0:2])
    358         # ax3 = plt.subplot(gs[1, 2])
    359         # ax4 = plt.subplot(gs[2, :])
    360         # # 绘制图像
    361         # ax1.plot([0, 1], [0, 1])
    362         # ax1.set_title('Test')
    363         # ax2.plot([0, 1], [0, 1])
    364         # ax3.plot([0, 1], [0, 1])
    365         # ax4.plot([0, 1], [0, 1])
    366         # plt.show()
    367 
    368         # # 定义figure
    369         # fig = plt.figure()
    370         # # 定义数据
    371         # x = [1, 2, 3, 4, 5, 6, 7]
    372         # y = [1, 3, 4, 2, 5, 8, 6]
    373         # # figure的百分比, 从figure 10%的位置开始绘制, 宽高是figure的80%
    374         # left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
    375         # # 获得绘制的句柄
    376         # ax1 = fig.add_axes([left, bottom, width, height])
    377         # # 绘制点(x,y)
    378         # ax1.plot(x, y, 'r')
    379         # ax1.set_xlabel('x')
    380         # ax1.set_ylabel('y')
    381         # ax1.set_title('test')
    382         # # 嵌套方法一
    383         # # figure的百分比, 从figure 10%的位置开始绘制, 宽高是figure的80%
    384         # left, bottom, width, height = 0.2, 0.6, 0.25, 0.25
    385         # # 获得绘制的句柄
    386         # ax2 = fig.add_axes([left, bottom, width, height])
    387         # # 绘制点(x,y)
    388         # ax2.plot(x, y, 'r')
    389         # ax2.set_xlabel('x')
    390         # ax2.set_ylabel('y')
    391         # ax2.set_title('part1')
    392         # # 嵌套方法二
    393         # plt.axes([bottom, left, width, height])
    394         # plt.plot(x, y, 'r')
    395         # plt.xlabel('x')
    396         # plt.ylabel('y')
    397         # plt.title('part2')
    398         # plt.show()
    399 
    400         # # 定义数据
    401         # x = np.arange(0, 10, 0.1)
    402         # y1 = 0.05 * x ** 2
    403         # y2 = -1 * y1
    404         # # 定义figure
    405         # fig, ax1 = plt.subplots()
    406         # # 得到ax1的对称轴ax2
    407         # ax2 = ax1.twinx()
    408         # # 绘制图像
    409         # ax1.plot(x, y1, 'g-')
    410         # ax2.plot(x, y2, 'b--')
    411         # # 设置label
    412         # ax1.set_xlabel('X data')
    413         # ax1.set_xlabel('Y1', color='g')
    414         # ax2.set_xlabel('Y2', color='b')
    415         # plt.show()
    416 
    417         # 定义figure
    418         fig, ax = plt.subplots()
    419         # 定义数据
    420         x = np.arange(0, 2 * np.pi, 0.01)
    421         # line, 表示只取返回值中的第一个元素
    422         line, = ax.plot(x, np.sin(x))
    423         # 定义动画的更新
    424         def update(i):
    425             line.set_ydata(np.sin(x + i / 10))
    426             return line,
    427         # 定义动画的初始值
    428         def init():
    429             line.set_ydata(np.sin(x))
    430             return line,
    431         # 创建动画
    432         ani = animation.FuncAnimation(fig=fig, func=update, init_func=init, interval=10, blit=False, frames=200)
    433         # 展示动画
    434         plt.show()
    435         # 动画保存
    436         # 我这里是保存为html文件了,打开即可完美运行
    437         ani.save('sin.html', writer='imagemagick', fps=30, dpi=100)
    438 
    439     except Exception:
    440         print("Error !!!")
    441 
    442     # # # name:
    443     # try:
    444     #     print("此部分有问题")
    445     #     print('>>>1
    ', )
    446     #     print('>>>2
    ', )
    447     #     print('>>>3
    ', )
    448     #     print('>>>4
    ', )
    449     #     print('>>>5
    ', )
    450     #     print('>>>6
    ', )
    451     #     print('>>>7
    ', )
    452     #     print('>>>8
    ', )
    453     #     print('>>>9
    ', )
    454     #     print('>>>10
    ', )
    455     #     print('>>>11
    ', )
    456     #     print('>>>12
    ', )
    457     #     print('>>>13
    ', )
    458     #     print('>>>14
    ', )
    459     #     print('>>>15
    ', )
    460     #     print('>>>16
    ', )
    461     #     print('>>>17
    ', )
    462     # except Exception:
    463     #     print("Error !!!")

    --------

    ---------------------------------------------------------------------------

  • 相关阅读:
    es5预览本地文件、es6练习代码演示案例
    Java实现 LeetCode 838 推多米诺(暴力模拟)
    Java实现 LeetCode 838 推多米诺(暴力模拟)
    Java实现 LeetCode 838 推多米诺(暴力模拟)
    Java实现 LeetCode 837 新21点(DP)
    Java实现 LeetCode 837 新21点(DP)
    Java实现 LeetCode 837 新21点(DP)
    Java实现 LeetCode 836 矩形重叠(暴力)
    Subversion under Linux [Reprint]
    Subversion how[Reprint]
  • 原文地址:https://www.cnblogs.com/caochucheng/p/10042150.html
Copyright © 2011-2022 走看看