datashader#
Links: notebook
, html, PDF
, python
, slides, GitHub
datashader plots huge volume of data.
from jyquickhelper import add_notebook_menu
add_notebook_menu()
import bokeh.plotting as bp
bp.output_notebook()
import datashader
datashader.__version__
'0.6.4dev1'
The version should be higher than 0.6.4
.
short example#
From 4_Trajectories.ipynb.
import pandas as pd
import numpy as np
import xarray as xr
# On Windows, you must run the notebook with admin right
# otherwise the following instruction does not end.
import datashader
import datashader as ds
import datashader.transfer_functions as tf
# Constants
np.random.seed(1)
n = 1000000 # Number of points
f = filter_width = 5000 # momentum or smoothing parameter, for a moving average filter
# filtered random walk
xs = np.convolve(np.random.normal(0, 0.1, size=n), np.ones(f)/f).cumsum()
ys = np.convolve(np.random.normal(0, 0.1, size=n), np.ones(f)/f).cumsum()
# Add "mechanical" wobble on the x axis
xs += 0.1*np.sin(0.1*np.array(range(n-1+f)))
# Add "measurement" noise
xs += np.random.normal(0, 0.005, size=n-1+f)
ys += np.random.normal(0, 0.005, size=n-1+f)
# Add a completely incorrect value
xs[int(len(xs)/2)] = 100
ys[int(len(xs)/2)] = 0
# Create a dataframe
df = pd.DataFrame(dict(x=xs,y=ys))
# Default plot ranges:
x_range = (xs.min(), xs.max())
y_range = (ys.min(), ys.max())
df.tail()
x | y | |
---|---|---|
1004994 | 65.164829 | -105.064056 |
1004995 | 65.177603 | -105.069781 |
1004996 | 65.190898 | -105.071699 |
1004997 | 65.194054 | -105.054657 |
1004998 | 65.204752 | -105.073366 |
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)
%time create_image()
Wall time: 1.1 s

from datashader.bokeh_ext import InteractiveImage
import bokeh.plotting as bp
def base_plot(tools='pan,wheel_zoom,reset'):
p = bp.figure(tools=tools, plot_width=500, plot_height=500,
x_range=x_range, y_range=y_range, outline_line_color=None,
min_border=0, min_border_left=0, min_border_right=0,
min_border_top=0, min_border_bottom=0)
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None
return p
p = base_plot()
InteractiveImage(p, create_image)