Does anyone know how to plot an array of float numbers, 30 elements, as a histogram with ptolemy in Jython?
thank you
from javax.swing import JButton, JFrame, JPanel, JLabel, JMenuBar
from java.awt import GridBagLayout,GridBagConstraints
from java.awt import BorderLayout as BorderLayout
from javax.swing import WindowConstants
from ptolemy import *
from ptolemy.plot import Plot as Plot
from RainfallAnalysis import RainfallAnalysis
from jarray import array;
class Histogram(Plot):
dataset = 0;
theJFrame = JFrame();
def __init__(self):
self.theJFrame.setSize(400, 350); #outer box
self.setSize(350, 300); #graph window
self.setButtons(True); #buttons to print, edit, etc.
self.setMarksStyle("none"); #do not show marks at points
##
# Draw a histogram.
# It is assumed that all bins are of equal size.
# #param name The name to give this histogram in the key
# #param xMin minimum of x-range covered by histogram
# #param xMax maximum of x-range covered by histogram
# #param y array of bin heights; length of array is used to give number of points
def drawHistogram(self,name, xMin, xMax,y):
binWidth = (xMax - xMin)/y.__len__();
self.setBars(binWidth,0.0);
self.setConnected(False); # do not join bars with a line
first = True;
self.setYLabel("Rain Measurement");
self.setXLabel("days");
for i in range(y.__len__()): #loop to add bars to plot
x = i
self.addPoint(self.dataset, x, y[i], not first);
first = False;
self.addLegend(self.dataset, name);
self.dataset = self.dataset+1;
def showIt(self):
gridbag = GridBagLayout();
c = GridBagConstraints();
self.theJFrame.getContentPane().setLayout(gridbag);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
gridbag.setConstraints(self, c);
self.theJFrame.getContentPane().add(self);
self.theJFrame.setVisible(True);
self.theJFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
if __name__ == '__main__':
h = Histogram();
rf = RainfallAnalysis();
min = rf.getMin();
max = rf.getMax();
data = rf.getData();
h.drawHistogram("rainfall",min,max,data);
h.showIt();
And data is simply and array of double numbers.
Related
I'm trying to link a grid of images to a scatterplot, so that the right scatterplot point is highlighted when hovering over the corresponding image.
Problem is that when registering DOM events iteratively, they seem to get overwritten, and as a result only the last point of the scatterplot gets highlighted. How can I register independent events for each image.
See reproducible examples below.
import io
import random
import ipywidgets as ipw
from ipyevents import Event
import numpy as np
import PIL
from PIL import Image as PILImage
from bqplot import LinearScale, Figure, Axis, Scatter
# Random ipywidget image generator
def random_image():
arr = np.random.randint(255, size=(200, 200, 3), dtype=np.uint8)
img = PILImage.fromarray(arr)
with io.BytesIO() as fileobj:
img.save(fileobj, 'PNG')
img_b = fileobj.getvalue()
img_w = ipw.Image(value=img_b)
return img_w
# Create data
data = [{'x': idx, 'y': random.randint(1,10), 'image': random_image()}
for idx in range(1,6)]
# Create scatter plot
xs = LinearScale()
ys = LinearScale()
xa = Axis(scale=xs)
ya = Axis(scale=ys, orientation='vertical')
points = Scatter(x=[d['x'] for d in data],
y=[d['y'] for d in data],
scales={'x': xs, 'y': ys})
highlighted_point = Scatter(x=[-1000], y=[-1000], # Dummy point out of view
scales={'x': xs, 'y': ys},
preserve_domain={'x': True, 'y': True},
colors=['red'])
fig = Figure(marks=[points, highlighted_point], axes=[xa,ya])
# Add DOM events to images
img_list = []
for d in data:
img = d['image']
event = Event(source=img, watched_events=['mouseenter'])
def handle_event(event):
highlighted_point.x = [d['x']]
highlighted_point.y = [d['y']]
event.on_dom_event(handle_event)
img_list.append(img)
images = ipw.HBox(img_list)
ipw.VBox([fig, images])
I found a way to do it using functools.partial
import functools
# Event handler
def handle_event(idx, event):
highlighted_point.x = [data[idx]['x']]
highlighted_point.y = [data[idx]['y']]
# Add DOM events to images
img_list = []
for idx, d in enumerate(data):
img = d['image']
event = Event(source=img, watched_events=['mouseenter'])
event.on_dom_event(functools.partial(handle_event, idx))
img_list.append(img)
images = ipw.HBox(img_list)
ipw.VBox([fig, images])
I've N plots and i want to show the plots in M*N layout where M=1,2,3,.....
import holoviews as hv
from bokeh.layouts import layout as bk_lyout
from bokeh.layouts import row, column
from bokeh.models import (HoverTool, Panel, CustomJS)
from bokeh.models.widgets import (Tabs, Select, Button)
from bokeh.models.widgets.inputs import AutocompleteInput
from dask import dataframe as dd
from bokeh.core.enums import SizingMode
from holoviews.operation.datashader import datashade
def get_vmap(x, y, label=''):
if x not in cols or y not in cols:
return None
hover = HoverTool(tooltips=[('x-value', '#' + x + '{%F %H:%M:%Ss %6Nms}'),
('y-value', '$y')],
formatters={'#timestamp': 'datetime'})
curve_generated = curve(x, y, label=label)
vmap = datashade(curve_generated, normalization='linear').opts(width=400, height=400)
range_stream = hv.streams.RangeX(source=curve_generated)
filtered_zoom = curve_generated.apply(xrange_filter, streams=[range_stream])
hover_enabled = filtered_zoom.opts(tools=[hover])
# hover_enabled = hv.util.Dynamic(aggregate(curve_generated, width=50, height=50), operation=hv.QuadMesh).opts(tools=[hover], alpha=0, hover_alpha=0.1)
return vmap * hover_enabled
plot_layout = column(children=[])
def modify_doc(doc):
plots = []
for key, value in PLOT_INFO.items():
overlay_plots = []
for carrier in range(7):
car_value = value.format(carrier)
plot_label = "{}-Carrier_{}".format(key.split("_vs_")[1], carrier)
carrier_plot = get_vmap('timestamp', car_value, label=plot_label)
if carrier_plot is not None:
print('\n Found plot for Carrier {}, {}'.format(carrier, carrier_plot))
overlay_plots.append(carrier_plot)
overlaid_plot = None
for plot in overlay_plots:
overlaid_plot = overlaid_plot * plot if overlaid_plot is not None else plot
#overlaid_plot = hv.Overlay(overlay_plots).collate()
if overlaid_plot is not None:
try:
hv_overlay_plot = renderer.get_plot(overlaid_plot, doc)
hv_overlay_plot = bk_lyout([[hv_overlay_plot.state]], sizing_mode='fixed')
select.js_link('value', hv_overlay_plot, 'sizing_mode')
plots.append(hv_overlay_plot)
plot_layout.children.append(hv_overlay_plot)
except Exception as e:
print(e)
tab = Panel(child=plot_layout, title='Interactive Dashboard')
Here plot_layout.children has list of plots and i want to show them in M*N. for example 10 plots, 2 in each row and user should have option to modify it like select 5 plots then 2 rows.
Sliders have format property, see
https://docs.bokeh.org/en/latest/docs/reference/models/widgets.sliders.html
A) Where is the documentation for this property?
B) Is there an example of using the format attribute?
EDIT: is there a way to pass a function that takes the slider value and returns a string?
Formatting documentation can be found on this page with multiple examples. The sliders value can be used by calling slider.value.
I also edited an example where I added a formatter for the amplitude slider. The slider values in this example are used to change the sine wave.
You can run this example by using this command: bokeh serve script.py --show
import numpy as np
from bokeh.io import curdoc
from bokeh.layouts import row, column
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Slider, TextInput
from bokeh.plotting import figure
# Set up data
N = 200
x = np.linspace(0, 4*np.pi, N)
y = np.sin(x)
source = ColumnDataSource(data=dict(x=x, y=y))
# Set up plot
plot = figure(plot_height=400, plot_width=400, title="my sine wave",
tools="crosshair,pan,reset,save,wheel_zoom",
x_range=[0, 4*np.pi], y_range=[-2.5, 2.5])
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
# Set up widgets
text = TextInput(title="title", value='my sine wave')
offset = Slider(title="offset", value=0.0, start=-5.0, end=5.0, step=0.1)
amplitude = Slider(title="amplitude", value=1.0, start=-5.0, end=5.0, step=0.0000001, format='0.000f') #Slider with different formatting
phase = Slider(title="phase", value=0.0, start=0.0, end=2*np.pi)
freq = Slider(title="frequency", value=1.0, start=0.1, end=5.1, step=0.1)
# Set up callbacks
def update_title(attrname, old, new):
plot.title.text = text.value
text.on_change('value', update_title)
def update_data(attrname, old, new):
# Get the current slider values
a = amplitude.value
b = offset.value
w = phase.value
k = freq.value
# Generate the new curve
x = np.linspace(0, 4*np.pi, N)
y = a*np.sin(k*x + w) + b
source.data = dict(x=x, y=y)
for w in [offset, amplitude, phase, freq]:
w.on_change('value', update_data)
# Set up layouts and add to document
inputs = column(text, offset, amplitude, phase, freq)
curdoc().add_root(row(inputs, plot, width=800))
curdoc().title = "Sliders"
Based on the first example of the user-guide of Bokeh,
from bokeh.io import show, output_file
from bokeh.plotting import figure
from bokeh.models import Span
output_file("bars.html")
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
counts = [5, 3, 4, 2, 4, 6]
p = figure(x_range=fruits, plot_height=250, title="Fruit Counts",
toolbar_location=None, tools="")
p.vbar(x=fruits, top=counts, width=0.9)
# these two lines
vline = Span(location='Apples', dimension='height', line_color='blue', line_width=4)
p.renderers.extend([vline])
p.xgrid.grid_line_color = None
p.y_range.start = 0
show(p)
I am trying to add a vertical line to a bar plot whose x-range are categories. However, this does not seem to be possible, as this raises an error "ValueError: expected a value of type Real, got Apples of type str".
location='Apples' does not work as intended as it expected a number.
One solution is to convert the categorical value to the corresponding numeric value on the plot:
index = p.x_range.factors.index("Apples")
delta = (p.x_range.end - p.x_range.start)/p.x_range.factors.length;
location = delta/2 + index;
If the plot is dynamic (e.g. values are not known when the plot is built), then use an auxiliary JS function to do the conversion:
function _value_to_location(x_range, value) {
var index = x_range.factors.findIndex(x => x == value)
var delta = (x_range.end - x_range.start)/x_range.factors.length;
return delta/2 + index;
};
...
vline.location = _value_to_location(figure.x_range, "Apples");
I have modified the ImageView example by adding the statement data[:, ::10, :] = 0, which sets every tenth element of the middle dimension to 0. The program now shows horizontal lines. This is consistent with the documentation of the ImageView.setImage function: the default axes dictionary is {'t':0, 'x':1, 'y':2, 'c':3}. However, when I change this to {'t':0, 'x':2, 'y':1, 'c':3}, nothing changes where I would expect to get vertical rows.
So my question is: how can I give the row dimension a higher precedence in PyQtGraph? Of course I can transpose all my arrays myself before passing them to the setImage function but I prefer not to. Especially since both Numpy and Qt use the row/column convention and not X before Y. I don't see why PyQtGraph chooses the latter.
For completeness, find my modified ImageView example below.
import numpy as np
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg
app = QtGui.QApplication([])
## Create window with ImageView widget
win = QtGui.QMainWindow()
win.resize(800,800)
imv = pg.ImageView()
win.setCentralWidget(imv)
win.show()
win.setWindowTitle('pyqtgraph example: ImageView')
## Create random 3D data set with noisy signals
img = pg.gaussianFilter(np.random.normal(size=(200, 200)), (5, 5)) * 20 + 100
img = img[np.newaxis,:,:]
decay = np.exp(-np.linspace(0,0.3,100))[:,np.newaxis,np.newaxis]
data = np.random.normal(size=(100, 200, 200))
data += img * decay
data += 2
## Add time-varying signal
sig = np.zeros(data.shape[0])
sig[30:] += np.exp(-np.linspace(1,10, 70))
sig[40:] += np.exp(-np.linspace(1,10, 60))
sig[70:] += np.exp(-np.linspace(1,10, 30))
sig = sig[:,np.newaxis,np.newaxis] * 3
data[:,50:60,50:60] += sig
data[:, ::10, :] = 0 # Make image a-symmetrical
## Display the data and assign each frame a time value from 1.0 to 3.0
imv.setImage(data, xvals=np.linspace(1., 3., data.shape[0]),
axes={'t':0, 'x':2, 'y':1, 'c':3}) # doesn't help
## Start Qt event loop unless running in interactive mode.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
Looking through ImageView.py, setImage() parses the axes dictionary and based on presence of 't' it builds the z-axis/frame slider, and that's it. Rearranging the axes seems unimplemented yet.