我想画一个和弦图。为了首先使该方法起作用,我在遵循this示例。 (请注意,为此,您必须在命令行上键入“ 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),但它没有改变。有人有什么想法吗?
更新为有效的方法: 将hloviews导入为hv 从holoviews导入opts,昏暗 从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')
并且可以正常工作,如下所示。