Python 数据可视化利器有哪些?都用过的你至少是30k往上了吧?

Python 数据可视化利器有哪些?都用过的你至少是30k往上了吧?,第1张

概述推荐数据可视化的库有挺多的,这里推荐几个比较常用的:MatplotlibPlotlySeaborn

推荐

数据可视化的库有挺多的,这里推荐几个比较常用的:

Matplotlib Plotly Seaborn Ggplot bokeh Pyechart Pygal

Plotly

plotly 文档地址(https://plot.ly/python/#financial-charts)

使用方式:

plotly 有 online 和 offline 两种方式,这里只介绍 offline 的。

这是 plotly 官方教程的一部分

import plotly.plotly as pyimport numpy as npdata = [dict( visible=False,line=dict(color='#00CED1',wIDth=6),# 配置线宽和颜色 name=' = ' + str(step),x=np.arange(0,10,0.01),# x 轴参数 y=np.sin(step * np.arange(0,0.01))) for step in np.arange(0,5,0.1)] # y 轴参数data[10]['visible'] = Truepy.iplot(data,filename='Single Sine Wave')

只要将最后一行中的

py.iplot

替换为下面代码

py.offline.plot

便可以运行。

漏斗图

这个图代码太长了,就不 po 出来了。

进群:548377875  即可获取神秘大礼包一份哦!从零基础到项目实战的数十套pdf哦!

Basic Box Plot

好吧,不知道怎么翻译,直接用原名。

import plotly.plotlyimport plotly.graph_obJs as goimport numpy as npy0 = np.random.randn(50)-1y1 = np.random.randn(50)+1trace0 = go.Box( y=y0)trace1 = go.Box( y=y1)data = [trace0,trace1]plotly.offline.plot(data)

Wind Rose Chart

好吧,不知道怎么翻译,直接用原名。

import plotly.graph_obJs as gotrace1 = go.barpolar( r=[77.5,72.5,70.0,45.0,22.5,42.5,40.0,62.5],text=['north','N-E','East','S-E','South','S-W','West','N-W'],name='11-14 m/s',marker=dict( color='rgb(106,81,163)' ))trace2 = go.barpolar( r=[57.49999999999999,50.0,35.0,20.0,37.5,55.00000000000001],# 鼠标浮动标签文字描述 name='8-11 m/s',marker=dict( color='rgb(158,154,200)' ))trace3 = go.barpolar( r=[40.0,30.0,7.5,32.5,40.0],name='5-8 m/s',marker=dict( color='rgb(203,201,226)' ))trace4 = go.barpolar( r=[20.0,15.0,2.5,12.5,22.5],name='< 5 m/s',marker=dict( color='rgb(242,240,247)' ))data = [trace1,trace2,trace3,trace4]layout = go.Layout( Title='Wind Speed distribution in Laurel,NE',Font=dict( size=16 ),legend=dict( Font=dict( size=16 ) ),radialaxis=dict( ticksuffix='%' ),orIEntation=270)fig = go.figure(data=data,layout=layout)plotly.offline.plot(fig,filename='polar-area-chart')

Basic Ternary Plot with Markers

篇幅有点长,这里就不 po 代码了。

bokeh

这里展示一下常用的图表和比较抢眼的图表,详细的文档可查看(https://bokeh.pydata.org/en/latest/docs/user_guIDe/categorical.HTML)

条形图

这配色看着还挺舒服的,比 pyecharts 条形图的配色好看一点。

条形图

from bokeh.io import show,output_filefrom bokeh.models import ColumnDataSourcefrom bokeh.palettes import Spectral6from bokeh.plotting import figureoutput_file("colormapped_bars.HTML")# 配置输出文件名fruits = ['Apples','魅族','OPPO','VIVO','小米','华为'] # 数据counts = [5,3,4,2,6] # 数据source = ColumnDataSource(data=dict(fruits=fruits,counts=counts,color=Spectral6))p = figure(x_range=fruits,y_range=(0,9),plot_height=250,title="Fruit Counts",toolbar_location=None,tools="")# 条形图配置项p.vbar(x='fruits',top='counts',wIDth=0.9,color='color',legend="fruits",source=source)p.xgrID.grID_line_color = None # 配置网格线颜色p.legend.orIEntation = "horizontal" # 图表方向为水平方向p.legend.location = "top_center"show(p) # 展示图表

年度条形图

可以对比不同时间点的量。

年度条形图

from bokeh.io import show,output_filefrom bokeh.models import ColumnDataSource,FactorRangefrom bokeh.plotting import figureoutput_file("bars.HTML") # 输出文件名fruits = ['Apple','华为'] # 参数years = ['2015','2016','2017'] # 参数data = {'fruits': fruits,'2015': [2,1,4],'2016': [5,6],'2017': [3,3]}x = [(fruit,year) for fruit in fruits for year in years]counts = sum(zip(data['2015'],data['2016'],data['2017']),()) source = ColumnDataSource(data=dict(x=x,counts=counts))p = figure(x_range=FactorRange(*x),title="Fruit Counts by Year",tools="")p.vbar(x='x',source=source)p.y_range.start = 0p.x_range.range_padding = 0.1p.xaxis.major_label_orIEntation = 1p.xgrID.grID_line_color = Noneshow(p)

饼图

饼图

from collections import Counterfrom math import piimport pandas as pdfrom bokeh.io import output_file,showfrom bokeh.palettes import category20cfrom bokeh.plotting import figurefrom bokeh.transform import cumsumoutput_file("pIE.HTML")x = Counter({ '中国': 157,'美国': 93,'日本': 89,'巴西': 63,'德国': 44,'印度': 42,'意大利': 40,'澳大利亚': 35,'法国': 31,'西班牙': 29})data = pd.DataFrame.from_dict(dict(x),orIEnt='index').reset_index().rename(index=str,columns={0:'value','index':'country'})data['angle'] = data['value']/sum(x.values()) * 2*pidata['color'] = category20c[len(x)]p = figure(plot_height=350,title="PIE Chart",tools="hover",tooltips="@country: @value")p.wedge(x=0,y=1,radius=0.4,start_angle=cumsum('angle',include_zero=True),end_angle=cumsum('angle'),line_color="white",fill_color='color',legend='country',source=data)p.axis.axis_label=Nonep.axis.visible=Falsep.grID.grID_line_color = Noneshow(p) 

条形图

年度水果进出口

from bokeh.io import output_file,showfrom bokeh.models import ColumnDataSourcefrom bokeh.palettes import GnBu3,OrRd3from bokeh.plotting import figureoutput_file("stacked_split.HTML")fruits = ['Apples','Pears','Nectarines','Plums','Grapes','StrawberrIEs']years = ["2015","2016","2017"]exports = {'fruits': fruits,3]}imports = {'fruits': fruits,'2015': [-1,-1,-3,-2,-1],'2016': [-2,-2],'2017': [-1,-2]}p = figure(y_range=fruits,x_range=(-16,16),title="Fruit import/export,by year",toolbar_location=None)p.hbar_stack(years,y='fruits',height=0.9,color=GnBu3,source=ColumnDataSource(exports),legend=["%s exports" % x for x in years])p.hbar_stack(years,color=OrRd3,source=ColumnDataSource(imports),legend=["%s imports" % x for x in years])p.y_range.range_padding = 0.1p.ygrID.grID_line_color = Nonep.legend.location = "top_left"p.axis.minor_tick_line_color = Nonep.outline_line_color = Noneshow(p)

散点图

散点图

from bokeh.plotting import figure,output_file,showoutput_file("line.HTML")p = figure(plot_wIDth=400,plot_height=400)p.circle([1,5],[6,7,size=20,color="navy",Alpha=0.5)show(p)

六边形图

这两天,马蜂窝刚被发现数据造假,这不,与马蜂窝应应景。

六边形图

import numpy as npfrom bokeh.io import output_file,showfrom bokeh.plotting import figurefrom bokeh.util.hex import axial_to_cartesianoutput_file("hex_coords.HTML")q = np.array([0,1])r = np.array([0,0])p = figure(plot_wIDth=400,plot_height=400,toolbar_location=None) # p.grID.visible = False # 配置网格是否可见p.hex_tile(q,r,size=1,fill_color=["firebrick"] * 3 + ["navy"] * 4,Alpha=0.5)x,y = axial_to_cartesian(q,"pointytop")p.text(x,y,text=["(%d,%d)" % (q,r) for (q,r) in zip(q,r)],text_baseline="mIDdle",text_align="center")show(p)

环比条形图

这个实现挺厉害的,看了一眼就吸引了我。我在代码中都做了一些注释,希望对你理解有帮助。注:圆心为正中央,即直角坐标系中标签为(0,0)的地方。

环比条形图

from collections import OrderedDictfrom math import log,sqrtimport numpy as npimport pandas as pdfrom six.moves import cStringIO as StringIOfrom bokeh.plotting import figure,show,output_fileantibiotics = """bacteria,penicillin,streptomycin,neomycin,gram结核分枝杆菌,800,negative沙门氏菌,0.8,0.09,negative变形杆菌,0.1,negative肺炎克雷伯氏菌,850,1.2,negative布鲁氏菌,0.02,negative铜绿假单胞菌,0.4,negative大肠杆菌,100,negative产气杆菌,870,1.6,negative白色葡萄球菌,0.007,0.001,positive溶血性链球菌,14,positive草绿色链球菌,0.005,40,positive肺炎双球菌,11,positive"""drug_color = OrderedDict([# 配置中间标签名称与颜色 ("盘尼西林","#0d3362"),("链霉素","#c64737"),("新霉素","black"),])gram_color = { "positive": "#aeaeb8","negative": "#e69584",}# 读取数据df = pd.read_csv(StringIO(antibiotics),skiprows=1,skipinitialspace=True,engine='python')wIDth = 800height = 800inner_radius = 90outer_radius = 300 - 10minr = sqrt(log(.001 * 1E4))maxr = sqrt(log(1000 * 1E4))a = (outer_radius - inner_radius) / (minr - maxr)b = inner_radius - a * maxrdef rad(mic): return a * np.sqrt(np.log(mic * 1E4)) + bbig_angle = 2.0 * np.pi / (len(df) + 1)small_angle = big_angle / 7# 整体配置p = figure(plot_wIDth=wIDth,plot_height=height,title="",x_axis_type=None,y_axis_type=None,x_range=(-420,420),y_range=(-420,min_border=0,outline_line_color="black",background_fill_color="#f0e1d2")p.xgrID.grID_line_color = Nonep.ygrID.grID_line_color = None# annular wedgesangles = np.pi / 2 - big_angle / 2 - df.index.to_serIEs() * big_angle #计算角度colors = [gram_color[gram] for gram in df.gram] # 配置颜色p.annular_wedge( 0,inner_radius,outer_radius,-big_angle + angles,angles,color=colors,)# small wedgesp.annular_wedge(0,rad(df.penicillin),-big_angle + angles + 5 * small_angle,-big_angle + angles + 6 * small_angle,color=drug_color['盘尼西林'])p.annular_wedge(0,rad(df.streptomycin),-big_angle + angles + 3 * small_angle,-big_angle + angles + 4 * small_angle,color=drug_color['链霉素'])p.annular_wedge(0,rad(df.neomycin),-big_angle + angles + 1 * small_angle,-big_angle + angles + 2 * small_angle,color=drug_color['新霉素'])# 绘制大圆和标签labels = np.power(10.0,np.arange(-3,4))radii = a * np.sqrt(np.log(labels * 1E4)) + bp.circle(0,radius=radii,fill_color=None,line_color="white")p.text(0,radii[:-1],[str(r) for r in labels[:-1]],text_Font_size="8pt",text_align="center",text_baseline="mIDdle")# 半径p.annular_wedge(0,inner_radius - 10,outer_radius + 10,color="black")# 细菌标签xr = radii[0] * np.cos(np.array(-big_angle / 2 + angles))yr = radii[0] * np.sin(np.array(-big_angle / 2 + angles))label_angle = np.array(-big_angle / 2 + angles)label_angle[label_angle < -np.pi / 2] += np.pi # easIEr to read labels on the left sIDe# 绘制各个细菌的名字p.text(xr,yr,df.bacteria,angle=label_angle,text_Font_size="9pt",text_baseline="mIDdle")# 绘制圆形,其中数字分别为 x 轴与 y 轴标签p.circle([-40,-40],[-370,-390],color=List(gram_color.values()),radius=5)# 绘制文字p.text([-30,-30],text=["Gram-" + gr for gr in gram_color.keys()],text_Font_size="7pt",text_align="left",text_baseline="mIDdle")# 绘制矩形,中间标签部分。其中 -40,-40,-40 为三个矩形的 x 轴坐标。18,0,-18 为三个矩形的 y 轴坐标p.rect([-40,-40,[18,-18],wIDth=30,height=13,color=List(drug_color.values()))# 配置中间标签文字、文字大小、文字对齐方式p.text([-15,-15,-15],text=List(drug_color),text_baseline="mIDdle")output_file("burtin.HTML",title="burtin.py example")show(p)

元素周期表

元素周期表,这个实现好牛逼啊,距离初三刚开始学化学已经很遥远了,想当年我还是化学课代表呢!由于基本用不到化学了,这里就不实现了。

元素周期表

真实状态

Pyecharts

pyecharts 也是一个比较常用的数据可视化库,用得也是比较多的了,是百度 echarts 库的 python 支持。这里也展示一下常用的图表。文档地址为(http://pyecharts.org/#/zh-cn/prepare?ID=%E5%AE%89%E8%A3%85-pyecharts)

条形图

条形图

from pyecharts import barbar = bar("我的第一个图表","这里是副标题")bar.add("服装",["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"],[5,20,36,75,90])# bar.print_echarts_options() # 该行只为了打印配置项,方便调试时使用bar.render() # 生成本地 HTML 文件

散点图

散点图

from pyecharts import Polarimport randomdata_1 = [(10,random.randint(1,100)) for i in range(300)]data_2 = [(11,100)) for i in range(300)]polar = Polar("极坐标系-散点图示例",wIDth=1200,height=600)polar.add("",data_1,type='scatter')polar.add("",data_2,type='scatter')polar.render()

饼图

饼图

import randomfrom pyecharts import PIEattr = ['A','B','C','D','E','F']pIE = PIE("饼图示例",wIDth=1000,height=600)pIE.add( "",attr,[random.randint(0,100) for _ in range(6)],radius=[50,55],center=[25,50],is_random=True,)pIE.add( "",[random.randint(20,radius=[0,45],rosetype="area",center=[65,rosetype="radius",)pIE.render()

词云

这个是我在前面的文章中用到的图片实例,这里就不 po 具体数据了。

词云

from pyecharts import WordCloudname = ['Sam S Club'] # 词条value = [10000] # 权重wordcloud = WordCloud(wIDth=1300,height=620)wordcloud.add("",name,value,word_size_range=[20,100])wordcloud.render()

树图

这个是我在前面的文章中用到的图片实例,这里就不 po 具体数据了。

树图

from pyecharts import TreeMapdata = [ # 键值对数据结构 { value: 1212,# 数值 # 子节点 children: [ { # 子节点数值 value: 2323,# 子节点名 name: 'description of this node',children: [...],},{ value: 4545,name: 'description of this node',children: [ { value: 5656,children: [...] },... ] } ] },... ]treemap = TreeMap(Title,height=600) # 设置标题与宽高treemap.add("深圳",data,is_label_show=True,label_pos='insIDe',label_text_size=19)treemap.render()

地图

地图

from pyecharts import Mapvalue = [155,66,78,33,80,190,53,49.6]attr = [ "福建","山东","北京","上海","甘肃","新疆","河南","广西","西藏" ]map = Map("Map 结合 VisualMap 示例",height=600)map.add( "",maptype="china",is_visualmap=True,visual_text_color="#000",)map.render()

3D 散点图

from pyecharts import Scatter3Dimport randomdata = [ [random.randint(0,100),random.randint(0,100)] for _ in range(80)]range_color = [ '#313695','#4575b4','#74add1','#abd9e9','#e0f3f8','#ffffbf','#fee090','#fdae61','#f46d43','#d73027','#a50026']scatter3D = Scatter3D("3D 散点图示例",height=600) # 配置宽高scatter3D.add("",visual_range_color=range_color) # 设置颜色等scatter3D.render() # 渲染

后记

大概介绍就是这样了,三个库的功能都挺强大的,bokeh 的中文资料会少一点,如果阅读英文有点难度,还是建议使用 pyecharts 就好。总体也不是很难,按照文档来修改数据都能够直接上手使用。主要是多练习。

总结

以上是内存溢出为你收集整理的Python 数据可视化利器有哪些?都用过的你至少是30k往上了吧?全部内容,希望文章能够帮你解决Python 数据可视化利器有哪些?都用过的你至少是30k往上了吧?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1208584.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存