美文网首页
python3.6 matplotlib figure, sub

python3.6 matplotlib figure, sub

作者: LeeMin_Z | 来源:发表于2018-08-20 23:41 被阅读617次

    小结:

    1. 注意Anaconda prompt启动项 (小心!)
    2. 简单画图
    3. 颜色,标记和线型
    4. 设置标题,轴标签,刻度以及刻度标签
    5. 一张图中包含多个函数,label 参数

    1. 注意Anaconda prompt启动项

    ipython --pylab

    如果你仅用ipython启动,将会在google/stack overflow耗费两三小时排查“为啥show不了两次图片” ,试遍以下方法都是得个吉。终于在看源码前发现启动项不一样...

    https://stackoverflow.com/questions/5524858/matplotlib-show-doesnt-work-twice?FORM=UCIAST&pname=shenma

    https://stackoverflow.com/questions/5524858/matplotlib-show-doesnt-work-twice

    2. 简单画图

    1. 创建subplot画格子图
    import numpy as np
    import pandas as pd
    from pandas import DataFrame,Series
    import matplotlib.pyplot as plt
    
    # 生成四个格子subplot
    
    In [23]: fig = plt.figure()
    
    In [24]: ax4 = fig.add_subplot(2,2,4)
    
    In [25]: ax3 = fig.add_subplot(2,2,3)
    
    In [26]: ax2 = fig.add_subplot(2,2,2)
    
    In [27]: ax1 = fig.add_subplot(2,2,1)
    
    # 选中格子生成图片
    
    In [29]: ax4.plot([1.5,3.5,-2,1.6])
    Out[29]: [<matplotlib.lines.Line2D at 0x27b1331710>]
    
    In [30]: _ = ax1.hist(randn(100),bins=20,color='k',alpha=0.3)
    
    In [31]: ax2.scatter(np.arange(30),np.arange(30) + 3 * randn(30))
    Out[31]: <matplotlib.collections.PathCollection at 0x27b19872e8>
    
    In [32]: ax3.plot(randn(50).cumsum(),'k--')
    Out[32]: [<matplotlib.lines.Line2D at 0x27b199e3c8>]
    
    
    pic1.png
    1. subplots生成多个格子,再挑选作图。

    这种生成方式的格子坐标有点像二维数组

    # 生成多个subplots
    
    In [35]: fig, axes = plt.subplots(2,3)
    
    In [36]: axes
    Out[36]:
    array([[<matplotlib.axes._subplots.AxesSubplot object at 0x00000027B46BD518>,
            <matplotlib.axes._subplots.AxesSubplot object at 0x00000027B478FEB8>,
            <matplotlib.axes._subplots.AxesSubplot object at 0x00000027B48979B0>],
           [<matplotlib.axes._subplots.AxesSubplot object at 0x00000027B48D36D8>,
            <matplotlib.axes._subplots.AxesSubplot object at 0x00000027B4910208>,
            <matplotlib.axes._subplots.AxesSubplot object at 0x00000027B4949828>]],
    dtype=object)
    
    In [38]: axes[0,1].plot([1.5,3.5,-2,1.6])
    Out[38]: [<matplotlib.lines.Line2D at 0x27b50fc9e8>]
    
    pic2.png
    1. 共享坐标
    In [41]: for i in range(2):
        ...:     for j in range(2):
        ...:         axes[i,j].hist(randn(500),bins=50,color='k',alpha=0.5)
        ...:     plt.subplots_adjust(wspace=0 , hspace = 0)
        ...:
    
    pic3.png

    3. 颜色,标记和线型

    1. 颜色和线型
    In [48]: plt.plot(x,y,'g--')
    Out[48]: [<matplotlib.lines.Line2D at 0x27b400e278>]
    
    In [49]: plt.plot(x,y,linestyle = '--',color = 'g')
    Out[49]: [<matplotlib.lines.Line2D at 0x27b5299128>]
    
    pic4.png
    1. 标记
    In [53]: plt.plot(randn(30).cumsum(),'ko--')
    Out[53]: [<matplotlib.lines.Line2D at 0x27b19a89e8>]
    
    In [54]: plt.plot(randn(30).cumsum(),'g+--')
    Out[54]: [<matplotlib.lines.Line2D at 0x27b08bb588>]
    
    pic5.png

    4. 设置标题,轴标签,刻度以及刻度标签

    基本就是set后面看看选项,x轴和y轴基本可以按着选。

    pic6.png

    5. 一张图中包含多个函数,label 参数

    In [61]: ax = plt.figure().add_subplot(1,1,1)
    
    In [62]: ax.plot(randn(1000).cumsum(),'k',label='one')
    Out[62]: [<matplotlib.lines.Line2D at 0x27b5389d68>]
    
    In [63]: ax.plot(randn(1000).cumsum(),'g--',label='two')
    Out[63]: [<matplotlib.lines.Line2D at 0x27b53929e8>]
    
    In [66]: ax.plot(randn(1000).cumsum(),'r.',label='three')
    Out[66]: [<matplotlib.lines.Line2D at 0x27b53afd68>]
    
    In [67]: ax.legend(loc = 'best')
    Out[67]: <matplotlib.legend.Legend at 0x27b525d550>
    
    
    pic6.png

    2018.8.14

    学到《用python进行数据分析》的 P214
    理智上是知道python3.7已经出来了,现实是anaconda升级stream有点麻烦..

    相关文章

      网友评论

          本文标题:python3.6 matplotlib figure, sub

          本文链接:https://www.haomeiwen.com/subject/ysgjvftx.html