### Run Jupyter Notebooks Source: https://github.com/holoviz/datashader/blob/main/examples/README.md Navigate to the examples directory and start the Jupyter notebook server to access the examples. ```bash cd datashader-examples jupyter notebook ``` -------------------------------- ### Install Datashader Examples with Conda Source: https://github.com/holoviz/datashader/blob/main/examples/README.md Use these commands to create a Conda environment, activate it, and copy/download Datashader examples and their data. ```bash 1. conda env create --file environment.yml 2. conda activate ds 3. datashader examples 3. cd datashader-examples ``` -------------------------------- ### Fetch Datashader Examples Source: https://github.com/holoviz/datashader/blob/main/doc/getting_started/index.rst Fetch all example code and data for Datashader. This creates a new directory named 'datashader-examples'. ```bash datashader examples cd datashader-examples ``` -------------------------------- ### Start HTTP Server for Tile Preview Source: https://github.com/holoviz/datashader/blob/main/examples/tiling.ipynb Use Python's built-in http.server to serve tile output files locally. Navigate to the tile output directory before starting the server. ```bash cd test_tiles_output python -m http.server ``` -------------------------------- ### Install Datashader with Pip Source: https://github.com/holoviz/datashader/blob/main/doc/getting_started/index.rst Use pip to install Datashader. This is an alternative to using conda. ```bash pip install datashader ``` -------------------------------- ### Install Required Libraries Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/1_Plotting_Pitfalls.ipynb Installs HoloViews, colorcet, matplotlib, and optionally scikit-image using conda. These libraries are necessary for the plotting examples. ```bash conda install holoviews colorcet matplotlib scikit-image ``` -------------------------------- ### Install Boto3 for S3 Integration Source: https://github.com/holoviz/datashader/blob/main/examples/tiling.ipynb Install the boto3 library, which is required for rendering tiles directly to Amazon S3. This is a conda installation command. ```bash conda install boto3 ``` -------------------------------- ### Publish and Preview Benchmark Results Source: https://github.com/holoviz/datashader/blob/main/benchmarks/README.md Publish benchmark results and start a local webserver to preview them in a browser. This allows for interactive exploration of benchmark performance. ```bash asv publish asv preview ``` -------------------------------- ### Install ASV with Virtualenv Source: https://github.com/holoviz/datashader/blob/main/benchmarks/README.md Install ASV version 0.4.2 and virtualenv using pip. This is the recommended method if you are using a virtualenv environment. ```bash pip install asv==0.4.2 virtualenv ``` -------------------------------- ### Create and Activate New Conda Environment for Examples Source: https://github.com/holoviz/datashader/blob/main/doc/getting_started/index.rst If you did not install Datashader within an existing conda environment, create a new one named 'datashader' using 'environment.yml' and then activate it. ```bash conda env create --name datashader --file environment.yml conda activate datashader ``` -------------------------------- ### Install Datashader Pre-release from Dev Channel Source: https://github.com/holoviz/datashader/blob/main/README.md Install the latest pre-release versions of Datashader from the dev-labelled conda channel. ```bash conda install -c pyviz/label/dev datashader ``` -------------------------------- ### Render Tiles to Local Filesystem Source: https://github.com/holoviz/datashader/blob/main/examples/tiling.ipynb Demonstrates rendering tiles to a local directory using the previously defined component functions. This example renders tiles for zoom levels 0, 1, and 2. ```python full_extent_of_data = (-500000, -500000, 500000, 500000) output_path = 'tiles_output_directory/wald_tiles' results = render_tiles(full_extent_of_data, range(3), load_data_func=load_data_func, rasterize_func=rasterize_func, shader_func=shader_func, post_render_func=post_render_func, output_path=output_path) ``` -------------------------------- ### Install Datashader with Conda Source: https://github.com/holoviz/datashader/blob/main/README.md Install Datashader using conda. For best performance, conda is recommended for optimized numerical libraries. ```bash conda install datashader ``` -------------------------------- ### Install ASV with Conda Source: https://github.com/holoviz/datashader/blob/main/benchmarks/README.md Install ASV version 0.4.2 using conda. This is the recommended method if you are using a conda environment. ```bash conda install -c conda-forge asv==0.4.2 ``` -------------------------------- ### Install Datashader from PyViz Channel Source: https://github.com/holoviz/datashader/blob/main/README.md Install the latest releases of Datashader from the pyviz conda channel. ```bash conda install -c pyviz datashader ``` -------------------------------- ### Create post_render_func Example Source: https://github.com/holoviz/datashader/blob/main/examples/tiling.ipynb Provides an example `post_render_func` that adds text information (x, y, z coordinates) to the output PIL Image using ImageDraw. This function is executed for each final tile before it is saved. ```python from PIL import ImageDraw def post_render_func(img, **kwargs): info = "x={},y={},z={}".format(kwargs['x'], kwargs['y'], kwargs['z']) draw = ImageDraw.Draw(img) draw.text((5, 5), info, fill='rgb(255, 255, 255)') return img ``` -------------------------------- ### Update Conda Environment for Examples Source: https://github.com/holoviz/datashader/blob/main/doc/getting_started/index.rst If Datashader was installed within a conda environment, activate that environment and run this command to update it with example dependencies from 'environment.yml'. ```bash conda env update --file environment.yml ``` -------------------------------- ### Multiple Data Transformations for Colormapping Source: https://github.com/holoviz/datashader/blob/main/examples/getting_started/2_Pipeline.ipynb This example demonstrates creating multiple shaded images using different transformation methods ('log', 'eq_hist', and a custom 23rd root) to visualize data structure effectively. It uses tf.Images to combine these visualizations. ```python tf.Images(tf.shade(agg,how='log', name="log"), tf.shade(agg,how='eq_hist', name="eq_hist"), tf.shade(agg,how=lambda d, m: np.where(m, np.nan, d)**(1/23.), name="23rd root")) ``` -------------------------------- ### Install Datashader with Conda Source: https://github.com/holoviz/datashader/blob/main/doc/getting_started/index.rst Use conda to install Datashader for optimized numerical libraries. For the latest releases, use the pyviz channel. For pre-release versions, use the dev-labeled channel. ```bash conda install datashader ``` ```bash conda install -c pyviz datashader ``` ```bash conda install -c pyviz/label/dev datashader ``` -------------------------------- ### Render Tiles to S3 Source: https://github.com/holoviz/datashader/blob/main/examples/tiling.ipynb Render tiles and specify an S3 bucket path for the output. This requires AWS credentials to be configured and the boto3 library to be installed. Tiles written to S3 are marked with 'public-read' ACL. ```python full_extent_of_data = (int(-20e6), int(-20e6), int(20e6), int(20e6)) output_path = 's3://datashader-tiles-testing/wald_tiles/' try: results = render_tiles(full_extent_of_data, range(3), load_data_func=load_data_func, rasterize_func=rasterize_func, shader_func=shader_func, post_render_func=post_render_func, output_path=output_path) except ImportError: print('you must install boto3 to save tiles to Amazon S3') ``` -------------------------------- ### Import Libraries for Datashader Graph Visualization Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/7_Networks.ipynb Imports essential libraries including numpy, pandas, datashader, and specific layout and bundling functions. Ensure these are installed before running. ```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 ``` -------------------------------- ### Datashader Plotting with ImageGrid and Customizations Source: https://github.com/holoviz/datashader/blob/main/examples/getting_started/3_Interactivity.ipynb Demonstrates creating a grid of plots using Matplotlib's `ImageGrid` and `dsshow`. This example shows various combinations of aggregators, normalization methods (`linear`, `log`), colormaps, and alpha-based colormaps. ```python from mpl_toolkits.axes_grid1 import ImageGrid fig = plt.figure(figsize=(9, 9)) # Here, we create a grid of axes using ImageGrid # https://matplotlib.org/3.1.0/gallery/axes_grid1/demo_axes_grid.html grid = ImageGrid(fig, 111, nrows_ncols=(2, 2), axes_pad=0.5, share_all=True, cbar_location="right", cbar_mode="each", cbar_size="5%", cbar_pad="2%") artist0 = dsshow(df, ds.Point('x', 'y'), ds.count(), vmax=1000, aspect='equal', ax=grid[0]) plt.colorbar(artist0, cax=grid.cbar_axes[0]); grid[0].set_title('Point density (linear scale, clipped)'); artist1 = dsshow(df, ds.Point('x', 'y'), ds.mean('val'), cmap='inferno', aspect='equal', ax=grid[1]) plt.colorbar(artist1, cax=grid.cbar_axes[1]); grid[1].set_title('Mean point value (linear scale)'); dsblue=['lightblue', 'darkblue'] artist2 = dsshow(df, ds.Point('x', 'y'), ds.count(), norm='log', cmap=dsblue, aspect='equal', ax=grid[2]) plt.colorbar(artist2, cax=grid.cbar_axes[2]); grid[2].set_title('Point density (log scale)'); artist3 = dsshow(df, ds.Point('x', 'y'), norm='log', cmap=alpha_colormap('#ffffff', 40, 255), ax=grid[3]) grid[3].set_facecolor('#000000'); grid.cbar_axes[3].set_visible(False); grid[3].set_title('Point density (log, alpha-based colormap)'); fig ``` -------------------------------- ### Import Datashader Matplotlib Extensions Source: https://github.com/holoviz/datashader/blob/main/examples/getting_started/3_Interactivity.ipynb Import necessary modules from Matplotlib and Datashader's Matplotlib extension. Ensure `ipympl` is installed for `%matplotlib widget` interactivity. ```python import matplotlib.pyplot as plt from datashader.mpl_ext import dsshow, alpha_colormap ``` -------------------------------- ### Create shader_func Example Source: https://github.com/holoviz/datashader/blob/main/examples/tiling.ipynb Defines a `shader_func` that takes an aggregated xarray DataArray and applies Datashader's transfer functions to create a shaded image. It uses a reversed viridis colormap and sets the background to black. ```python import datashader as ds import datashader.transfer_functions as tf from datashader.colors import viridis def shader_func(agg, span=None): img = tf.shade(agg, cmap=reversed(viridis), span=span, how='log') img = tf.set_background(img, 'black') return img ``` -------------------------------- ### 3D Reduction with Cyclic Categorizer Source: https://github.com/holoviz/datashader/blob/main/examples/getting_started/2_Pipeline.ipynb Create a 3D aggregate using a cyclic categorizer for cyclical data like day of the week. This example demonstrates averaging 'x' values binned by 'val' modulo 7. ```python modulo_categorizer = ds.category_modulo('val', modulo=7, offset=0) agg3D_modulo = canvas.points(df, 'x', 'y', ds.by(modulo_categorizer, ds.mean('x'))) agg3D_modulo ``` -------------------------------- ### Interactive Visualization with `dynspread` Source: https://github.com/holoviz/datashader/blob/main/examples/getting_started/3_Interactivity.ipynb Chain HoloViews operations for complex interactive visualizations. This example uses `hd.datashade` with a categorical aggregator and `hd.dynspread` to enhance visibility of sparse data. ```python datashaded = hd.datashade(points, aggregator=ds.count_cat('cat')).redim.range(x=(-5,5),y=(-5,5)) hd.dynspread(datashaded, threshold=0.8, how='over', max_px=5).opts(height=500,width=500) ``` -------------------------------- ### Import necessary libraries for Datashader and spatialpandas Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/8_Polygons.ipynb Imports required for working with Datashader, Dask, colorcet, and spatialpandas, including its geometry and dask modules. Ensure spatialpandas is installed. ```python import pandas as pd import numpy as np import dask.dataframe as dd import colorcet as cc import datashader as ds import datashader.transfer_functions as tf import spatialpandas as sp import spatialpandas.geometry import spatialpandas.dask ``` -------------------------------- ### Generate Gaussian Distributions for Plotting Examples Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/1_Plotting_Pitfalls.ipynb Defines a function to generate points from 2D Gaussian distributions, used for creating sample datasets for plotting. It allows specifying the number of points and the parameters of the Gaussian distributions. ```python def gaussians(specs=[(1.5,0,1.0),(-1.5,0,1.0)],num=100): """ A concatenated list of points taken from 2D Gaussian distributions. Each distribution is specified as a tuple (x,y,s), where x,y is the mean and s is the standard deviation. Defaults to two horizontally offset unit-mean Gaussians. """ np.random.seed(1) dists = [(np.random.normal(x,s,num), np.random.normal(y,s,num)) for x,y,s in specs] return np.hstack([d[0] for d in dists]), np.hstack([d[1] for d in dists]) points = (hv.Points(gaussians(num=600), label="600 points", group="Small dots") + hv.Points(gaussians(num=60000), label="60000 points", group="Small dots") + hv.Points(gaussians(num=600), label="600 points", group="Tiny dots") + hv.Points(gaussians(num=60000), label="60000 points", group="Tiny dots")) points.opts( opts.Points('Small_dots', s=1, alpha=1), opts.Points('Tiny_dots', s=0.1, alpha=0.1)) ``` -------------------------------- ### Create load_data_func Example Source: https://github.com/holoviz/datashader/blob/main/examples/tiling.ipynb Defines a `load_data_func` that generates a pandas DataFrame with 'x' and 'y' coordinates sampled from a Wald distribution, filtering data within specified ranges. This function is used to load data for tile rendering. ```python import pandas as pd import numpy as np df = None def load_data_func(x_range, y_range): global df if df is None: xoffsets = [-1, 1, -1, 1] yoffsets = [-1, 1, 1, -1] xs = np.concatenate([np.random.wald(10000000, 10000000, size=10000000) * offset for offset in xoffsets]) ys = np.concatenate([np.random.wald(10000000, 10000000, size=10000000) * offset for offset in yoffsets]) df = pd.DataFrame(dict(x=xs, y=ys)) return df.loc[df['x'].between(*x_range) & df['y'].between(*y_range)] ``` -------------------------------- ### Illustrate Six Plotting Pitfalls with HoloViews Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/1_Plotting_Pitfalls.ipynb Combines examples of six common plotting pitfalls (overplotting, oversaturation, undersampling, undersaturation, underutilized dynamic range, and nonuniform colormapping) into a single HoloViews layout for comparison. This helps visualize the systematic misrepresentation of data distributions. ```python layout = (hv.Points(dist,label="1. Overplotting") + hv.Points(dist,label="2. Oversaturation").opts(s=0.1,alpha=0.5) + hv.Points((dist[0][::200],dist[1][::200]),label="3. Undersampling").opts(s=2,alpha=0.5) + hv.Points(dist,label="4. Undersaturation").opts(s=0.01,alpha=0.05) + heatmap(dist,200,offset=0.2,label="5. Underutilized dynamic range") + heatmap(dist,200,transform=eq_hist,label="6. Nonuniform colormapping").opts(cmap="hot")) layout.opts( opts.Points(axiswise=False), opts.Layout(sublabel_format="", tight=True)).cols(3) ``` -------------------------------- ### Run All Benchmarks Source: https://github.com/holoviz/datashader/blob/main/benchmarks/README.md Execute all benchmarks defined in the project against the default 'main' branch. This command will create a machine file on the first run and set up virtual environments for each benchmark. ```bash cd benchmarks asv run ``` -------------------------------- ### Set Background and Combine Images Source: https://github.com/holoviz/datashader/blob/main/examples/getting_started/2_Pipeline.ipynb Demonstrates setting a background color and combining images using different composition operators like 'saturate'. Requires pre-defined aggregate data. ```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')) ``` -------------------------------- ### Initialize HoloViews and Datashader Extension Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/3_Timeseries.ipynb Imports necessary libraries and sets up the Bokeh extension for HoloViews. Adjusts PlotSize for better rendering. ```python import holoviews as hv from holoviews.operation.datashader import datashade from holoviews.streams import PlotSize PlotSize.scale=2 hv.extension('bokeh') ``` -------------------------------- ### Run Benchmarks in Default Development Environment Source: https://github.com/holoviz/datashader/blob/main/benchmarks/README.md Execute all benchmarks using your current Python environment, ensuring compatibility with libraries like cuDF and Dask-cuDF. The '--launch-method spawn' is recommended for GPU access from subprocesses. ```bash asv run --python=same --launch-method spawn ``` -------------------------------- ### Create Sample Pandas DataFrame Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/12_Inspection_Reductions.ipynb Creates a sample pandas DataFrame with 'x', 'y', 'value', and 'other' columns for testing Datashader reductions. ```python import datashader as ds import pandas as pd df = pd.DataFrame(dict( x = [ 0, 0, 1, 1, 0, 0, 2, 2], y = [ 0, 0, 0, 0, 1, 1, 1, 1], value = [ 9, 8, 7, 6, 2, 3, 4, 5], other = [11, 12, 13, 14, 15, 16, 17, 18], )) ``` -------------------------------- ### Import Datashader Render Tiles Source: https://github.com/holoviz/datashader/blob/main/examples/tiling.ipynb Imports the `render_tiles` utility function from Datashader. ```python from datashader.tiles import render_tiles ``` -------------------------------- ### Install Datashader in Editable Mode Source: https://github.com/holoviz/datashader/blob/main/doc/getting_started/index.rst Add the cloned Datashader directory to the Python path in the active conda environment for development. This installs Datashader without dependencies. ```bash pip install --no-deps -e . ``` -------------------------------- ### Show Benchmark Timings Source: https://github.com/holoviz/datashader/blob/main/benchmarks/README.md Display the benchmark timings that have been stored for the 'main' branch. This command lists the results of previously run benchmarks. ```bash asv show main ``` -------------------------------- ### Dataset-Dependent Alpha Settings Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/1_Plotting_Pitfalls.ipynb The optimal alpha value is dataset-dependent. This example shows how changing the number of points can affect the visualization even with the same alpha setting. ```python blues, reds = points(pts=600, s=1), points(pts=600, s=-1) layout = blues + reds + (reds * blues) + (blues * reds) layout.opts(opts.Points(alpha=0.1)) ``` -------------------------------- ### HoloViews Trimesh Initialization Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/6_Trimesh.ipynb Initializes HoloViews with the Bokeh extension and imports the datashade operation. This is boilerplate for creating interactive plots. ```Python import holoviews as hv from holoviews.operation.datashader import datashade hv.extension("bokeh") ``` -------------------------------- ### Quickly Run Benchmarks for Error Checking Source: https://github.com/holoviz/datashader/blob/main/benchmarks/README.md Run all benchmarks once without extensive statistical validation. Use this command to quickly check for errors or basic functionality. ```bash asv dev ``` -------------------------------- ### Configure ASV to Benchmark Multiple Branches Source: https://github.com/holoviz/datashader/blob/main/benchmarks/README.md Modify the 'asv.conf.json' file to include multiple branches for benchmarking. This allows comparison of performance between different versions of the code. ```json "branches": ["main"], ``` ```json "branches": ["main", "new_feature_branch"], ``` -------------------------------- ### Create rasterize_func Example Source: https://github.com/holoviz/datashader/blob/main/examples/tiling.ipynb Implements a `rasterize_func` that uses Datashader's Canvas to aggregate points from a DataFrame into an xarray DataArray, based on provided ranges and dimensions. This is used for creating the aggregate grid for tiles. ```python import datashader as ds def rasterize_func(df, x_range, y_range, height, width): # aggregate cvs = ds.Canvas(x_range=x_range, y_range=y_range, plot_height=height, plot_width=width) agg = cvs.points(df, 'x', 'y') return agg ``` -------------------------------- ### Datashade Overlapping Curves Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/3_Timeseries.ipynb Generates an interactive plot of potentially overlapping timeseries curves using Datashader. Useful for identifying 'rogue lines' or outliers in dense datasets. Requires HoloViews and Datashader setup. ```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 Pipeline: Aggregation, Transformation, and Shading Source: https://github.com/holoviz/datashader/blob/main/examples/getting_started/2_Pipeline.ipynb Demonstrates creating a canvas, aggregating points, spreading pixels for visibility, and shading the aggregate with different histogram equalization methods. Use `rescale_discrete_levels=True` for better visibility of sparse discrete data. ```python zoom_agg = ds.Canvas(plot_width=300, plot_height=300, x_range=(1.92,1.95), y_range=(1.92,1.95)).points(df, 'x', 'y') zoom = tf.spread(zoom_agg, px=3) tf.Images(tf.shade(zoom, how='eq_hist', name="eq_hist rescale_discrete_levels=False", rescale_discrete_levels=False), tf.shade(zoom, how='eq_hist', name="eq_hist rescale_discrete_levels=True", rescale_discrete_levels=True)) ``` -------------------------------- ### Preview Tileset with Bokeh Source: https://github.com/holoviz/datashader/blob/main/examples/tiling.ipynb Configure a Bokeh figure to display a tileset served by a local HTTP server. Ensure the WMTSTileSource URL points to the correct local server address. ```python from bokeh.plotting import figure from bokeh.models.tiles import WMTSTileSource from bokeh.io import show from bokeh.io import output_notebook output_notebook() xmin, ymin, xmax, ymax = full_extent_of_data p = figure(width=800, height=800, x_range=(int(-20e6), int(20e6)), y_range=(int(-20e6), int(20e6)), tools="pan,wheel_zoom,reset") p.background_fill_color = 'black' p.grid.grid_line_alpha = 0 p.axis.visible = False p.add_tile(WMTSTileSource(url="http://localhost:8080/{Z}/{X}/{Y}.png"), render_parents=False) show(p) ``` -------------------------------- ### Create and Activate Conda Environment for Development Source: https://github.com/holoviz/datashader/blob/main/doc/getting_started/index.rst After cloning the repository, navigate into the datashader directory and create a new conda environment named 'datashader' using the provided 'environment.yml' file, then activate it. ```bash cd datashader conda env create --name datashader --file ./examples/environment.yml conda activate datashader ``` -------------------------------- ### Explore Bundling Parameters for Star Graph Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/7_Networks.ipynb Generates a grid of visualizations for a star graph, varying the `decay` and `initial_bandwidth` parameters of the `hammer_bundle` function. Uses %%time to measure execution time. ```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]] ``` -------------------------------- ### Shade 2D Categorical Data with Custom Colors Source: https://github.com/holoviz/datashader/blob/main/examples/getting_started/2_Pipeline.ipynb Shade a 2D categorical aggregate where each pixel has a single category. This example demonstrates mapping numerical values directly to categorical colors using custom color keys for different sets of categories. ```python aggc_2d = canvas.points(df, 'x', 'y', ds.min('val')) custom_cats_colorkey = {10: 'blue', 20: 'green', 30: 'red'} all_cats_colorkey = {10: 'blue', 20: 'green', 30: 'red', 40: 'orange', 50: 'purple'} tf.Images(tf.shade(aggc_2d, color_key=custom_cats_colorkey, name="Custom categories - 2D"), tf.shade(aggc_2d, color_key=all_cats_colorkey, name="All categories - 2D")) ``` -------------------------------- ### Import HoloViews and Datashader operations Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/4_Trajectories.ipynb Imports HoloViews for interactive plotting and the `datashade` and `spread` operations for dynamic visualization. Initializes the Bokeh extension for HoloViews. ```python from holoviews.operation.datashader import datashade, spread import holoviews as hv hv.extension('bokeh') ``` -------------------------------- ### Combine Aggregates with Conditional Masking Source: https://github.com/holoviz/datashader/blob/main/examples/getting_started/2_Pipeline.ipynb Combine multiple aggregates using a mask derived from category comparisons. This example shades data based on whether the 'd3' category count equals the 'd5' category count, applied to different aggregate datasets. ```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')) ``` -------------------------------- ### Create synthetic timeseries data Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/3_Timeseries.ipynb Generates a base signal and creates multiple noisy versions, including 'rogue' lines with different statistical properties, and modifies specific data points to simulate outliers. This setup is useful for testing visualization techniques. ```python # Constants np.random.seed(2) n = 100000 # Number of points cols = list('abcdefg') # Column names of samples start = datetime.datetime(2010, 10, 1, 0) # Start time # Generate a fake signal signal = np.random.normal(0, 0.3, size=n).cumsum() + 50 # Generate many noisy samples from the signal noise = lambda var, bias, n: np.random.normal(bias, var, n) data = {c: signal + noise(1, 10*(np.random.random() - 0.5), n) for c in cols} # Add some "rogue lines" that differ from the rest cols += ['x'] ; data['x'] = signal + np.random.normal(0, 0.02, size=n).cumsum() # Gradually diverges cols += ['y'] ; data['y'] = signal + noise(1, 20*(np.random.random() - 0.5), n) # Much noisier cols += ['z'] ; data['z'] = signal # No noise at all # Pick a few samples from the first line and really blow them out locs = np.random.choice(n, 10) data['a'][locs] *= 2 # Create a dataframe data['Time'] = [start + datetime.timedelta(minutes=1)*i for i in range(n)] df = pd.DataFrame(data) df.tail() ``` -------------------------------- ### Load Plotting Libraries and Set Defaults Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/1_Plotting_Pitfalls.ipynb Imports necessary libraries including numpy, holoviews, and sets default options for plotting. It also configures the colormap for datashading. ```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:] ``` -------------------------------- ### Colormapping Aggregates with Different Color Schemes Source: https://github.com/holoviz/datashader/blob/main/examples/getting_started/2_Pipeline.ipynb Illustrates `tf.shade` for mapping aggregate data to colors, demonstrating the use of custom color lists, Bokeh palettes, and single-color mapping for alpha variation. ```python from bokeh.palettes import RdBu9 tf.Images(tf.shade(agg,cmap=["darkred", "yellow"], name="darkred, yellow"), tf.shade(agg,cmap=[(230,230,0), "orangered", "#300030"], name="yellow, orange red, dark purple"), tf.shade(agg,cmap=list(RdBu9), name="Bokeh RdBu9"), tf.shade(agg,cmap="black", name="Black")) ``` -------------------------------- ### Run Specific Benchmark by Regex Source: https://github.com/holoviz/datashader/blob/main/benchmarks/README.md Execute a subset of benchmarks by providing a regular expression to match benchmark file, class, and function names. This is useful for focusing on specific performance tests. ```bash asv run -b ShadeCategorical ``` -------------------------------- ### Execute Triangulation and Visualize Trimesh Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/6_Trimesh.ipynb Executes the triangulation function and visualizes the resulting triangular mesh using Datashader's trimesh method. Shows the first 15 triangles. ```python tris = triangulate(verts) ``` ```python tf.Images(tris.head(15), tf.shade(cvs.trimesh(verts, tris))) ``` -------------------------------- ### Trimesh Rasterization with Interpolation Options Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/6_Trimesh.ipynb Compares Datashader's trimesh rasterization with 'nearest' and 'linear' interpolation methods. Uses the colorcet rainbow colormap for visualization. Requires colorcet library. ```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')) ``` -------------------------------- ### Parallelizing Trimesh with Dask DataFrames Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/6_Trimesh.ipynb Prepares vertices and triangles as Dask DataFrames and constructs a partitioned mesh for parallel trimesh aggregation. Note: Mesh construction requires loading all 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 ``` -------------------------------- ### Apply Pixel Spreading with Different Parameters Source: https://github.com/holoviz/datashader/blob/main/examples/getting_started/2_Pipeline.ipynb Demonstrates the `tf.spread` function to enlarge pixels, showing effects with default settings, different pixel sizes (`px`), and custom shapes like 'square'. Useful for making isolated data points more visible. ```python img = tf.shade(agg, name="Original image") tf.Images(img, tf.shade(tf.spread(agg, name="spread 1px")), tf.shade(tf.spread(agg, px=3, name="spread 2px")), tf.shade(tf.spread(agg, px=5, shape='square', name="spread square"))) ``` -------------------------------- ### Datashader Trimesh Aggregation Functions Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/6_Trimesh.ipynb Demonstrates different aggregation functions (mean, max, min) for datashader's trimesh method. Useful for visualizing different aspects of the mesh data. ```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')) ``` -------------------------------- ### Datashade 100 Million Points with HoloViews Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/1_Plotting_Pitfalls.ipynb Generate a large dataset with 100 million points using the `gaussians` function and then visualize it using `datashade` and `hv.Points`. This demonstrates Datashader's capability to handle extremely large datasets efficiently. ```python %%time distb = gaussians(specs=[(2,2,0.03), (2,-2,0.1), (-2,-2,0.5), (-2,2,1.0), (0,0,3)], num=20000000) datashade(hv.Points(distb)) ``` -------------------------------- ### Preview S3 Tileset with Bokeh Source: https://github.com/holoviz/datashader/blob/main/examples/tiling.ipynb Configure a Bokeh figure to display a tileset rendered to an S3 bucket. The WMTSTileSource URL should point to the S3 bucket's public URL. ```python xmin, ymin, xmax, ymax = full_extent_of_data p = figure(width=800, height=800, x_range=(int(-20e6), int(20e6)), y_range=(int(-20e6), int(20e6)), tools="pan,wheel_zoom,reset") p.axis.visible = False p.background_fill_color = 'black' p.grid.grid_line_alpha = 0 p.add_tile(WMTSTileSource(url="https://datashader-tiles-testing.s3.amazonaws.com/wald_tiles/{Z}/{X}/{Y}.png"), render_parents=False) show(p) ``` -------------------------------- ### Import Libraries and Load Data Source: https://github.com/holoviz/datashader/blob/main/examples/getting_started/1_Introduction.ipynb Imports necessary libraries (Datashader, Colorcet, hvSampleData) and loads the NYC taxi dataset into a pandas DataFrame, selecting only the 'dropoff_x' and 'dropoff_y' columns. The dataset will be downloaded on the first run. ```python import datashader as ds, colorcet as cc import hvsampledata as hvs df = hvs.nyc_taxi_remote("pandas", engine_kwargs={"columns": ['dropoff_x', 'dropoff_y']}) df.head() ``` -------------------------------- ### Import HoloViews and Set Defaults Source: https://github.com/holoviz/datashader/blob/main/examples/getting_started/3_Interactivity.ipynb Imports necessary HoloViews modules and sets default colormaps and extensions for Bokeh and Matplotlib. This is a prerequisite for using HoloViews with Datashader. ```python import holoviews as hv import holoviews.operation.datashader as hd hd.shade.cmap=["lightblue", "darkblue"] hv.extension("bokeh", "matplotlib") ``` -------------------------------- ### Apply Spreading with Custom Masks Source: https://github.com/holoviz/datashader/blob/main/examples/getting_started/2_Pipeline.ipynb Shows how to use `tf.spread` with custom square masks of odd dimensions to alter the spreading pattern. This allows for more control over how pixels are enlarged. ```python box = np.array([[1, 1, 1, 1, 1], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [1, 1, 1, 1, 1]]) cross = np.array([[1, 0, 0, 0, 1], [0, 1, 0, 1, 0], [0, 0, 1, 0, 0], [0, 1, 0, 1, 0], [1, 0, 0, 0, 1]]) tf.Images(tf.shade(tf.spread(agg, mask=box), name="Custom box mask"), tf.shade(tf.spread(agg, mask=cross), name="Custom cross mask")) ``` -------------------------------- ### Datashader Render Tiles Function Signature Source: https://github.com/holoviz/datashader/blob/main/examples/tiling.ipynb Illustrates the expected arguments for the `render_tiles` function, including extent, tile levels, output path, and custom functions for data loading, rasterization, shading, and post-processing. ```python extent_of_area_i_want_to_tile = (-500000, -500000, 500000, 500000) # xmin, ymin, xmax, ymax render_tiles(extent_of_data_i_want_to_handle, tile_levels=range(6), output_path='example_tileset_output_directory', load_data_func=function_which_returns_dataframe, rasterize_func=function_which_creates_xarray_aggregate, shader_func=function_which_renders_aggregate_to_datashader_image, post_render_func=function_which_post_processes_image) ``` -------------------------------- ### Categorical aggregation with different methods Source: https://github.com/holoviz/datashader/blob/main/examples/getting_started/2_Pipeline.ipynb Illustrates how to use Datashader's 'by' function with categorical data to perform different types of aggregations (count, sum, mean) and visualize the results. ```python agg_c = canvas.points(df,'x','y', ds.by('cat', ds.count())) agg_s = canvas.points(df,'x','y', ds.by("cat", ds.sum("val"))) agg_m = canvas.points(df,'x','y', ds.by("cat", ds.mean("val"))) tf.Images(tf.shade(agg_c), tf.shade(agg_s), tf.shade(agg_m)) ``` -------------------------------- ### Plot Network with Different Layouts and Edge Rendering Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/7_Networks.ipynb Compares visualizations of a network using circular and force-directed layouts, with both direct edge connections and bundled edges. Uses %time to measure execution time. ```python cd = circular fd = forcedirected %time cd_d = graphplot(cd, connect_edges(cd,edges), "Circular layout") %time fd_d = graphplot(fd, connect_edges(fd,edges), "Force-directed") %time cd_b = graphplot(cd, hammer_bundle(cd,edges), "Circular layout, bundled") %time fd_b = graphplot(fd, hammer_bundle(fd,edges), "Force-directed, bundled") tf.Images(cd_d,fd_d,cd_b,fd_b).cols(2) ``` -------------------------------- ### Import necessary libraries Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/3_Timeseries.ipynb Imports essential libraries for data manipulation, time series generation, and datashader plotting. ```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 ``` -------------------------------- ### Visualize different reduction operators Source: https://github.com/holoviz/datashader/blob/main/examples/getting_started/2_Pipeline.ipynb Demonstrates visualizing the results of various reduction operators applied to the same data. Use `tf.Images` to display multiple shaded aggregations side-by-side for comparison. ```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')")) ``` -------------------------------- ### Display Grid of Bundled Star Graphs Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/7_Networks.ipynb Displays the grid of star graph visualizations generated by varying bundling parameters, arranged in 4 columns. ```python tf.Images(*grid).cols(4) ``` -------------------------------- ### Import necessary libraries Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/13_Geopandas.ipynb Imports required libraries for Datashader, color mapping, and GeoPandas data loading. ```python import colorcet as cc import datashader as ds import datashader.transfer_functions as tf import geopandas from geodatasets import get_path ``` -------------------------------- ### Visualize Categorized Graph Layouts Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/7_Networks.ipynb Applies random and force-directed layouts to the categorized graph data and visualizes the results with and without edge bundling. ```python rd = random_layout( cnodes, cedges) fd = forceatlas2_layout(cnodes, cedges) %time rd_d = graphplot(rd, connect_edges(rd,cedges), "Random layout", cat="cat") %time fd_d = graphplot(fd, connect_edges(fd,cedges), "Force-directed", cat="cat") %time rd_b = graphplot(rd, hammer_bundle(rd,cedges), "Random layout, bundled", cat="cat") %time fd_b = graphplot(fd, hammer_bundle(fd,cedges), "Force-directed, bundled",cat="cat") tf.Images(rd_d,fd_d,rd_b,fd_b).cols(2) ``` -------------------------------- ### Create Datashader Canvas Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/13_Geopandas.ipynb Initializes a Datashader Canvas object with specified dimensions for rendering. ```python canvas = ds.Canvas(plot_width=800, plot_height=400) ``` -------------------------------- ### Compare Benchmarks Between Commits Source: https://github.com/holoviz/datashader/blob/main/benchmarks/README.md Compare the performance of two specific commits side-by-side. This command is used after running benchmarks on multiple branches or commits to analyze performance differences. ```bash asv compare commit1 commit2 ``` -------------------------------- ### Create Datashader Canvas Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/12_Inspection_Reductions.ipynb Initializes a Datashader canvas with specified plot height and width. ```python canvas = ds.Canvas(plot_height=2, plot_width=3) ``` -------------------------------- ### Simplified `where` Reduction with `first` Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/12_Inspection_Reductions.ipynb Demonstrates a simplified `where` reduction where the lookup column is omitted, defaulting to returning the row index. ```python ds.where(ds.first('other')) ``` -------------------------------- ### Datashade 50,000 Points with HoloViews Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/1_Plotting_Pitfalls.ipynb Use the `datashade` function from HoloViews to plot a dataset of 50,000 points. This command works without any changes to settings, even for much larger datasets. ```python %%time datashade(hv.Points(dist)) ``` -------------------------------- ### Clone Datashader Git Repository Source: https://github.com/holoviz/datashader/blob/main/doc/getting_started/index.rst Clone the Datashader git repository from GitHub to set up a development environment. ```bash git clone git://github.com/holoviz/datashader.git ``` -------------------------------- ### Generate Sample Data with Pandas Source: https://github.com/holoviz/datashader/blob/main/examples/getting_started/2_Pipeline.ipynb Creates a Pandas DataFrame with synthetic data representing overlapping Gaussian distributions. This is useful for testing and demonstrating Datashader's capabilities. ```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") ``` -------------------------------- ### Render Large Rasters Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/5_Grids.ipynb Demonstrates rendering a large raster using `Canvas.raster`. The `%time` magic command measures the execution time for both data generation and rendering. ```python %time da5 = sample(m, n=10000, range_=(0,3)) %time tf.shade(cvs.raster(da5)) ``` -------------------------------- ### Demonstrate Nonuniform Colormapping with Heatmaps Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/1_Plotting_Pitfalls.ipynb Compares the 'hot' and 'fire' colormaps on a histogram-equalized dataset to show how perceptually uniform colormaps reveal more data detail. Use perceptually uniform colormaps to accurately represent data density differences. ```python hv.Layout([heatmap(dist,200,transform=eq_hist,label=cmap).opts(cmap=cmap) for cmap in ["hot","fire"]]).cols(2) ``` -------------------------------- ### Load and Prepare GeoDataFrame Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/8_Polygons.ipynb Loads a GeoDataFrame from a geodataset, reprojects it to a simple cylindrical projection, and calculates boundary and centroid geometries. ```python import geodatasets as gds import geopandas geodf = geopandas.read_file(gds.get_path('geoda health')) geodf = geodf.to_crs(epsg=4087) # simple cylindrical projection geodf['boundary'] = geodf.geometry.boundary geodf['centroid'] = geodf.geometry.centroid geodf.head() ``` -------------------------------- ### Create and Persist Dask DataFrame for Datashader Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/10_Performance.ipynb Create a Dask DataFrame from a Pandas DataFrame, setting the number of partitions based on CPU count, and then persist it in memory for efficient use with Datashader. ```python >>> from dask import dataframe as dd >>> import multiprocessing as mp >>> dask_df = dd.from_pandas(df, npartitions=mp.cpu_count()) >>> dask_df.persist() ... >>> cvs = datashader.Canvas(...) >>> agg = cvs.points(dask_df, ...) ``` -------------------------------- ### Visualize Various NetworkX Graph Structures Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/7_Networks.ipynb Generates and visualizes several standard NetworkX graph types (Complete, Lollipop, Barbell, Ladder, Circular Ladder, Star, Cycle) with different edge bundling levels. ```python n=50 plots = [nx_plot(g) for g in [ng(nx.complete_graph(n), name="Complete"), ng(nx.lollipop_graph(n, 5), name="Lollipop"), ng(nx.barbell_graph(n,2), name="Barbell"), ng(nx.ladder_graph(n), name="Ladder"), ng(nx.circular_ladder_graph(n), name="Circular Ladder"), ng(nx.star_graph(n), name="Star"), ng(nx.cycle_graph(n), name="Cycle")]] tf.Images(*chain.from_iterable(plots)).cols(3) ``` -------------------------------- ### Visualize Points with and without Z-value Coloring Source: https://github.com/holoviz/datashader/blob/main/examples/user_guide/6_Trimesh.ipynb Renders the sampled points using `cvs.points`. The first visualization shows only the spatial distribution, while the second colors the points based on their 'z' value, providing a rough approximation of the function's shape. ```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')) ```