飞道的博客

python大佬的可视化工具-HoloViews

454人阅读  评论(0)

欢迎关注公众号:pythonic生物人

holoviews是一个超级简洁的python可视化工具,后端为bokeh、matplotlib、datashader库,最擅长干的是一行代码搞定一张图(类似seaborn),如下文的河流图(Sankey);

HoloViews helps you understand your data better, by letting you work seamlessly with both the data and its graphical representation.

目录

01 - 精彩demo

02 - 快速上手holoviews

holoviews安装

从scatter开始

使用“+”添加Layout

使用“*”添加Overlay

添加交互小部件

使用opts个性化图形设置

更多精彩


01 - 精彩demo


  
  1. import pandas as pd
  2. import holoviews as hv
  3. hv.extension( 'matplotlib')
  4. edges_df = pd.read_csv( 'fb_edges.csv')
  5. nodes_df = pd.read_csv( 'fb_nodes.csv')
  6. fb_nodes = hv.Nodes(nodes_df).sort()
  7. fb_graph = hv.Graph((edges_df, fb_nodes), label= 'Facebook Circles') #绘图
  8. fb_graph.opts(cmap= 'Set1',
  9. node_color= 'circle',
  10. fig_size= 350,
  11. show_frame= False,
  12. xaxis= None,
  13. yaxis= None,
  14. node_size= 10)

 


  
  1. edges = pd.read_csv( 'energy.csv') #导入数据
  2. sankey = hv.Sankey(edges, label= 'Energy Diagram') #绘图
  3. sankey.opts(label_position= 'left',
  4. edge_color= 'target',
  5. node_color= 'index',
  6. cmap= 'set1') #图形属性设置

hv.Sankey(edges, label='Energy Diagram') 一行代码搞定小面的河流图~~


  
  1. # 矩阵图
  2. import holoviews as hv
  3. from holoviews import opts
  4. hv.extension( 'bokeh')
  5. from bokeh.sampledata.iris import flowers
  6. from holoviews.operation import gridmatrix
  7. ds = hv.Dataset(flowers)
  8. grouped_by_species = ds.groupby( 'species', container_type=hv.NdOverlay)
  9. grid = gridmatrix(grouped_by_species, diagonal_type=hv.Scatter) #绘图
  10. grid.opts(opts.Scatter(tools=[ 'hover', 'box_select'], bgcolor= '#efe8e2', fill_alpha= 0.2, size= 4))

02 - 快速上手holoviews

holoviews安装

pip install holoviews -i https://pypi.tuna.tsinghua.edu.cn/simple

从scatter开始


  
  1. import pandas as pd
  2. import numpy as np
  3. import holoviews as hv
  4. from holoviews import opts
  5. hv.extension( 'bokeh', 'matplotlib') #导入扩展'bokeh','matplotlib'
  6. station_info = pd.read_csv( 'station_info.csv')
  7. hv.Scatter(station_info, 'services', 'ridership') #轻松绘制散点图

使用“+”添加Layout


  
  1. # 使用“+”添加Layout
  2. hv.Scatter(station_info, 'services', 'ridership') + \
  3. hv.Histogram(
  4. np.histogram(station_info[ 'opened'], bins= 24), kdims=[ 'opened'])+\
  5. hv.Scatter(station_info, 'services', 'ridership')

使用“*”添加Overlay


  
  1. # 使用“*”添加Overlay
  2. taxi_dropoffs = {
  3. hour: arr
  4. for hour, arr in np.load( 'hourly_taxi_data.npz').items()
  5. }
  6. bounds = ( -74.05, 40.70, -73.90, 40.80)
  7. image = hv.Image(taxi_dropoffs[ '0'], [ 'lon', 'lat'], bounds=bounds)
  8. points = hv.Points(station_info, [ 'lon', 'lat'])
  9. image + image * points

添加交互小部件


  
  1. # 添加交互小部件
  2. dictionary = {
  3. int(hour): hv.Image(arr, [ 'lon', 'lat'], bounds=bounds)
  4. for hour, arr in taxi_dropoffs.items()
  5. }
  6. hv.HoloMap(dictionary, kdims= 'Hour')

使用opts个性化图形设置


  
  1. # 默认bokeh后端
  2. spike_train = pd.read_csv( 'spike_train.csv.gz')
  3. curve = hv.Curve(spike_train, 'milliseconds', 'Hertz') # 折线图
  4. spikes = hv.Spikes(spike_train, 'milliseconds', []) # 条形码
  5. layout = curve + spikes #
  6. layout


  
  1. #opts个性化图形属性设置
  2. layout.opts(
  3. #Options
  4. opts.Curve(height= 200,
  5. width= 900,
  6. xaxis= None,
  7. line_width= 1.50,
  8. color= 'red',
  9. tools=[ 'hover']),
  10. opts.Spikes(height= 150,
  11. width= 900,
  12. yaxis= None,
  13. line_width= 0.25,
  14. color= 'grey')).cols( 1)

更多精彩

https://github.com/holoviz/holoviews

 

 


转载:https://blog.csdn.net/qq_21478261/article/details/115425925
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场