How to create custom hover tool with value mapping - bokeh

I am trying to create a custom hover tool using which takes the y-value of the plot and maps the value to different value.
The code I could come up with so far to achieve this functionality is
from bokeh.models import HoverTool
import holoviews as hv
df = pd.DataFrame(
{
"zero": [0, 0, 0, 0, 0, 0, 0],
"one": [1, 1, 1, 1, 1, 1, 1],
"two": [2, 2, 2, 2, 2, 2, 2],
}
)
mapping = {i: c for i, c in enumerate(df.columns)}
def col_mapping(num):
return mapping[int(num)]
hover = HoverTool(tooltips=[("x", "$x"), ("y", "$y")])
img = hv.Image((df.index, np.arange(df.shape[1]), df.T)).opts(tools=[hover])
img
x and y will be float values. So the idea is to map the y coordinates to its corresponding value in the mapping dictionary
Let me know how I can get a new value in the hover tool so that when the value is b/w 0 and 1 it will be
Thanks

Here's how I'd do it:
code = f"return ({json.dumps(mapping)})[Math.floor(special_vars.y)];"
hover = HoverTool(tooltips=[("x", "$x"), ("y", "$y"), ('mapped_y', '$y{0}')],
formatters={'$y': CustomJSHover(code=code)})
If you need a some more complicated code than that of col_mapping, then you'd have to use a ColumnDataSource and just add to it the fully transformed column.

Related

How to determine normal vector to an SCNPlane in Apple SceneKit

I have an SCNode which is dynamically created using the SCNPlane geometry to draw a plane using SceneKit.
How do I determine the normal vector to this plane?
Here is a playground demonstrating what I have tried. I have attached a screenshot of the resulting scene that is drawn. I don't think any of the cylinders, which represent the vectors obtained at the normal vector to this red plane.
//: Playground - noun: a place where people can play
import UIKit
import SceneKit
import PlaygroundSupport
// Set up the scene view
let frame = CGRect(
x: 0,
y: 0,
width: 500,
height: 300)
let sceneView = SCNView(frame: frame)
sceneView.showsStatistics = true
sceneView.autoenablesDefaultLighting = true
sceneView.allowsCameraControl = true
sceneView.scene = SCNScene()
// Setup our view into the scene
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3(x: 0, y: 0, z: 3)
sceneView.scene!.rootNode.addChildNode(cameraNode)
// Add a plane to the scene
let plane = SCNNode(geometry: SCNPlane(width: 3,height: 3))
plane.geometry?.firstMaterial!.diffuse.contents = UIColor.red.withAlphaComponent(0.5)
plane.geometry?.firstMaterial?.isDoubleSided = true
sceneView.scene!.rootNode.addChildNode(plane)
/*
normalSource = [SCNGeometrySource geometrySourceWithData:data
semantic:SCNGeometrySourceSemanticNormal
vectorCount:VERTEX_COUNT
floatComponents:YES
componentsPerVector:3 // nx, ny, nz
bytesPerComponent:sizeof(float)
dataOffset:offsetof(MyVertex, nx)
dataStride:sizeof(MyVertex)];
*/
let dataBuffer = plane.geometry?.sources(for: SCNGeometrySource.Semantic.normal)[0].data
let colorArray = [UIColor.red, UIColor.orange, UIColor.yellow, UIColor.green, UIColor.blue, UIColor.systemIndigo, UIColor.purple, UIColor.brown, UIColor.black, UIColor.systemPink]
let sceneGeometrySource = dataBuffer!.withUnsafeBytes {
(vertexBuffer: UnsafePointer<SCNVector3>) -> SCNGeometrySource in
let sceneVectors = Array(UnsafeBufferPointer(start: vertexBuffer, count: dataBuffer!.count/MemoryLayout<SCNVector3>.stride))
var i=0
for vector in sceneVectors{
let cyl = SCNCylinder(radius: 0.05, height: 3)
cyl.firstMaterial!.diffuse.contents = colorArray[i].withAlphaComponent(0.8)
let lineNode = SCNNode(geometry: cyl)
lineNode.eulerAngles = vector
sceneView.scene!.rootNode.addChildNode(lineNode)
}
return SCNGeometrySource(vertices: sceneVectors)
}
PlaygroundSupport.PlaygroundPage.current.liveView = sceneView
in the apple documentation https://developer.apple.com/documentation/accelerate/working_with_vectors in "Calculate the Normal of a Triangle" you can find the solution. You just need 3 points which are not on one line and then use this:
The following code defines the three vertices of the triangle:
let vertex1 = simd_float3(-1.5, 0.5, 0)
let vertex2 = simd_float3(1, 0, 3)
let vertex3 = simd_float3(0.5, -0.5, -1.5)
Your first step in calculating the normal of the triangle is to create two vectors defined by the difference between the vertices—representing two sides of the triangle:
let vector1 = vertex2 - vertex3
let vector2 = vertex2 - vertex1
The simd_cross function returns the vector that's perpendicular to the two vectors you pass it. In this example, the returned vector is the normal of the triangle. Because the normal represents a direction, you can normalize the value to get a unit vector:
let normal = simd_normalize(simd_cross(vector1, vector2))

Bokeh HoverTool not working with multi_line

I'm banging my head on this one.
Bokeh's multip_line and HoverTool don't seem to want to play nice with each other. My issue is similar to this one: multi_line hover in bokeh. (side note: I've tried the solution code from that question and it's not working for me, which is probably not a good sign.)
I have my own reproducible example code here, condensed from a heatmap-like plot I'm working on:
from bokeh.plotting import figure, output_file, show
from bokeh.models.mappers import LinearColorMapper
from bokeh.models import ColumnDataSource, ColorBar, HoverTool
output_file("heatmap.html")
p = figure(title="multi_line hover failure example")
p.add_tools(HoverTool(
show_arrow=False,
line_policy="nearest",
tooltips=[
("color", "#color"),
("name", "#name")
]))
patch_xs = [[1, 2, 2, 3], [4, 5, 6, 5], [5, 5, 8, 8]]
patch_ys = [[1, 2, 4, 1], [3, 4, 3, 2], [5, 8, 8, 5]]
patch_colors = [1, 2, 3]
patch_names = ['robert', 'quinn', 'jessy']
line_xs = [[1, 2, 2, 3], [4, 5, 6, 5], [5, 5, 8, 8]]
line_ys = [[-1, -2, -4, -1], [-3, -4, -3, -2], [-5, -8, -8, -5]]
line_colors = [-1, -2, -3]
line_names = ['karen', 'louise', 'charles']
mapper = LinearColorMapper(
palette='Turbo256',
low=min(patch_colors + line_colors),
high=max(patch_colors + line_colors),
)
# patches included to confirm that hover is working
# commenting this out does not make hover work
p.patches('xs', 'ys', line_width=1,
color={'field': 'color', 'transform': mapper},
source=ColumnDataSource(dict(
xs=patch_xs,
ys=patch_ys,
color=patch_colors,
name=patch_names
))
)
p.multi_line('xs', 'ys', line_width=8,
color={'field': 'color', 'transform': mapper},
source=ColumnDataSource(dict(
xs=line_xs,
ys=line_ys,
color=line_colors,
name=line_names
)))
p.add_layout(
ColorBar(
color_mapper=mapper,
location=(0, 0)
),
'left')
show(p)
My code produces the graph below, in which the hover tool works fine for the filled in shapes (as can be seen in the screen shot) but not the lines. Trust me, I've tried mousing all over the lines like a deranged Aladdin trying to coax a genie to come out of them.
Removing the filled in shapes (patches) does not fix it so a left them in to show how the hover tool should be working.
Funnily enough, according to the documentation (https://docs.bokeh.org/en/latest/docs/reference/models/tools.html#bokeh.models.tools.HoverTool) the HoverTool does not work with patches but does work with multi_line. (perhaps it has something to do with using glyphs instead of figure elements?)
Any help would be appreciated
It's a bug. It was fixed in this commit and should be available in Bokeh 2.3.
Alternatively, you could try Bokeh 2.1 - IIRC it was working for me there.

ControlsFX SpreadsheetView rowspan IndexOutOfBoundsException

I am new to JavaFX and ControlsFX.
I am trying to create a very basic SpreadsheetView using the ControlsFX library. Following is the function to populate and create the SpreadsheetView:
private fun spreadSheetFunc() : SpreadsheetView {
val rowCount = 15
val columnCount = 10
val grid = GridBase(rowCount, columnCount)
val rows = FXCollections.observableArrayList<ObservableList<SpreadsheetCell>>()
var list = FXCollections.observableArrayList<SpreadsheetCell>()
list.add(SpreadsheetCellType.STRING.createCell(0, 0, 1, 1, "row0-col0"))
list.add(SpreadsheetCellType.STRING.createCell(0, 1, 2, 1, "row0-col1"))
list.add(SpreadsheetCellType.STRING.createCell(0, 2, 1, 1, "row0-col2"))
rows.add(list)
list = FXCollections.observableArrayList()
list.add(SpreadsheetCellType.STRING.createCell(1, 0, 1, 1, "row1-col0"))
//commenting row1-col1 as row0-col1 has a rowspan of 2
//list.add(SpreadsheetCellType.STRING.createCell(1, 1, 1, 1, "row1-col1"))
list.add(SpreadsheetCellType.STRING.createCell(1, 2, 1, 1, "row1-col2"))
rows.add(list)
list = FXCollections.observableArrayList()
list.add(SpreadsheetCellType.STRING.createCell(2, 0, 1, 1, "row2-col0"))
list.add(SpreadsheetCellType.STRING.createCell(2, 1, 1, 1, "row2-col1"))
list.add(SpreadsheetCellType.STRING.createCell(2, 2, 1, 1, "row2-col2"))
rows.add(list)
list = FXCollections.observableArrayList()
list.add(SpreadsheetCellType.STRING.createCell(3, 0, 1, 1, "row3-col0"))
list.add(SpreadsheetCellType.STRING.createCell(3, 1, 1, 1, "row3-col1"))
list.add(SpreadsheetCellType.STRING.createCell(3, 2, 1, 1, "row3-col2"))
rows.add(list)
grid.setRows(rows)
return SpreadsheetView(grid)
}
On running it, I get the following error:
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2 at
java.util.ArrayList.rangeCheck(ArrayList.java:653)
I know its happening because I am not adding any value for rowIndex=1 colIndex=1 (see the commented out line) ... but that is what I want.
The row0-col1 has a rowspan of 2 which should mean that even if my row1-col1 is absent, there shouldn't be any problem.
Why doesn't ControlsFX automatically take care of this?
If I uncomment that line, I get the following output:
Edit 1:
Also, I found another issue, when a colspan/rowspan occupies the whole column/row in the SpreadsheetView and then when one presses arrow key to navigate to cells you get an error:
The above situation arises when you press the right arrow key (Even though their isn't a cell on the right)
Let me apologize because it is not well documented how span must be made in the SpreadsheetView. I will update the documentation.
If you want to span, you have to put the same cell in every cell inside the span.
So either you build your own cell, and then in every place. In your case, you would add the same cell in row 0 column 1 and in row 1 column 1.
Or you could keep your code, and simply call the method spanRow on the Grid. This method will automatically take your cell and place it accordingly.
Regarding the second issue, please submit it to our issue tracker so we can fix it : https://bitbucket.org/controlsfx/controlsfx/issues?status=new&status=open
If you have other issue regarding the SpreadsheetView, consider posting in our Google group where we will get notifications : http://groups.controlsfx.org

Get same output as R console in Java using JRI

When I enter the following commands directly into the R console
library("xts")
mySeries <- xts(c(1.0, 2.0, 3.0, 5.0, 6.0), order.by=c(ISOdatetime(2001, 1, 1, 0, 0, 0), ISOdatetime(2001, 1, 2, 0, 0, 0), ISOdatetime(2001, 1, 3, 0, 0, 0), ISOdatetime(2001, 1, 4, 0, 0, 0), ISOdatetime(2001, 1, 5, 0, 0, 0)))
resultingSeries <- to.monthly(mySeries)
resultingSeries
I will get an output like this
mySeries.Open mySeries.High mySeries.Low mySeries.Close
Jan 2001 1 6 1 6
When I look into the attributes, I see the following output
attributes(resultingSeries)
$dim
[1] 1 4
$dimnames
$dimnames[[1]]
NULL
$dimnames[[2]]
[1] "mySeries.Open" "mySeries.High" "mySeries.Low" "mySeries.Close"
$index
[1] 978307200
attr(,"tclass")
[1] "yearmon"
$tclass
[1] "POSIXct" "POSIXt"
$tzone
[1] ""
$class
[1] "xts" "zoo"
$.indexCLASS
[1] "yearmon"
This is the same I get in Java. I'm wondering where the magic happens so that I see the nice output I get in R. I have no access to the event loop, since I'm using JRI like this (since, it's the recommended way and simplifies error handling):
REngine engine = REngine.engineForClass("org.rosuda.REngine.JRI.JRIEngine");
REXP result = engine.parseAndEval(...)
/edit
In Java I execute each command from above as follows:
REXP result = engine.parseAndEval("resultingSeries") // or any other command
What I get is
org.rosuda.REngine.REXPDouble#4ac66122+[12]
The payload being doubles: 1, 6, 1, 6
The attributes are the same as specified above.
Now R does some magic to display the output above. Is there a way I can get the same output without having to create it manually by myself? Where's the implementation stored, that R gets the above mentioned output?
Here is a piece of code that will work, here i extracted the first element of the field mySeries.Open from the object resultingSeries (which i converted to a data frame) which is equal to 1, notice that you can't pass all of the resultingSeries object strait into Java, you will need to break it down.
package stackoverflow;
import org.rosuda.JRI.REXP;
import org.rosuda.JRI.Rengine;
/**
*
* #author yschellekens
*/
public class StackOverflow {
public static void main(String[] args) throws Exception {
String[] Rargs = {"--vanilla"};
Rengine rengine = new Rengine( Rargs, false, null);
rengine.eval("library('xts')");
rengine.eval("mySeries <- xts(c(1.0, 2.0, 3.0, 5.0, 6.0), order.by=c(ISOdatetime(2001, 1, 1, 0, 0, 0), ISOdatetime(2001, 1, 2, 0, 0, 0), ISOdatetime(2001, 1, 3, 0, 0, 0), ISOdatetime(2001, 1, 4, 0, 0, 0), ISOdatetime(2001, 1, 5, 0, 0, 0)))");
rengine.eval("resultingSeries <- to.monthly(mySeries)");
rengine.eval("resultingSeries<-as.data.frame(resultingSeries)");
REXP result= rengine.eval("resultingSeries$mySeries.Open");
System.out.println("Greeting from R: "+result.asDouble());
}
}
And the Java output:
run:
Greeting from R: 1.0
I figured out the following workaround. The solution is far from perfect.
R offers a command to save its console output as characters vector.
capture.output( {command} )
We can access the output using
REXPString s = rengine.parseAndEval("capture.output( to.monthly(mySeries))")
String[] output = result.asStrings()
The variable output will contain all output lines
[0] mySeries.Open mySeries.High mySeries.Low mySeries.Close
[1]Jan 2001 1 6 1 6
Alternatively you coud use JRIEngine and attack yourself to the event loop, which it did not want in my case (due to the more complicated error handling).

plotting an array of dataset with ListPlot Mathematica

I have a set of datapoints such as (THIS IS AN EXAMPLE)
val=4; (*this value is calculated before in the program, so it not known a priori*)
x={0,1,2,3};
data=Table[0, {val}];
data[[1]] = {1,5,6,8};
data[[2]] = {9,7,1,3};
data[[3]] = {3,4,5,6};
data[[4]] = {2,2,4,6};
Now I can plot each of these data with ListPlot as
ListPlot[Transpose[{x,data[[1]]}]]
and if I want to plot more than one I can do
ListPlot[{Transpose[{x, data[[1]]}], Transpose[{x, data[[2]]}]}]
but how can I plot all of them in one code single line, by considering that val is calculated before in the program?
Is there a way to do something like
For[i = 1, i < val + 1, i++, ListPlot[Transpose[{x,data[i]}]]......]
having a single graph with all x-y curves?
Indeed I would like a static picture of
Manipulate[ListPlot[Transpose[{x, data[[i]]}]], {i, 1, val,1}]
Thanks
Virgilio
You want to "do the same thing" to every element of a list. That should tell you to think of using Map. Your list is named data and each element is your four element sublist. If you look at the help page for Map it shows you need to think up a function that does what you need to do to each individual sublist. You have already understood that you need to use Transpose with x and your sublist so that tells you your function and you are almost there. The result of Map will be a list of all those results. So
In[1]:= x = {0, 1, 2, 3};
data = {{1, 5, 6, 8}, {9, 7, 1, 3}, {3, 4, 5, 6}, {2, 2, 4, 6}};
ListPlot[Map[Transpose[{x, #}] &, data], Joined -> True]
Out[3]= ...FourOverlaidPlotsSnipped...
Go through that a word at a time until you can really understand the thinking that was done to be able to write that. You will use this idea again and again if you keep using Mathematica.
For the example you give the cleanest method is to use DataRange:
data = {{1, 5, 6, 8}, {9, 7, 1, 3}, {3, 4, 5, 6}, {2, 2, 4, 6}};
ListLinePlot[data, DataRange -> {0, 3}]
Please ask your future questions on the dedicated Mathematica StackExchange site:

Resources