Python可视化学习笔记二

来源:互联网 发布:小学四年级体测数据 编辑:程序博客网 时间:2024/06/05 18:46

一、定义图表类型-柱状图、线形图和堆积柱状图

# -*- coding: UTF-8 -*-from matplotlib.pyplot import *import matplotlib.pyplot as pltdef 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()

这里写图片描述