0

我想画一个和弦图。为了首先使该方法起作用,我遵循了这个示例。(请注意,为此,您必须在命令行上输入“bokeh sampledata”来下载示例数据)。

我使用的代码(主要取自示例,但添加了 matplotlib 以保存图像)是:

import holoviews as hv
from holoviews import opts, dim
from bokeh.sampledata.airport_routes import routes, airports
hv.extension('bokeh')

# Count the routes between Airports
route_counts = routes.groupby(['SourceID', 'DestinationID']).Stops.count().reset_index()
nodes = hv.Dataset(airports, 'AirportID', 'City')
chord = hv.Chord((route_counts, nodes), ['SourceID', 'DestinationID'], ['Stops'])

# Select the 20 busiest airports
busiest = list(routes.groupby('SourceID').count().sort_values('Stops').iloc[-20:].index.values)
busiest_airports = chord.select(AirportID=busiest, selection_mode='nodes')

fig = plt.figure()
busiest_airports.opts(opts.Chord(cmap='Category20', edge_color=dim('SourceID').str(),height=800, labels='City', node_color=dim('AirportID').str(), width=800))
fig.savefig('plot.png')

制作了一个名为 plot.png 的文件,但它是空的。我尝试以不同的方式稍微编辑代码(例如,使用不同的图像格式,如 .pdf),但它没有改变。有没有人有任何想法?

更新有效的方法:从 holoviews 导入 o​​pts 导入 holoviews 作为 hv,从 bokeh.sampledata.airport_routes 导入路线昏暗,机场 hv.extension('bokeh')

# Count the routes between Airports
route_counts = routes.groupby(['SourceID', 'DestinationID']).Stops.count().reset_index()
nodes = hv.Dataset(airports, 'AirportID', 'City')
chord = hv.Chord((route_counts, nodes), ['SourceID', 'DestinationID'], ['Stops'])

# Select the 20 busiest airports
busiest = list(routes.groupby('SourceID').count().sort_values('Stops').iloc[-20:].index.values)
busiest_airports = chord.select(AirportID=busiest, selection_mode='nodes')
plot = busiest_airports.opts(opts.Chord(cmap='Category20', edge_color=dim('SourceID').str(),height=800, labels='City', node_color=dim('AirportID').str(), width=800))
hv.save(plot,'plot2.png')

它有效,如下所示。

4

0 回答 0