-
Notifications
You must be signed in to change notification settings - Fork 2
/
PlottingUtil.py
59 lines (53 loc) · 2.01 KB
/
PlottingUtil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'''
# PlottingUtil
- Code By Michael Sherif Naguib
- license: MIT open source
- Date: 3/16/19
- @University of Tulsa
- Description: This is a class that uses other plotting software to make
it more convenient for plotting
'''
#imports
from datashader import transfer_functions as tf
from datashader.colors import inferno, viridis
from colorcet import palette
import bokeh.plotting as bp
from datashader.bokeh_ext import InteractiveImage
import numpy as np, pandas as pd, datashader as ds
#setup
palette["viridis"]=viridis
palette["inferno"]=inferno
# a class for plotting the points
class PlottingUtil():
def __init__(self,point_data,plot_w=500,plot_h=500,colormap=inferno):
#store the point data
self.point_data = point_data
#dimensions
self.plot_height = plot_h
self.plot_width = plot_w
#prepare the data
self.prepare_data()
#color
self.colormap = colormap
#Puts the data into a dataframe
def prepare_data(self):
self.df_data = pd.DataFrame(dict(x=np.array(self.point_data[0]),y=np.array(self.point_data[1])))
#sets new data
def set_data(self,point_data):
self.point_data=point_data
self.prepare_data()
#image callback: this funtion returns a curried function for interactive image plotting
def image_callback(self):
def callback(x_range, y_range, w, h, name=None):
# setup a plot
cvs = ds.Canvas(plot_width=w, plot_height=h, x_range=x_range, y_range=y_range)
agg = cvs.points(self.df_data, 'x', 'y')
img = tf.shade(agg, cmap=self.colormap, name=None)
return img
return callback
#creates a bokeh interactive plot for the data
def interactive_plot(self):
bp.output_notebook()
p = bp.figure(tools='pan,wheel_zoom,reset', x_range=(-5, 5), y_range=(-5, 5), \
plot_width=self.plot_width, plot_height=self.plot_height,background_fill_color='black')
return InteractiveImage(p, self.image_callback())