博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python绘制数据可视化图(seaborn+numpy+pandas+matplotlib)
阅读量:3917 次
发布时间:2019-05-23

本文共 3441 字,大约阅读时间需要 11 分钟。

通过简单的matlplotlib第三方库,导入你的数据进行绘制柱状图。## 柱状图

import matplotlib.pyplot as pltimport stringplt.xlabel('hour')plt.ylabel('time')plt.title('Average number of rides per hour per week')xx = []#存放X值yy = []def savey(str1):    fx=open("f://"+str1,"r").read().split("\n")    ax=[]    x=[]    s=0    i=0    for i in range(len(fx)):        s=float(fx[i])        ax.append(s)    x=ax    return xdef savex(str1):    fx=open("f://"+str1,"r").read().split("\n")    ax=[]    x=[]    s=0    i=0    for i in range(len(fx)):        s=int(fx[i])        ax.append(s)    x=ax    return xxx=savex("datax.txt")yy=savex("datay.txt")plt.bar(xx,yy,label='bike sharing',color='r')plt.legend()plt.show()

在这里插入图片描述

绘制折线图

通过plot函数,可以绘制多条折线。

import matplotlib.pyplot as pltdef savex(str1):    fx=open("f://"+str1,"r").read().split("\n")    ax=[]    x=[]    s=0    i=0    for i in range(len(fx)):        s=float(fx[i])        ax.append(s)    x=ax    return x#x=[2013,2014,2015,2016,2017,2018]x=savex("datax.txt")y1=savex("data_50.txt")y2=savex("data_60.txt")y3=savex("data_70.txt")y4=savex("data_80.txt")y5=savex("data_90.txt")plt.plot(x,y1,label="1950",color='r')plt.plot(x,y2,label="1960",color='g')plt.plot(x,y3,label="1970",color='b')plt.plot(x,y4,label="1980",color='y')plt.plot(x,y5,label="1990",color='black')#plt.plot(x2,y2,label='Second one')plt.xlabel('Month')plt.ylabel("People")plt.title('People Monthly active membership in 2015.1~2017.12')plt.legend()plt.show()

在这里插入图片描述

山脉图

import matplotlib.pyplot as plt

y0=[145227,99538,211741,220121,215442,173487]

y1=[669732,651493,808558,1057055,1218525,1240965]
y2=[219400,202856,269400,371680,444132,463433]
x=[2013,2014,2015,2016,2017,2018]
plt.stackplot(x,y0,y1,y2,colors = [‘g’,‘r’,‘b’])

plt.xlabel(‘year’)

plt.ylabel(‘times’)
plt.title(‘Total number of rides by men and women each September in 2013.9-2018.9’)
plt.legend()
plt.show()
在这里插入图片描述

Seaborn库绘制多变量图

通过用pairgrid()和map()函数可以生成多种可视化图。

import numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport seaborn as sns%matplotlib inlinedata = pd.read_excel('d:\\num.xls',sheet_name='Sheet1') data1=data["location"]data2=data["cost"]data3=data["impact"]a=pd.DataFrame({'location':data1,"impact":data3,"cost":data3})g = sns.PairGrid(a)g.map_diag(sns.distplot)g.map_upper(plt.scatter)g.map_lower(sns.kdeplot)

在这里插入图片描述

Seaborn多变量散点图

import numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport seaborn as sns%matplotlib inlinetips = pd.read_excel('d:\\num.xls',sheet_name='Sheet1')   sns.stripplot(x = 'location', y = 'religious', data = tips, jitter= True,hue = 'sex', dodge = True)sns.set()# Load the brain networks example datasetdf = sns.load_dataset("brain_networks", header=[0, 1, 2], index_col=0)# Select a subset of the networksused_networks = [1, 5, 6, 7, 8, 12, 13, 17]used_columns = (df.columns.get_level_values("network")                          .astype(int)                          .isin(used_networks))df = df.loc[:, used_columns]# Create a categorical palette to identify the networksnetwork_pal = sns.husl_palette(8, s=.45)network_lut = dict(zip(map(str, used_networks), network_pal))# Convert the palette to vectors that will be drawn on the side of the matrixnetworks = df.columns.get_level_values("network")network_colors = pd.Series(networks, index=df.columns).map(network_lut)# Draw the full plotsns.clustermap(df.corr(), center=0, cmap="vlag",               row_colors=network_colors, col_colors=network_colors,               linewidths=.75, figsize=(13, 13))

在这里插入图片描述

转载地址:http://noprn.baihongyu.com/

你可能感兴趣的文章
[NewLife.Net]单机400万长连接压力测试
查看>>
使用Azure人脸API对图片进行人脸识别
查看>>
在.NET Core中使用MongoDB明细教程(1):驱动基础及文档插入
查看>>
行为型设计模式总结
查看>>
基于Prometheus和Grafana打造业务监控看板
查看>>
UEFI + GPT 启动 VHD
查看>>
聊聊常见的服务(接口)认证授权
查看>>
[Hei-Ocelot-Gateway ].Net Core Api网关Ocelot的开箱即用版本
查看>>
初识ABP vNext(4):vue用户登录&菜单权限
查看>>
麒麟系统兼容安卓生态 弥补生态短板
查看>>
ASP.NET Core Blazor Webassembly 之 组件
查看>>
【Azure Show】|第五期(下)当下最火热的Blazor与App Service, 嘉宾闫晓迪&Alan Tsai...
查看>>
如何让多端口网站用一个nginx进行反向代理实际场景分析
查看>>
如何校验内存数据的一致性,DynamicExpresso 算是帮上大忙了
查看>>
C#实践设计模式原则SOLID
查看>>
可能是Asp.net Core On host、 docker、kubernetes(K8s) 配置读取的最佳实践
查看>>
基于PaaS平台的多应用自集成方案之公共数据集成
查看>>
利用 Github Actions 自动更新 docfx 文档
查看>>
安装VSCode作为常用的文本编辑器
查看>>
.NET Core + K8S + Apollo 玩转配置中心
查看>>