0

I want to draw a chord diagram. To first get the method working, I was following this example. (Note that for this, on the command line, you have to type 'bokeh sampledata' to download the sample data).

The code I used (taken mostly from the example, but adding in matplotlib to save the image) is:

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')

There is a file called plot.png made, but it is empty. I've tried editing the code slightly in different ways (e.g. doing different image formats such as .pdf) but it doesn't change. Does anyone have any ideas?

Update to what works: 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')
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')

and it works, as suggested below.

Slowat_Kela
  • 1,377
  • 2
  • 22
  • 60
  • 1
    Is there any indication that you can mix holoviews bokeh with matplotlib? That is, how is the matplotlib figure `fig` supposed to know it should contain any data? – ImportanceOfBeingErnest Feb 11 '19 at 10:25
  • Wow I literally just assumed. Thanks so much, have just found this: http://holoviews.org/user_guide/Plotting_with_Matplotlib.html and am giving it a go now. – Slowat_Kela Feb 11 '19 at 10:35
  • 2
    Anything to do with matplotlib should be able to be deleted in the above code; that code is not doing anything, because you selected the bokeh extension, not matplotlib. You also shouldn't need both hv.output and hv.save; hv.save should be sufficient. – James A. Bednar Feb 11 '19 at 14:28

0 Answers0