4

I downloaded and installed datasheder using the below steps:

git clone https://github.com/bokeh/datashader.git  
cd datashader  
conda install -c bokeh --file requirements.txt  
python setup.py install

After that, I have run the code using terminal like `python data.py, but no graph is displayed; nothin is being displayed.

I am not sure if I've follwed the right steps here, can somebody help me display the graphs? Here is my code:

import pandas as pd
import numpy as np
import xarray as xr
import datashader as ds
import datashader.glyphs
import datashader.transfer_functions as tf
from collections import OrderedDict

np.random.seed(1)
num=10000

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.01,10,"d1"), (2,-2,0.1,20,"d2"), (-2,-2,0.5,30,"d3"), (-2,2,1.0,40,"d4"), (0,0,3,50,"d5")]}

df = pd.concat(dists,ignore_index=True)
df["cat"]=df["cat"].astype("category")
df.tail()

tf.shade(ds.Canvas().points(df,'x','y'))
glyph = ds.glyphs.Point('x', 'y')
canvas = ds.Canvas(plot_width=200, plot_height=200, x_range=(-8,8)y_range=(-8,8))
from datashader import reductions
reduction = reductions.count()

from datashader.core import bypixel
agg = bypixel(df, canvas, glyph, reduction)
agg
canvas.points(df, 'x', 'y', agg=reductions.count())
tf.shade(canvas.points(df,'x','y',agg=reductions.count()))
tf.shade(canvas.points(df,'x','y',agg=reductions.any()))
tf.shade(canvas.points(df,'x','y',agg=reductions.mean('y')))
tf.shade(50-canvas.points(df,'x','y',agg=reductions.mean('val')))
agg  = canvas.points(df, 'x', 'y')
tf.shade(agg.where(agg>=np.percentile(agg,99)))
tf.shade(np.sin(agg))
aggc = canvas.points(df, 'x', 'y', ds.count_cat('cat'))
aggc
tf.shade(aggc.sel(cat='d3'))
agg_d3_d5=aggc.sel(cat=['d3', 'd5']).sum(dim='cat')
tf.shade(agg_d3_d5)
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
gaurav singh
  • 1,376
  • 1
  • 15
  • 25
  • Since there is an accepted answear, can you provide final solution within this code? I am follwoing datashader documentation but I am not into Python at all. I just need to plot some large dataset nicely which is not possible in pgfplots nor Matlab – struct Sep 16 '21 at 07:21

2 Answers2

5

I haven't tried your code, but there is nothing in there that would actually display the image. Each shade() call creates an image in memory, but then nothing is done with it here. If you were in a Jupyter notebook environment and the shade() call were the last item in the cell, it would display automatically, but the regular Python prompt doesn't have such "rich display" support. So you can either save it to an image file on disk (using e.g. utils/export_image), or you can assign the result of shade() to a variable and then pass that to a Bokeh or Matplotlib or other plot, as you prefer. But you have to do something with the image if you want to see it.

James A. Bednar
  • 3,195
  • 1
  • 9
  • 13
  • Since there is an accepted answear, can you provide final solution within this code? I am follwoing datashader documentation but I am not into Python at all. I just need to plot some large dataset nicely which is not possible in pgfplots nor Matlab – struct Sep 16 '21 at 07:21
  • I'm not sure what the original code was meant to do, i.e. whether and how all the shade() calls were meant to interact with each other. Datashader.org has lots of current examples; hopefully one of those is illustrating something you're trying to do! – James A. Bednar Sep 17 '21 at 18:57
  • Unfortunatelly no, provided examples wont produce any actuall plot. – struct Sep 19 '21 at 07:47
  • Please pick the simplest example illustrating what you are trying to do and open a discussion at https://discourse.holoviz.org/c/datashader listing precisely what you steps you took, what the outcome was, and what you wanted the outcome to be. – James A. Bednar Sep 20 '21 at 14:38
  • @struct I adapted James' answer to the OP's code and posted solution. Please check it. – Vandan Revanur May 27 '22 at 19:37
1

I was able to produce the plot one of the tf.shade in your code this way.

from datashader.utils import export_image
img = tf.shade(canvas.points(df,'x','y',agg=reductions.count()))
export_image(img=img, filename='test1', fmt=".png",  export_path=".")

This is the plot in test1.pngPoints_plot

Vandan Revanur
  • 459
  • 6
  • 17