美文网首页
DataFrame画图

DataFrame画图

作者: Chaweys | 来源:发表于2020-12-01 00:00 被阅读0次

    #coding=utf-8
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    df=pd.DataFrame(
        np.random.randint(1,15,30).reshape(10,3),
        columns=["A","B","C"]
    )
    print(df)
    '''
        A   B   C
    0   7   4   1
    1   1   5   6
    2  11   6   9
    3   8  12  14
    4  11   1  13
    5  12   6  14
    6  12  10  14
    7  10   4  14
    8  11  13   2
    9  11  14  11
    '''
    
    
    '''
    kind=line    画折线图
    kind=bar     x轴画矩形图
    kind=barh    y轴画矩形图
    kind=pie     画饼图
    kind=scatter 画散点
    kind=box     画盒子图
    kind=kde     画核密度估计图
    '''
    
    #kind=line 画折线图,默认以index作为横轴,所有列的数据作为纵轴
    df.plot(kind="line")
    plt.show()
    
    kind=line 画其中一列折线图
    df.A.plot(kind="line") #这实际就是Series的画图
    plt.show()
    
    
    #kind=line 画某一行折线图
    print(df.loc[0])
    '''
    A     9
    B     8
    C    11
    Name: 0, dtype: int32
    '''
    df.loc[0].plot(kind="line")
    plt.show()
    
    DataFrame画折线图1.png
    DataFrame画折线图2-画单独一列.png
    DataFrame画折线图3-画单独一行.png

    #kind=bar 画条形图
    df.plot(kind="bar")
    plt.show()
    
    #kind=bar 画条形图-堆叠各条形图
    df.plot(kind="bar",stacked=True)
    plt.show()
    
    #kind=barh 画y轴画矩形图
    df.plot(kind="barh")
    plt.show()
    
    DataFrame画条形图1.png
    DataFrame画条形图1-堆叠各条形图.png
    DataFrame画条形图2-y轴矩形图.png

    #kind=area 画面积占比图
    df.plot(kind="area")
    plt.show()
    
    DataFrame画面积占比图1.png

    #kind=hist 画直方图
    df.plot(kind="hist")
    plt.show()
    
    #kind=hist 指定某一列画直方图
    df.A.plot(kind="hist")
    plt.show()
    
    DataFrame画直方图1.png
    DataFrame画直方图1-指定某一列.png

    相关文章

      网友评论

          本文标题:DataFrame画图

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