Python可視化學習筆記二

一、定義圖表類型-柱狀圖、線形圖和堆積柱狀圖

# -*- coding: UTF-8 -*-

from matplotlib.pyplot import *
import matplotlib.pyplot as plt

def for_example():
    #plot儲存數據集,多組數據展現多條線
    '''
    plot([1,2,3,2,3,2,2,1])
    plot([4,3,2,1],[1,2,3,4])
    '''
    x=[1,2,3,4]
    y=[5,4,3,2]
    #創建一個新的圖表
    figure()
    #創建一個圖表中的子圖
    subplot(231)#括號裏面是2行3列第一個圖,中間也可以是(2,3,1)
    plot(x,y)

    subplot(232)
    bar(x,y)

    subplot(233)
    barh(x,y)

    subplot(234)
    bar(x,y)

    y1=[7,8,5,3]
    bar(x,y1,bottom=y,color='r')

    subplot(235)
    boxplot(x)

    subplot(236)
    scatter(x,y)

    plt.show()

if __name__ == '__main__':
    for_example()

這裏寫圖片描述

散點圖

def san_dian_picture():
    x = np.random.rand(1000)#生成隨機值
    y1= np.random.randn(len(x))
    y2= 1.2+np.exp(x)#對矩陣a中每個元素取指數函數,ex

    ax1=plt.subplot(121)
    plt.scatter(x,y1,color='indigo',alpha=0.3,edgecolors='white',label='no correl')
    plt.xlabel('no correlation')
    plt.grid(True)
    plt.legend()

    ax2=plt.subplot(122,sharey=ax1,sharex=ax1)
    plt.scatter(x,y2,color='green',alpha=0.3,edgecolors='grey',label='correl')
    plt.xlabel('strong correlation')
    plt.grid(True)
    plt.legend()

    plt.show()

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章