### Fetch Datashader Examples Source: https://datashader.org/getting_started/index Fetches the Datashader example datasets and navigates to the project directory. ```bash datashader examples cd datashader-examples ``` -------------------------------- ### Install Datashader using Pip Source: https://datashader.org/getting_started/index Installs Datashader using the pip package manager. ```bash pip install datashader ``` -------------------------------- ### Setup Datashader Development Environment Source: https://datashader.org/getting_started Sets up a new conda environment for Datashader development, installs the library in editable mode, and activates the environment. ```shell cd datashader conda env create --name datashader --file ./examples/environment.yml conda activate datashader pip install --no-deps -e . ``` -------------------------------- ### Install Latest/Pre-release Datashader Source: https://datashader.org/getting_started/index Installs the latest or pre-release versions of Datashader from the pyviz channel using conda. ```bash conda install -c pyviz datashader conda install -c pyviz/label/dev datashader ``` -------------------------------- ### Fetch Datashader Examples Source: https://datashader.org/getting_started Fetches the example datasets and code for Datashader, creating a 'datashader-examples' directory. It also includes commands to update conda environment or create a new one for running the examples. ```shell datashader examples cd datashader-examples conda env update --file environment.yml ``` ```shell conda env create --name datashader --file environment.yml conda activate datashader ``` -------------------------------- ### Install Latest Releases using Conda Source: https://datashader.org/getting_started Installs the latest stable releases of Datashader from the pyviz channel using conda. ```shell conda install -c pyviz datashader ``` -------------------------------- ### Install Datashader in Editable Mode Source: https://datashader.org/getting_started/index Installs the Datashader package in editable mode without its dependencies, preparing it for development. ```bash pip install --no-deps -e . ``` -------------------------------- ### Install Pre-release Versions using Conda Source: https://datashader.org/getting_started Installs the latest pre-release versions of Datashader from the dev-labeled channel using conda. ```shell conda install -c pyviz/label/dev datashader ``` -------------------------------- ### Install Datashader using Conda Source: https://datashader.org/getting_started/index Installs Datashader using the conda package manager. Recommended for optimized numerical libraries. ```bash conda install datashader ``` -------------------------------- ### Clone Datashader Git Repository Source: https://datashader.org/getting_started/index Clones the Datashader project from its GitHub repository. ```bash git clone git://github.com/holoviz/datashader.git ``` -------------------------------- ### Datashader examples function Source: https://datashader.org/releases The `examples()` function allows users to retrieve notebooks and other example files corresponding to the installed Datashader version. This facilitates easy access to demonstration code and usage examples. ```python datashader.examples() ``` -------------------------------- ### Update Conda Environment for Examples Source: https://datashader.org/getting_started/index Updates the current conda environment with dependencies required to run Datashader examples, using an environment.yml file. ```bash conda env update --file environment.yml ``` -------------------------------- ### Install Datashader dependencies for examples Source: https://datashader.org/releases This command is used to install the necessary dependencies to run the Datashader examples. It relies on the `environment.yml` file for package management. ```shell conda env create -f examples/environment.yml ``` -------------------------------- ### Create and Activate Conda Environment Source: https://datashader.org/getting_started/index Creates a new conda environment named 'datashader' and activates it, using an environment.yml file for dependencies. ```bash conda env create --name datashader --file environment.yml conda activate datashader ``` -------------------------------- ### Datashader CLI for Example Datasets Source: https://datashader.org/releases This command-line interface command is used for downloading example datasets that can be utilized with Datashader, as demonstrated in various example notebooks. ```bash datashader-download-data ``` -------------------------------- ### Initialize HoloViews with Datashader Extension Source: https://datashader.org/user_guide/Trimesh Imports necessary libraries (holoviews, datashade) and initializes the HoloViews extension for Bokeh, preparing for interactive plotting with Datashader. ```python import holoviews as hv from holoviews.operation.datashader import datashade hv.extension("bokeh") ``` -------------------------------- ### Initialize Datashader and HoloViews for Dynamic Plots Source: https://datashader.org/user_guide/Timeseries Sets up the HoloViews environment with Bokeh backend and configures PlotSize for better visualization. Imports necessary components from holoviews and datashader. ```python import holoviews as hv from holoviews.operation.datashader import datashade from holoviews.streams import PlotSize PlotSize.scale=2 hv.extension('bokeh') ``` -------------------------------- ### Datashader Aggregation Examples Source: https://datashader.org/getting_started/Pipeline Demonstrates the creation of aggregate arrays using Datashader's Canvas for points, showcasing different aggregation strategies and data selection. ```python agg = canvas.points(df, 'x', 'y') tf.Images(tf.shade(agg.where(agg>=np.percentile(agg,99)), name="99th Percentile"), tf.shade(np.power(agg,2), name="Numpy square ufunc"), tf.shade(np.sin(agg), name="Numpy sin ufunc")) ``` ```python sel1 = agg_d3_d5.where(aggc.sel(cat='d3') == aggc.sel(cat='d5'), other=-1).astype('uint32') sel2 = agg.where(aggc.sel(cat='d3') == aggc.sel(cat='d5'), other=-1).astype('uint32') tf.Images(tf.shade(sel1, name='d3+d5 where d3==d5'), tf.shade(sel2, name='d1+d2+d3+d4+d5 where d3==d5')) ``` -------------------------------- ### Import HoloViews for Interactive Plots Source: https://datashader.org/user_guide/Trajectories Imports `datashade` and `spread` operations from HoloViews, along with HoloViews itself and initializes the Bokeh extension for interactive plotting. ```python from holoviews.operation.datashader import datashade, spread import holoviews as hv hv.extension('bokeh') ``` -------------------------------- ### Run Datashader dashboard with OpenSky data Source: https://datashader.org/releases This command executes the Datashader dashboard script with a specified configuration file for the OpenSky data. It's an example of how to deploy a Datashader-powered dashboard. ```python python dashboard/dashboard.py -c dashboard/opensky.yml ``` -------------------------------- ### Install Datashader Optional Dependencies Source: https://datashader.org/releases To enable raster data support via `Canvas.raster`, you need to install optional dependencies like `rasterio` and `scikit-image`. Note that `rasterio` relies on `gdal`, and its conda package might have missing dependencies such as `krb5`. ```bash conda install rasterio scikit-image ``` -------------------------------- ### Visualize Trimesh with Datashader Source: https://datashader.org/user_guide/Trimesh Visualizes the generated triangular mesh using Datashader's `trimesh` method. It shades the mesh based on vertex attributes and displays the first 15 triangles. ```python tf.Images(tris.head(15), tf.shade(cvs.trimesh(verts, tris))) ``` -------------------------------- ### Execute Triangulation and Time Source: https://datashader.org/user_guide/Trimesh Executes the `triangulate` function on the generated vertices and measures the execution time using IPython's %time magic command. ```python %time tris = triangulate(verts) ``` -------------------------------- ### Import Libraries for Datashader Source: https://datashader.org/user_guide/Trajectories Imports necessary libraries including pandas for data manipulation, numpy for numerical operations, and datashader for efficient data visualization. ```python import pandas as pd import numpy as np import datashader as ds import datashader.transfer_functions as tf ``` -------------------------------- ### Datashader Shade Function Example Source: https://datashader.org/api Demonstrates the use of the datashader.transfer_functions.shade function to convert a DataArray into an image. It outlines the parameters for colormapping, categorical coloring, and alpha channel control. ```python datashader.transfer_functions.shade(_agg_, _cmap=['lightblue', 'darkblue']_, _color_key=['#e41a1c', '#377eb8', '#4daf4a', '#984ea3', '#ff7f00', '#ffff33', '#a65628', '#f781bf', '#999999', '#66c2a5', '#fc8d62', '#8da0cb', '#a6d854', '#ffd92f', '#e5c494', '#ffffb3', '#fb8072', '#fdb462', '#fccde5', '#d9d9d9', '#ccebc5', '#ffed6f']_, _how='eq_hist'_, _alpha=255_, _min_alpha=40_, _span=None_, _name=None_, _color_baseline=None_, _rescale_discrete_levels=False_) ``` -------------------------------- ### Import Libraries for Datashader Timeseries Source: https://datashader.org/user_guide/Timeseries Imports necessary libraries including datetime, pandas, numpy, xarray, and datashader for data manipulation and visualization. ```python import datetime import pandas as pd import numpy as np import xarray as xr import datashader as ds import datashader.transfer_functions as tf ``` -------------------------------- ### Datashader Trimesh with Interpolation Options Source: https://datashader.org/user_guide/Trimesh Demonstrates Datashader's `trimesh` visualization with and without linear interpolation. It uses the 'colorcet' library for a rainbow colormap and highlights the difference in shading between nearest neighbor and linear interpolation. ```python from colorcet import rainbow as c tf.Images(tf.shade(cvs.trimesh(verts, tris, interpolate='nearest'), cmap=c, name='10 Vertices'), tf.shade(cvs.trimesh(verts, tris, interpolate='linear'), cmap=c, name='10 Vertices Interpolated')) ``` -------------------------------- ### Datashader Where Reduction Example Source: https://datashader.org/api Example of using ds.where for reductions in Datashader, specifically to find values corresponding to the maximum in a column within bins. ```python >>> canvas.line(df, 'x', 'y', agg=ds.where(ds.max("value"), "other")) ``` -------------------------------- ### Apply Post-Shading Hook with Datashader and Dynspread Source: https://datashader.org/getting_started/Interactivity Shows how to apply a post-shading hook using Datashader's `dsshow`, specifically demonstrating the use of `tf.dynspread` to enhance visual clarity. Includes examples with default and custom parameters. ```python from functools import partial fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, sharex=True, sharey=True) dsshow(df, ds.Point('x', 'y'), ds.count_cat('cat'), ax=ax1); dsshow(df, ds.Point('x', 'y'), ds.count_cat('cat'), shade_hook=tf.dynspread, x_range=(-2, -1.5), y_range=(-2, -1.5), ax=ax2); ``` ```python dsshow(df, ds.Point('x', 'y'), ds.count_cat('cat'), shade_hook=partial(tf.dynspread, threshold=0.50, how='over')); ``` -------------------------------- ### Visualize Function on Regular Grid (Python) Source: https://datashader.org/user_guide/Trimesh Visualizes the sine-based function 'f' by sampling it on a regular grid using NumPy's linspace and meshgrid. The resulting image is then shaded using Datashader's tf.shade. ```python n = 400 ls = np.linspace(-1.0, 1.0, n) x,y = np.meshgrid(ls, ls) img = f(x,y) raster = tf.shade(tf.Image(img, name="Raster")) raster ``` -------------------------------- ### Install Plotting Libraries (conda) Source: https://datashader.org/user_guide/Plotting_Pitfalls Installs necessary Python libraries for plotting, including HoloViews, colorcet, and matplotlib, with an optional dependency on scikit-image. ```bash conda install holoviews colorcet matplotlib scikit-image ``` -------------------------------- ### Generate Random Points with Datashader Source: https://datashader.org/user_guide/Trimesh Generates a specified number of random 2D points with associated z-values using NumPy and Pandas. These points are then prepared for Datashader processing. ```python import numpy as np import datashader as ds import pandas as pd import datashader.utils as du import datashader.transfer_functions as tf from scipy.spatial import Delaunay import dask.dataframe as dd n = 10 np.random.seed(2) x = np.random.uniform(size=n) y = np.random.uniform(size=n) z = np.random.uniform(0,1.0,x.shape) pts = np.stack((x,y,z)).T verts = pd.DataFrame(np.stack((x,y,z)).T, columns=['x', 'y' , 'z']) ``` -------------------------------- ### Plot Points with Datashader Source: https://datashader.org/user_guide/Trimesh Visualizes the generated points using Datashader. It creates a canvas, shades the points based on their mean 'z' value, and applies a spread effect for better visibility. ```python cvs = ds.Canvas(plot_height=400,plot_width=400) tf.Images(verts.head(15), tf.spread(tf.shade(cvs.points(verts, 'x', 'y', agg=ds.mean('z')), name='Points'))) ``` -------------------------------- ### Display Static Datashader Plot Source: https://datashader.org/user_guide/Trajectories Generates and displays a static image of the entire trajectory dataset using the `create_image` function. This demonstrates plotting without downsampling. ```python %time create_image() ``` -------------------------------- ### Create and Datashade Trimesh Plots Source: https://datashader.org/user_guide/Trimesh Demonstrates creating HoloViews Trimesh objects from triangle and vertex data, applying the datashade operation, and optionally aggregating data (e.g., mean of 'z'). It shows how to combine a wireframe plot with a trimesh plot and set plot options. ```python wireframe = datashade(hv.TriMesh((tris,verts), label="Wireframe").edgepaths) trimesh = datashade(hv.TriMesh((tris,hv.Points(verts, vdims='z')), label="TriMesh"), aggregator=ds.mean('z')) (wireframe + trimesh).opts(width=400, height=400) ``` -------------------------------- ### Zoomed Trimesh Rendering with Datashader (Python) Source: https://datashader.org/user_guide/Trimesh Renders multiple zoomed views of the trimesh using Datashader's Canvas with specified x and y ranges. This allows for inspecting fine details in different regions of the data. ```python tf.Images(*([tf.shade(ds.Canvas(x_range=r, y_range=r).trimesh(verts, tris, mesh=mesh)) for r in [(0.1,0.8), (0.14,0.4), (0.15,0.2)]])) ``` -------------------------------- ### Datashader Summary Reduction Example Source: https://datashader.org/api Demonstrates how to create a summary reduction to compute the mean of one column and the sum of another column simultaneously in a single pass. ```python import datashader as ds red = ds.summary(mean_a=ds.mean('a'), sum_b=ds.sum('b')) ``` -------------------------------- ### Datashader Trimesh: Mean, Max, and Min Aggregations Source: https://datashader.org/user_guide/Trimesh Demonstrates the use of different aggregation functions (mean, max, min) with Datashader's trimesh method to visualize mesh data. These aggregations determine how values from multiple triangles contribute to a pixel's value, useful for revealing different aspects of the data, especially in dense areas or for narrow features. ```python tf.Images(tf.shade(cvs.trimesh(verts, tris, mesh=mesh, agg=ds.mean('z')),name='mean'), tf.shade(cvs.trimesh(verts, tris, mesh=mesh, agg=ds.max('z')), name='max'), tf.shade(cvs.trimesh(verts, tris, mesh=mesh, agg=ds.min('z')), name='min')) ``` -------------------------------- ### Datashader Hover Layer with Bokeh Source: https://datashader.org/releases This example demonstrates how to integrate Datashader with Bokeh to add hover capabilities to visualized data, allowing users to inspect data points. ```python # Example usage of datashader.bokeh_ext.HoverLayer import datashader as ds import datashader.bokeh_ext from bokeh.plotting import figure, show # Assuming 'agg' is a datashader aggregate p = figure() hover_layer = datashader.bokeh_ext.HoverLayer(p, agg) show(p) ``` -------------------------------- ### Create Static Datashader Image Source: https://datashader.org/user_guide/Trajectories Defines a function to create a static image of the trajectory data using Datashader. It initializes a Datashader Canvas, aggregates the line data, and shades the resulting aggregation. ```python def create_image(x_range=x_range, y_range=y_range, w=500, h=500): cvs = ds.Canvas(x_range=x_range, y_range=y_range, plot_height=h, plot_width=w) agg = cvs.line(df, 'x', 'y', agg=ds.any()) return tf.shade(agg) ``` -------------------------------- ### Datashader Trimesh: Any, Count, and Std Aggregations Source: https://datashader.org/user_guide/Trimesh Illustrates the application of 'any', 'count', and 'std' aggregation functions with Datashader's trimesh method. 'any' is useful for masking meshed areas, 'count' shows the number of triangles per pixel, and 'std' reveals value diversity within pixels. These provide different insights into the mesh structure and data distribution. ```python tf.Images(tf.shade(cvs.trimesh(verts, tris, mesh=mesh, agg=ds.any('z')), name='any'), tf.shade(cvs.trimesh(verts, tris, mesh=mesh, agg=ds.count()), name='count'), tf.shade(cvs.trimesh(verts, tris, mesh=mesh, agg=ds.std('z')), name='std')).cols(3) ``` -------------------------------- ### Datashade Multiple Trends with NdOverlay Source: https://datashader.org/user_guide/Timeseries Visualizes multiple time series trends using HoloViews NdOverlay and Datashader. Applies datashading with specific options for clarity in distinguishing groups and overlaps. ```python opts = hv.opts.RGB(width=600, height=300) ndoverlay = hv.NdOverlay({c:hv.Curve((dfm['Time'], dfm[c]), vdims=['Time']) for c in cols}) datashade(ndoverlay, cnorm='linear', aggregator=ds.count(), line_width=3).opts(opts) ``` -------------------------------- ### Generate Sample Data with Pandas and NumPy Source: https://datashader.org/getting_started/Pipeline Creates a Pandas DataFrame with 50,000 data points distributed across five Gaussian distributions. It sets up 'x', 'y', 'val', and 'cat' columns, with 'cat' as a categorical type. This data serves as an example for Datashader processing. ```python import pandas as pd import numpy as np num=10000 np.random.seed(1) dists = {cat: pd.DataFrame(dict([('x',np.random.normal(x,s,num)), ('y',np.random.normal(y,s,num)), ('val',val), ('cat',cat)])) for x, y, s, val, cat in [( 2, 2, 0.03, 10, "d1"), ( 2, -2, 0.10, 20, "d2"), ( -2, -2, 0.50, 30, "d3"), ( -2, 2, 1.00, 40, "d4"), ( 0, 0, 3.00, 50, "d5")] } df = pd.concat(dists,ignore_index=True) df["cat"]=df["cat"].astype("category") ``` -------------------------------- ### Import Libraries for Datashader Network Analysis Source: https://datashader.org/user_guide/Networks Imports necessary Python libraries for data manipulation, Datashader operations, and graph layout algorithms. This setup is crucial for all subsequent network visualization tasks. ```python import math import numpy as np import pandas as pd import datashader as ds import datashader.transfer_functions as tf from datashader.layout import random_layout, circular_layout, forceatlas2_layout from datashader.bundling import connect_edges, hammer_bundle from itertools import chain ``` -------------------------------- ### HoloViews Interactive Visualization with `dynspread` Source: https://datashader.org/getting_started/Interactivity Demonstrates chaining HoloViews operations for complex interactive visualizations, specifically using `hd.dynspread` to enhance the visibility of data points in a datashaded plot. This example also includes redimensioning the plot ranges. ```python import holoviews as hv import datashader as ds import pandas as pd import numpy as np hv.extension('bokeh') # Assuming 'points' is a HoloViews element or DataFrame # Example DataFrame creation (replace with your actual data) df = pd.DataFrame({'x': np.random.rand(100000) * 10 - 5, 'y': np.random.rand(100000) * 10 - 5, 'cat': np.random.choice(['A', 'B', 'C'], 100000)}) points = hv.Points(df, 'x', 'y', vdims='cat') datashaded = hv.datashade(points, aggregator=ds.count_cat('cat')).redim.range(x=(-5,5),y=(-5,5)) # To display interactively in a live notebook: # hv.render(hv.dynspread(datashaded, threshold=0.8, how='over', max_px=5).opts(height=500,width=500)) # For static export: hv.save(hv.dynspread(datashaded, threshold=0.8, how='over', max_px=5).opts(height=500,width=500), 'interactive_spread_plot.html') ``` -------------------------------- ### Plot Points with Datashader (Python) Source: https://datashader.org/user_guide/Trimesh Plots the generated points using Datashader's Canvas. It demonstrates plotting points colored by their 'z' value, representing the function's output at those locations, using cvs.points. ```python cvs = ds.Canvas(plot_height=400,plot_width=400) tf.Images(tf.shade(cvs.points(verts, 'x', 'y'), name='Points'), tf.shade(cvs.points(verts, 'x', 'y', agg=ds.mean('z')), name='PointsZ')) ``` -------------------------------- ### Load NYC Taxi Data and Libraries Source: https://datashader.org/getting_started/Introduction Imports necessary libraries (Datashader, Pandas, Colorcet) and loads taxi trip data from a CSV file, specifically selecting 'dropoff_x' and 'dropoff_y' columns. It then displays the first few rows of the DataFrame. ```python import datashader as ds, pandas as pd, colorcet as cc df = pd.read_csv('../data/nyc_taxi.csv', usecols=['dropoff_x', 'dropoff_y']) df.head() ``` -------------------------------- ### Render Trimesh with Datashader (Python) Source: https://datashader.org/user_guide/Trimesh Renders the triangulated data using Datashader's trimesh functionality, utilizing the pre-computed mesh. This provides a smoother representation of the function compared to simple point plots. ```python tf.shade(cvs.trimesh(verts, tris, mesh=mesh)) ``` -------------------------------- ### Shade Sum Aggregation for Trends (Python) Source: https://datashader.org/user_guide/Timeseries Uses the `.sum()` operator to aggregate data and `tf.shade` to visualize the combined patterns, revealing trends and overlap. Specifies `uint32` for transparent background handling. ```python total = tf.shade(merged.sum(dim='cols').astype('uint32'), how='linear') ``` -------------------------------- ### Create Interactive Datashader Plot with HoloViews Source: https://datashader.org/user_guide/Trajectories Builds an interactive visualization by applying the `datashade` and `spread` operations to a HoloViews `Path` object created from the trajectory DataFrame. This allows for interactive zooming and exploration of the data. ```python opts = hv.opts.RGB(width=900, height=500, aspect='equal') spread(datashade(hv.Path(df, kdims=['x','y']), cnorm='linear', aggregator=ds.any())).opts(opts) ``` -------------------------------- ### Create Mesh Data Structure (Python) Source: https://datashader.org/user_guide/Trimesh Pre-computes the mesh data structure by combining vertices and triangles using 'du.mesh'. This can significantly speed up plotting for large meshes by reusing computed data. ```python %time mesh = du.mesh(verts,tris) ``` -------------------------------- ### Zoomed Static Datashader Plots Source: https://datashader.org/user_guide/Trajectories Shows how to create zoomed-in static plots of the trajectory data by specifying different `x_range` and `y_range` values in the `create_image` function. This reveals finer details like wobble and noise. ```python create_image(x_range=(95.0,101.0), y_range=(-61,-55)) ``` ```python create_image(x_range=(98.8,99.8), y_range=(-60.2,-59.2)) ``` -------------------------------- ### Define Sine-Based Function with Varying Frequency (Python) Source: https://datashader.org/user_guide/Trimesh Defines a function 'f' that generates sine waves with frequencies dependent on the radial distance. It uses NumPy for numerical operations and handles conditions where the radius is outside a specified range by returning NaN. ```python rad = 0.05,1.0 def f(x,y): rsq = x**2+y**2 return np.where(np.logical_or(rsqrad[1]), np.nan, np.sin(10/rsq)) ``` -------------------------------- ### Generate and Prepare Time Series Data for Multiple Trends Source: https://datashader.org/user_guide/Timeseries Creates synthetic time series data with three distinct groups, each containing noisy samples. It then prepares this data into a Pandas DataFrame, incorporating a 'Time' column. ```python signals = [np.random.normal(0, 0.3, size=n).cumsum() + 50, np.random.normal(0, 0.3, size=n).cumsum() + 50, np.random.normal(0, 0.3, size=n).cumsum() + 50] data = {c: signals[i%3] + noise(1+i, 5*(np.random.random() - 0.5), n) for (i,c) in enumerate(cols)} y_range = (1.2*min([s.min() for s in signals]), 1.2*max([s.max() for s in signals])) data['Time'] = df['Time'] dfm = pd.DataFrame(data) ``` -------------------------------- ### Enable Hover Info on Datashader Plots with HoloViews Source: https://datashader.org/getting_started/Interactivity Shows how to add hover information to Datashader plots using HoloViews. It includes examples for both rasterized plots and aggregated data displayed as QuadMeshes, demonstrating fixed and dynamic hover behaviors. ```python from holoviews.streams import RangeXY pts = hd.datashade(points, width=400, height=400) rasterized = hd.rasterize(points).opts(tools=['hover']) quadmesh = hv.QuadMesh(hd.aggregate(points, width=12, height=12, dynamic=False)) \ .opts(tools=['hover'], alpha=0, hover_alpha=0.2) dynamic = hv.util.Dynamic(hd.aggregate(points, width=12, height=12, streams=[RangeXY]), operation=hv.QuadMesh) \ .opts(tools=['hover'], alpha=0, hover_alpha=0.2) rasterized.relabel("Pixel hover").opts(cmap=["lightblue","darkblue"], cnorm="eq_hist") + \ (pts * quadmesh).relabel("Fixed square hover") + \ (pts * dynamic).relabel("Dynamic square hover") ``` -------------------------------- ### Coloring and Stacking Multiple Curves with Datashader Source: https://datashader.org/user_guide/Timeseries This code demonstrates visualizing multiple curves by assigning unique colors to each and stacking the resulting shaded images. It iterates through a list of predefined colors, shades each curve's aggregation with a single color, and then stacks these images for comparison. This helps in identifying individual curves within a complex dataset. ```python colors = ["red", "grey", "black", "purple", "pink", "yellow", "brown", "green", "orange", "blue"] imgs = [tf.shade(aggs[i], cmap=[c]) for i, c in zip(cols, colors)] tf.stack(*imgs) ``` -------------------------------- ### Aggregate and Shade Taxi Trip Data Source: https://datashader.org/getting_started/Introduction Aggregates the taxi trip data points using Datashader's Canvas and then shades the aggregated data with a specified colormap (cc.fire), setting a black background for the output image. ```python agg = ds.Canvas().points(df, 'dropoff_x', 'dropoff_y') ds.tf.set_background(ds.tf.shade(agg, cmap=cc.fire), "black") ``` -------------------------------- ### Parallelizing Trimesh Aggregation with Dask DataFrames Source: https://datashader.org/user_guide/Trimesh Shows how to parallelize Datashader's trimesh aggregation using Dask DataFrames. This involves converting pandas DataFrames to Dask DataFrames, creating a partitioned mesh using `du.mesh`, and then passing this partitioned mesh to `cvs.trimesh`. Note that `du.mesh` itself is not parallelized and requires loading all vertex and triangle data into memory. ```python verts_ddf = dd.from_pandas(verts, npartitions=4) tris_ddf = dd.from_pandas(tris, npartitions=4) mesh_ddf = du.mesh(verts_ddf, tris_ddf) mesh_ddf ``` ```python tf.shade(cvs.trimesh(verts_ddf, tris_ddf, mesh=mesh_ddf)) ``` -------------------------------- ### Interactive Datashader Plot with HoloViews Source: https://datashader.org/getting_started/Introduction Integrates Datashader with HoloViews and Bokeh to create an interactive map overlay of taxi trip data. It loads geographic map tiles, creates a HoloViews Points element from the taxi data, applies Datashader's 'datashade' operation for efficient rendering, and overlays it on the map tiles. ```python import holoviews as hv from holoviews.element.tiles import EsriImagery from holoviews.operation.datashader import datashade hv.extension('bokeh') map_tiles = EsriImagery().opts(alpha=0.5, width=900, height=480, bgcolor='black') points = hv.Points(df, ['dropoff_x', 'dropoff_y']) taxi_trips = datashade(points, x_sampling=1, y_sampling=1, cmap=cc.fire, width=900, height=480) map_tiles * taxi_trips ``` -------------------------------- ### Generate Random Walk Data with NumPy Source: https://datashader.org/user_guide/Timeseries Generates a NumPy array for 100,000 random walks, each with 10 points. This data simulates time series data suitable for Datashader's large-scale plotting capabilities. It uses `np.cumsum` for the walk and `np.random.randn` for step sizes. ```python n = 100000 points = 10 time = np.linspace(0, 1, points) data = np.cumsum(np.c_[np.zeros((n,1)), np.random.randn(n, points)] , axis=1) data.shape ``` -------------------------------- ### Datashader: where max reduction to get row index Source: https://datashader.org/user_guide/Inspection_Reductions Example of using `ds.where(ds.max('value'))` with `canvas.points` to get the row index of the maximum value for each pixel. If no data maps to a pixel, an index of -1 is returned. ```python canvas.points(df, 'x', 'y', ds.where(ds.max('value'))) ``` -------------------------------- ### Downsampling and Plotting Data with Datashader Source: https://datashader.org/user_guide/Timeseries This snippet illustrates downsampling a dataset by a factor of 10 and then plotting the reduced dataset using Datashader's line aggregator. It creates a mask to select every tenth data point and then shades the line plot of the selected data, highlighting the impact of sampling on visualization. ```python mask = (df.index % 10) == 0 tf.shade(cvs.line(df[mask][['a','ITime']], 'ITime', 'a')) ``` -------------------------------- ### Datashader Image Rendering with Various Aggregations Source: https://datashader.org/getting_started/Pipeline Renders images from a Datashader canvas using different aggregation functions (count, any, mean) and shading. It demonstrates how to visualize the distribution, presence, and average values of data points within bins. The last example shows a transformation (50 - mean('val')) applied before shading. ```python tf.Images(tf.shade(canvas.points(df, 'x', 'y', ds.count()), name="count()"), tf.shade(canvas.points(df, 'x', 'y', ds.any()), name="any()"), tf.shade(canvas.points(df, 'x', 'y', ds.mean('y')), name="mean('y')"), tf.shade(50 - canvas.points(df, 'x', 'y', ds.mean('val')), name="50- mean('val')")) ``` -------------------------------- ### Datashader: where first reduction to get row index Source: https://datashader.org/user_guide/Inspection_Reductions Example of using `ds.where(ds.first('value'))` with `canvas.points` to get the row index of the first value encountered for each pixel. Pixels without data receive an index of -1. ```python canvas.points(df, 'x', 'y', ds.where(ds.first('value'))) ``` -------------------------------- ### Datashader: where first_n reduction to get row indices Source: https://datashader.org/user_guide/Inspection_Reductions Example of using `ds.where(ds.first_n('value', 3))` with `canvas.points` to retrieve the row indices of the first 3 values mapped to each pixel. Pixels with fewer than 3 data points will have -1 for the remaining indices. ```python canvas.points(df, 'x', 'y', ds.where(ds.first_n('value', 3))) ``` -------------------------------- ### Generate Sample Data with Pandas and NumPy Source: https://datashader.org/getting_started/Interactivity Creates a sample dataset using Pandas and NumPy for demonstration. It generates multiple distributions of 'x' and 'y' coordinates, along with associated 'val' and 'cat' values, simulating a complex dataset suitable for Datashader. ```python import pandas as pd import numpy as np import datashader as ds import datashader.transfer_functions as tf num=100000 np.random.seed(1) dists = {cat: pd.DataFrame(dict([('x',np.random.normal(x,s,num)), ('y',np.random.normal(y,s,num)), ('val',val), ('cat',cat)])) for x, y, s, val, cat in [( 2, 2, 0.03, 10, "d1"), ( 2, -2, 0.10, 20, "d2"), ( -2, -2, 0.50, 30, "d3"), ( -2, 2, 1.00, 40, "d4"), ( 0, 0, 3.00, 50, "d5")] } df = pd.concat(dists,ignore_index=True) df["cat"]=df["cat"].astype("category") ``` -------------------------------- ### Initialize HoloViews and Datashader Source: https://datashader.org/user_guide/Plotting_Pitfalls Sets up the HoloViews environment, imports necessary components, and configures the default colormap for datashading using the 'fire' colormap from colorcet. ```python import numpy as np np.random.seed(42) import holoviews as hv from holoviews.operation.datashader import datashade from holoviews import opts, dim hv.extension('matplotlib') from colorcet import fire datashade.cmap=fire[50:] ``` -------------------------------- ### Datashader Edge Bundling API Source: https://datashader.org/api Documentation for edge bundling functionalities in Datashader. ```APIDOC ## Function directly_connect_edges ### Description Alias for `connect_edges`. ## Function hammer_bundle ### Description Iteratively group edges and return as paths suitable for datashading. ### Parameters - **accuracy** (float) - The accuracy of the bundling. - **** (any) - Additional keyword arguments. ``` -------------------------------- ### Import HoloViews and Datashader Operations Source: https://datashader.org/getting_started/Interactivity Imports necessary HoloViews and Datashader modules, sets a colormap for shading, and enables both Bokeh and Matplotlib extensions for HoloViews. This prepares the environment for creating interactive and static visualizations. ```python import holoviews as hv import holoviews.operation.datashader as hd hd.shade.cmap=["lightblue", "darkblue"] hv.extension("bokeh", "matplotlib") ``` -------------------------------- ### Using HoloViews with Datashader for Plotting Source: https://datashader.org/FAQ Demonstrates how HoloViews can be used to easily construct plots with server-side rendering from Datashader, simplifying the process of visualizing large datasets. HoloViews leverages Bokeh or Plotly for rendering. ```python import holoviews as hv import datashader as ds hv.extension('bokeh') df = # ... your large pandas DataFrame ... # Example: Using rasterize and shade for Datashader rendering with HoloViews # This approach allows HoloViews to handle colormapping and colorbars. plot = hv.Points(df).opts(tools=['hover', 'pan', 'wheel_zoom'], responsive=True) # For server-side rendering with datashader: # Use rasterize() to aggregate data, then let HoloViews handle shading and color mapping. # This is preferred for colorbars compared to datashade(). # For example: # hv.Points(df).opts(hv.opts.Points(tools=['hover'], datashade=True, cmap='viridis', width=600, height=400)) ``` -------------------------------- ### Create and Datashade an NdOverlay of Curves Source: https://datashader.org/user_guide/Timeseries Generates an NdOverlay of curves from a DataFrame, applying datashading with linear normalization and count aggregation. Configures plot options for width and height. ```python opts = hv.opts.RGB(width=600, height=300) ndoverlay = hv.NdOverlay({c:hv.Curve((df['Time'], df[c]), kdims=['Time'], vdims=['Value']) for c in cols}) datashade(ndoverlay, cnorm='linear', aggregator=ds.count(), line_width=2).opts(opts) ``` -------------------------------- ### Datashader: Set Background and Combine Images Source: https://datashader.org/getting_started/Pipeline Shows how to set a background color for an image and combine images using Datashader's tf.set_background and tf.stack functions. It illustrates different composition methods like 'saturate' for combining image layers. ```python sum_d2_d3 = aggc.sel(cat=['d2', 'd3']).sum(dim='cat').astype('uint32') tf.Images(tf.set_background(img,"black", name="Black bg"), tf.stack(img,tf.shade(sum_d2_d3), name="Sum d2 and d3 colors"), tf.stack(img,tf.shade(sum_d2_d3), name="d2+d3 saturated", how='saturate')) ``` -------------------------------- ### Calculate Plotting Ranges for Timeseries Source: https://datashader.org/user_guide/Timeseries Determines the minimum and maximum values for the x-axis (integer representation of time) and y-axis (signal values) to set appropriate plotting ranges. ```python # Default plot ranges: x_range = (df.iloc[0].ITime, df.iloc[-1].ITime) y_range = (1.2*signal.min(), 1.2*signal.max()) print("x_range: {0} y_range: {1}".format(x_range,y_range)) ``` -------------------------------- ### Convert Datetime to Integer for Datashader Source: https://datashader.org/user_guide/Timeseries Converts the 'Time' column from datetime objects to a 64-bit integer format, as the native Datashader API does not directly support datetime objects. ```python df['ITime'] = pd.to_datetime(df['Time']).astype('int64') ``` -------------------------------- ### HoloViews Datashader Bokeh Integration Source: https://datashader.org/getting_started/Interactivity Illustrates using HoloViews with Datashader and the Bokeh backend for interactive visualizations. The `hd.datashade` operation is shown to dynamically call Datashader on zoom and pan events, enabling interactive exploration of large datasets in a live notebook environment. ```python import holoviews as hv import datashader as ds import pandas as pd import numpy as np hv.extension('bokeh') # Assuming 'points' is a HoloViews element or DataFrame # Example DataFrame creation (replace with your actual data) df = pd.DataFrame({'x': np.random.rand(100000) * 10 - 5, 'y': np.random.rand(100000) * 10 - 5}) points = hv.Points(df, 'x', 'y') hv.save(ds.datashade(points), 'interactive_plot.html') # For static export # In a live notebook, simply calling datashade on the element enables interactivity: # hv.render(ds.datashade(points)) # This would display interactively in a live notebook ``` -------------------------------- ### Datashader Raster Data Support Source: https://datashader.org/releases Datashader version 0.3.0 introduced support for raster data through the `Canvas.raster` method. This functionality requires the `rasterio` and `scikit-image` libraries to be installed. ```python # Example usage of Canvas.raster (requires rasterio and scikit-image) from datashader.colors import Greys from datashader.transfer_functions import shade, stack, interpolate from datashader.colors import Hot import datashader as ds c = ds.Canvas() agg = c.raster(filename='path/to/your/raster/file.tif') img = shade(agg, cmap=Hot) ``` -------------------------------- ### Tune Edge Bundling Parameters Source: https://datashader.org/user_guide/Networks This code snippet iterates through different `decay` and `initial_bandwidth` parameters for the `hammer_bundle` function to explore their effect on the bundling scale. It generates a grid of visualizations for each parameter combination. ```python %%time grid = [graphplot(snodes, hammer_bundle(*star, iterations=5, decay=decay, initial_bandwidth=bw), "d={:0.2f}, bw={:0.2f}".format(decay, bw)) for decay in [0.1, 0.25, 0.5, 0.9] for bw in [0.1, 0.2, 0.5, 1]] tf.Images(*grid).cols(4) ``` -------------------------------- ### Datashader Canvas Trimesh Rendering Source: https://datashader.org/releases Introduces support for rendering irregular triangular meshes using the `Canvas.trimesh()` method. This feature is part of a major release and is detailed in the user guide. ```python from datashader.colors import viridis from datashader.layout import random_layout from datashader.transfer_functions import shade, stack from datashader.bundling import connect_edges, hammer import datashader as ds import pandas as pd import networkx as nx # Example usage of Canvas.trimesh() would go here, but is not provided in the text. ``` -------------------------------- ### Visualize Star Graph with Bundling Source: https://datashader.org/user_guide/Networks Renders a star graph using Datashader, comparing a direct edge connection visualization with one using the edge bundling algorithm. This highlights how bundling groups nearby connections. ```python tf.Images(graphplot(snodes, connect_edges(*star),"Star"), graphplot(snodes, hammer_bundle(*star),"Star bundled")) ``` -------------------------------- ### Plot polygon centroids with Datashader Source: https://datashader.org/user_guide/Polygons Plots polygon centroids as points using Datashader. It uses `ds.Canvas` and `cvs.points` for aggregation and `tf.spread` and `tf.shade` for rendering. The example aggregates by the mean of 'cty_pop200'. ```python # Discard the output to avoid measuring Numba compilation times cvs.points(sgeodf, geometry='centroid', agg=ds.mean('cty_pop200')) ``` ```python %%time agg = cvs.points(sgeodf, geometry='centroid', agg=ds.mean('cty_pop200')) tf.spread(tf.shade(agg), 2) ``` -------------------------------- ### Delaunay Triangulation Function Source: https://datashader.org/user_guide/Trimesh A Python function that performs Delaunay triangulation on a set of vertices to create a triangular mesh. It takes a Pandas DataFrame of vertices and returns a DataFrame of triangle indices. ```python def triangulate(vertices, x="x", y="y"): """ Generate a triangular mesh for the given x,y,z vertices, using Delaunay triangulation. For large n, typically results in about double the number of triangles as vertices. """ triang = Delaunay(vertices[[x,y]].values) print('Given', len(vertices), "vertices, created", len(triang.simplices), 'triangles.') return pd.DataFrame(triang.simplices, columns=['v0', 'v1', 'v2']) ```