zoukankan      html  css  js  c++  java
  • 数据可视化:python matplotlib小试牛刀

    Matplotlib有两个模块:

    1) 绘图API:pyplot, 可这样导入import matplotlib.pyplot as plt   

    2)集成库:pylab, 是matplotlib Scipy Numpy的集成库

    import pandas as pd
    import matplotlib.pyplot as plt
    from pylab import mpl
    #设置标题字体
    mpl.rcParams['font.sans-serif']=['FangSong']
    
    df = pd.read_csv('data.csv', index_col = '年份')
    df.head()
    #dataframe读取数据的方式,行属性
    x=df.index.values
    #dataframe读取数据的方式,列名
    y=df['人均GDP(元)'].values
    #构造figure对象 ,和子窗口
    fig,ax = plt.subplots()
    ax.plot(x,y,'r--o')
    ax.set(title='GDP走势',xlabel='年份')
    print(df.head())
    plt.show()
    #第二种画折线图的方式2
    #fig=plt.figure()
    #设置subplot(x,y,z) x:行数 y:列数,z:接下来Plot画图位置,放在第几个自窗口
    #ax=fig.add_subplot(111)
    
    #柱形图
    #df['人均GDP(元)'].plot(kind='bar')
    #plt.show()
    

      

    加上!chcp 65001,解决pycharm里的python console不识别中文的问题

    3)通过matplotlib可以作动画不,回答是肯定的,以下是结合matplotlib.animation, 做了一个在sin曲线上运动的小动画。

    主要通过调用animation.FuncAnimation来实现的。

    
    
    import numpy as np
    import matplotlib
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    
    def update_points(num):
        print(num)
        point_ani.set_data(x[num], y[num])
        return  point_ani,
    
    x = np.linspace(0, 2*np.pi,100)
    y = np.sin(x)
    
    fig = plt.figure(tight_layout = True)
    plt.plot(x,y)
    point_ani, = plt.plot(x[0], y[0], "ro")
    plt.grid(ls="--")
    ani = animation.FuncAnimation(fig, update_points , np.arange(0,100),interval =100,blit =True)
    ### where to draw, how to draw , init list
    plt.show()

  • 相关阅读:
    linux 清理cache中的内存
    科学计算和可视化
    Python生成随机数的方法
    matplotlib绘图的基本操作
    python中的数组和列表
    Python人工智能学习笔记
    利用numpy+matplotlib绘图的基本操作教程
    split函数用法
    玩转PIL库
    广师大学习笔记之文本统计(jieba库好玩的词云)
  • 原文地址:https://www.cnblogs.com/7star/p/12388630.html
Copyright © 2011-2022 走看看