{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Most examples work across multiple plotting backends, this example is also available for:\n", "\n", "* [Matplotlib NYC Taxi Connection](../matplotlib/nyc_taxi_connections.ipynb)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import networkx as nx\n", "import holoviews as hv\n", "from holoviews import opts\n", "from holoviews import dim\n", "\n", "from holoviews.element.graphs import layout_nodes\n", "from bokeh.sampledata.airport_routes import routes, airports\n", "\n", "hv.extension('bokeh')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Declare data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create dataset indexed by AirportID and with additional value dimension\n", "airports = hv.Dataset(airports, ['AirportID'], ['Name', 'IATA', 'City'])\n", "source_airports = list(airports.select(City='New York').data.AirportID)\n", "\n", "# Add connections count to routes then aggregate and select just routes connecting to NYC\n", "routes['connections'] = 1\n", "nyc_graph = hv.Graph((routes, airports), ['SourceID', \"DestinationID\"], ['connections'], label='NYC Airport Connections')\\\n", " .aggregate(function=np.count_nonzero).select(SourceID=source_airports)\n", "\n", "# Lay out graph weighting and weight by the number of connections\n", "np.random.seed(14)\n", "graph = layout_nodes(nyc_graph, layout=nx.layout.fruchterman_reingold_layout, kwargs={'weight': 'connections'})\n", "labels = hv.Labels(graph.nodes, ['x', 'y'], ['IATA', 'City'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nyc_labels = labels.select(City='New York').opts(\n", " text_color='white', yoffset=0.05, text_font_size='16pt')\n", "\n", "other_labels = labels[labels['City']!='New York'].opts(\n", " text_color='white', text_font_size='8pt')\n", "\n", "cmap = {3697: 'red', 3797: 'blue'}\n", "\n", "(graph * nyc_labels * other_labels).opts(\n", " opts.Graph(bgcolor='gray', width=800, height=800,\n", " edge_color=dim('SourceID').categorize(cmap, 'gray'),\n", " node_color=dim('index').categorize(cmap, 'gray'),\n", " title='NYC Airport Connections',\n", " xaxis=None, yaxis=None)\n", ")" ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 2 }