2
Contains convenience functions to create ready-made PlotRenderer
3
and PlotFrame instances of various types.
6
from numpy import array, ndarray, transpose, cos, sin
8
# Local relative imports
9
from abstract_data_source import AbstractDataSource
10
from array_data_source import ArrayDataSource
11
from axis import PlotAxis
12
from barplot import BarPlot
13
from data_range_1d import DataRange1D
14
from grid import PlotGrid
15
from linear_mapper import LinearMapper
16
from scatterplot import ScatterPlot
17
from polar_mapper import PolarMapper
18
from lineplot import LinePlot
19
from polar_line_renderer import PolarLineRenderer
21
def _create_data_sources(data, index_sort="none"):
23
Returns datasources for index and value based on the inputs. Assumes that
24
the index data is unsorted unless otherwise specified.
26
if (type(data) == ndarray) or (len(data) == 2):
28
if type(index) in (list, tuple, ndarray):
29
index = ArrayDataSource(array(index), sort_order=index_sort)
30
elif not isinstance(index, AbstractDataSource):
31
raise RuntimeError, "Need an array or list of values or a DataSource, got %s instead." % type(index)
33
if type(value) in (list, tuple, ndarray):
34
value = ArrayDataSource(array(value))
35
elif not isinstance(value, AbstractDataSource):
36
raise RuntimeError, "Need an array or list of values or a DataSource, got %s instead." % type(index)
40
raise RuntimeError, "Unable to create datasources."
43
def create_scatter_plot(data=[], index_bounds=None, value_bounds=None,
44
orientation="h", color="green", marker="square",
46
bgcolor="transparent", outline_color="black",
48
add_grid=False, add_axis=False,
51
Creates a ScatterPlot from a single Nx2 data array or a tuple of
52
two length-N 1-D arrays. The data must be sorted on the index if any
53
reverse-mapping tools are to be used.
55
Pre-existing "index" and "value" datasources can be passed in.
58
index, value = _create_data_sources(data)
60
if index_bounds is not None:
61
index_range = DataRange1D(low=index_bounds[0], high=index_bounds[1])
63
index_range = DataRange1D()
64
index_range.add(index)
65
index_mapper = LinearMapper(range=index_range)
67
if value_bounds is not None:
68
value_range = DataRange1D(low=value_bounds[0], high=value_bounds[1])
70
value_range = DataRange1D()
71
value_range.add(value)
72
value_mapper = LinearMapper(range=value_range)
74
plot = ScatterPlot(index=index, value=value,
75
index_mapper=index_mapper,
76
value_mapper=value_mapper,
77
orientation=orientation,
79
marker_size=marker_size,
82
outline_color=outline_color,
83
border_visible=border_visible,)
86
add_default_grids(plot, orientation)
88
add_default_axes(plot, orientation)
92
def create_line_plot(data=[], index_bounds=None, value_bounds=None,
93
orientation="h", color="red", width=1.0,
94
dash="solid", value_mapper_class=LinearMapper,
95
bgcolor="transparent", border_visible=False,
96
add_grid=False, add_axis=False,
99
index, value = _create_data_sources(data, index_sort)
101
if index_bounds is not None:
102
index_range = DataRange1D(low=index_bounds[0], high=index_bounds[1])
104
index_range = DataRange1D()
105
index_range.add(index)
106
index_mapper = LinearMapper(range=index_range)
108
if value_bounds is not None:
109
value_range = DataRange1D(low=value_bounds[0], high=value_bounds[1])
111
value_range = DataRange1D()
112
value_range.add(value)
113
value_mapper = value_mapper_class(range=value_range)
115
plot = LinePlot(index=index, value=value,
116
index_mapper = index_mapper,
117
value_mapper = value_mapper,
118
orientation = orientation,
123
border_visible=border_visible)
126
add_default_grids(plot, orientation)
128
add_default_axes(plot, orientation)
132
def create_bar_plot(data=[], index_bounds=None, value_bounds=None,
133
orientation="h", color="red", bar_width=10.0,
134
value_mapper_class=LinearMapper,
136
fill_color="red", line_width=1,
137
bgcolor="transparent", border_visible=False,
139
add_grid=False, add_axis=False):
141
index, value = _create_data_sources(data)
143
if index_bounds is not None:
144
index_range = DataRange1D(low=index_bounds[0], high=index_bounds[1])
146
index_range = DataRange1D()
147
index_range.add(index)
148
index_mapper = LinearMapper(range=index_range)
150
if value_bounds is not None:
151
value_range = DataRange1D(low=value_bounds[0], high=value_bounds[1])
153
value_range = DataRange1D()
154
value_range.add(value)
155
value_mapper = value_mapper_class(range=value_range)
158
plot = BarPlot(index=index,
160
value_mapper=value_mapper,
161
index_mapper=index_mapper,
162
orientation=orientation,
163
line_color=line_color,
164
fill_color=fill_color,
165
line_width=line_width,
167
antialias=antialias,)
170
add_default_grids(plot, orientation)
172
add_default_axes(plot, orientation)
176
def create_polar_plot(data, orientation='h', color='black', width=1.0,
177
dash="solid", grid="dot", value_mapper_class=PolarMapper):
178
if (type(data) != ndarray) and (len(data) == 2):
179
data = transpose(array(data))
181
r_data, t_data = transpose(data)
182
index_data= r_data*cos(t_data)
183
value_data= r_data*sin(t_data)
185
index = ArrayDataSource(index_data, sort_order='ascending')
186
# Typically the value data is unsorted
187
value = ArrayDataSource(value_data)
189
index_range = DataRange1D()
190
index_range.add(index)
191
index_mapper = PolarMapper(range=index_range)
193
value_range = DataRange1D()
194
value_range.add(value)
195
value_mapper = value_mapper_class(range=value_range)
197
plot = PolarLineRenderer(index=index, value=value,
198
index_mapper = index_mapper,
199
value_mapper = value_mapper,
200
orientation = orientation,
209
def add_default_axes(plot, orientation="normal", vtitle="",htitle=""):
211
Creates left and bottom axes for a plot. Assumes that the index is
212
horizontal and value is vertical by default; set *orientation* to
213
something other than "normal" if they are flipped.
215
if orientation in ("normal", "h"):
216
v_mapper = plot.value_mapper
217
h_mapper = plot.index_mapper
219
v_mapper = plot.index_mapper
220
h_mapper = plot.value_mapper
222
left = PlotAxis(orientation='left',
227
bottom = PlotAxis(orientation='bottom',
232
plot.underlays.append(left)
233
plot.underlays.append(bottom)
237
def add_default_grids(plot, orientation="normal"):
239
Creates horizontal and vertical gridlines for a plot. Assumes that the
240
index is horizontal and value is vertical by default; set orientation to
241
something other than "normal" if they are flipped.
243
if orientation in ("normal", "h"):
244
v_mapper = plot.index_mapper
245
h_mapper = plot.value_mapper
247
v_mapper = plot.value_mapper
248
h_mapper = plot.index_mapper
250
vgrid = PlotGrid(mapper=v_mapper, orientation='vertical',
252
line_color="lightgray", line_style="dot")
254
hgrid = PlotGrid(mapper=h_mapper, orientation='horizontal',
256
line_color="lightgray", line_style="dot")
258
plot.underlays.append(vgrid)
259
plot.underlays.append(hgrid)