最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • 数据可视化matplotlib

    正文概述 流芳   2020-12-15   425

    数据可视化matplotlib

    import matplotlib.pyplot as plt
    import numpy as np
    import numpy.random as randn
    import pandas as pd
    from pandas import Series,DataFrame
    from pylab import mpl
    mpl.rcParams['axes.unicode_minus'] = False # 我自己配置的问题
    plt.rc('figure', figsize=(10, 6)) # 设置图像大小
    
    %matplotlib inline

    1. figure对象

    Matplotlib的图像均位于figure对象中。

    • 创建figure: plt.figure()

    fig = plt.figure()

    2. subplot子图

    • add_subplot:向figure对象中添加子图。

    • add_subplot(a, b, c):a,b 表示讲fig分割成axb的区域,c 表示当前选中要操作的区域(c从1开始)。
      add_subplot返回的是AxesSubplot对象,plot 绘图的区域是最后一次指定subplot的位置

    ax1 = fig.add_subplot(2,2,1)
    ax2 = fig.add_subplot(2,2,2)
    ax3 = fig.add_subplot(2,2,3)
    ax4 = fig.add_subplot(2,2,4)
    random_arr = randn.rand(50)
    # 默认是在最后一次使用subplot的位置上作图
    plt.plot(random_arr,'ro--') # r:表示颜色为红色,o:表示数据用o标记 ,--:表示虚线
    # 等价于:
    # plt.plot(random_arr,linestyle='--',color='r',marker='o')
    plt.show()

    数据可视化matplotlib

    # hist:直方图:统计分布情况
    plt.hist(np.random.rand(8), bins=6, color='b', alpha=0.3) 
    # bins:数据箱子个数
    (array([ 3.,  0.,  0.,  0.,  2.,  3.]),
     array([ 0.10261627,  0.19557319,  0.28853011,  0.38148703,  0.47444396,
             0.56740088,  0.6603578 ]),
     <a list of 6 Patch objects>)

    数据可视化matplotlib

    # 散点图
    plt.scatter(np.arange(30), np.arange(30) + 3 * randn.randn(30))

    数据可视化matplotlib

    • subplots :生成子图/子图数组

    # 柱状图
    fig, ax = plt.subplots()
    x = np.arange(5)
    y1, y2 = np.random.randint(1, 25, size=(2, 5))
    width = 0.25
    ax.bar(x, y1, width, color='r') 
    # 画柱子ax.bar(x+width, y2, width, color='g') 
    # 画柱子ax.set_xticks(x+width)
    ax.set_xticklabels(['a', 'b', 'c', 'd', 'e']) # 下标注明

    数据可视化matplotlib

    fig, axes = plt.subplots(2, 2, sharex=True, sharey=True) # 共享轴坐标

    数据可视化matplotlib

    • subplots_adjust:调整subplots的间距

    plt.subplots_adjust(left=0.5,top=0.5)
    fig, axes = plt.subplots(2, 2)

    数据可视化matplotlib

    random_arr = randn.randn(8)
    fig, axes = plt.subplots(2, 2)
    axes[0, 0].hist(random_arr, bins=16, color='k', alpha=0.5)
    axes[0, 1].plot(random_arr,'ko--')
    x = np.arange(8)
    y = x + 5 * np.random.rand(8)
    axes[1,0].scatter(x, y)
    x = np.arange(5)
    y1, y2 = np.random.randint(1, 25, size=(2, 5))
    width = 0.25axes[1,1].bar(x, y1, width, color='r') # 画柱子
    axes[1,1].bar(x+width, y2, width, color='g') # 画柱子
    axes[1,1].set_xticks(x+width)
    axes[1,1].set_xticklabels(['a', 'b', 'c', 'd', 'e']) # 下标注明

    数据可视化matplotlib

    • 重叠绘制

    • legend:显示图例

    random_arr1 = randn.randn(8)
    random_arr2 = randn.randn(8)
    fig, ax = plt.subplots()
    ax.plot(random_arr1,'ko--',label='A')
    ax.plot(random_arr2,'b^--',label='B')
    plt.legend(loc='best') # 自动选择放置图例的最佳位置

    数据可视化matplotlib

    • 设置刻度范围:set_xlim、set_ylim

    • 设置显示的刻度:set_xticks、set_yticks

    • 刻度标签:set_xticklabels、set_yticklabels

    • 坐标轴标签:set_xlabel、set_ylabe

    • l图像标题:set_title

    fig, ax = plt.subplots(1)
    ax.plot(np.random.randn(380).cumsum())
    
    # 设置刻度范围a
    x.set_xlim([0, 500])
    
    # 设置显示的刻度(记号)
    ax.set_xticks(range(0,500,100))
    
    # 设置刻度标签
    ax.set_xticklabels(['one', 'two', 'three', 'four', 'five'],
    rotation=30, fontsize='small')
    
    # 设置坐标轴标签ax.set_xlabel('X:...')
    ax.set_ylabel('Y:...')
    
    # 设置标题
    ax.set_title('Example')

    数据可视化matplotlib

    3. Plotting functions in pandas

    plt.close('all')
    s = Series(np.random.randn(10).cumsum(), index=np.arange(0, 100, 10))
    s
    fig,ax = plt.subplots(1)
    s.plot(ax=ax,style='ko--')

    数据可视化matplotlib

    fig, axes = plt.subplots(2, 1)
    data = Series(np.random.rand(16), index=list('abcdefghijklmnop'))
    data.plot(kind='bar', ax=axes[0], color='k', alpha=0.7)
    data.plot(kind='barh', ax=axes[1], color='k', alpha=0.7)

    数据可视化matplotlib

    df = DataFrame(np.random.randn(10, 4).cumsum(0),
                   columns=['A', 'B', 'C', 'D'],
                   index=np.arange(0, 100, 10))
    df
    ABCD
    0-0.5238221.061179-0.882215-0.26771810-0.178175-0.367573-1.465189-1.095390200.2761660.816511-0.3445571.297281300.5294000.159374-2.7651681.78469240-1.129003-1.665272-2.7465123.140976500.265113-1.821224-5.1408502.37744960-2.699879-3.895255-5.0115611.71517470-2.384257-3.480928-4.5191312.80536980-2.525243-3.031608-4.8401251.10662490-2.020589-3.519473-4.8232920.522323
    df.plot() # 列索引为图例,行索引为横坐标,值为纵坐标

    数据可视化matplotlib

    df = DataFrame(np.random.randint(0,2,(10, 2)),
                   columns=['A', 'B'],
                   index=np.arange(0, 10, 1))
    df
    AB
    001101210301410510611700810910
    df.plot(kind='bar')

    数据可视化matplotlib

    df.A.value_counts().plot(kind='bar')

    数据可视化matplotlib

    df.A[df.B == 1].plot(kind='kde')   
    df.A[df.B == 0].plot(kind='kde')    # 密度图

    数据可视化matplotlib

    df = DataFrame(np.random.rand(6, 4),
                   index=['one', 'two', 'three', 'four', 'five', 'six'],
                   columns=pd.Index(['A', 'B', 'C', 'D'], name='Genus'))
    df
    GenusABCD
    one0.7607500.9511590.6431810.792940two0.1372940.0054170.6856680.858801three0.2574550.7219730.9689510.043061four0.2981000.1212930.4006580.236369five0.4639190.5370550.6759180.487098six0.7986760.2391880.9155830.456184
    df.plot(kind='bar',stacked='True') #行索引:横坐标

    数据可视化matplotlib

    values = Series(np.random.normal(0, 1, size=200))
    values.hist(bins=100, alpha=0.3, color='k', normed=True)
    values.plot(kind='kde', style='k--')

    数据可视化matplotlib

    df = DataFrame(np.random.randn(10,2),
                   columns=['A', 'B'],
                   index=np.arange(0, 10, 1))
    df
    plt.scatter(df.A, df.B)

    数据可视化matplotlib

    更多python相关文章请关注python自学网。


    起源地下载网 » 数据可视化matplotlib

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元