Bokeh Hovertool for patch - bokeh

Is the hover tool for patch different than other glyphs? The plot can see the index for circle but not for patch.
output_file("patch.html")
TOOLTIPS = [
("index", "$index"),
]
p = figure(plot_width=400, plot_height=400, tooltips=TOOLTIPS, tools='hover,help')
# add a patch renderer with an alpha an line width
p.patch([1, 2, 3, 4, 5], [6, 7, 8, 7, 3], alpha=0.5, line_width=2)
p.circle([2], [4],size=20, line_width=2)
show(p)

As of Bokeh 1.0.2, hit testing has not been implemented for Patch, i.e. it is invisible as far as the Hover tool is concerned. You could use the vectorized patches glyph method instead:
p.patches([[1, 2, 3, 4, 5]], [[6, 7, 8, 7, 3]], alpha=0.5, line_width=2)
However, this will always only return 0 as the value of $index since there is only just the one patch (whose index is 0). If you are looking to get "index" values for the vertices of the patch, you will need to do something like plot invisible circles at the same locations as the vertices, that are only there for hit-testing purposes (to drive the hover tool)

Related

Bokeh, how do I add the "?" in the configuring plot tool

I could not find the information regarding the helptool on the bokeh documentation. There is an explanation for all other tools but not this one. I am talking about these tools from the toolbar:
http://docs.bokeh.org/en/latest/docs/user_guide/tools.html
I can choose most of the plot tools I want to use with no problem. I want to include the helptool, which is clearly visible just before the bokeh logo, however I cannot find any direct reference to it.
I just tested it(!), if you want the Helptool, which has a "?" sybmol, it will only be included by default if you do not specify the tools displayed with the plot. OK I will go with this but I will have no "?" if I choose the tools.
I understand your problem I think. You are saying that by default the tool panel will include the help tool. When you specify all the tools from scratch, help tool is not included as default.
If you generate the plot without specifying any tools, then list all tools on the plot:
p.tools
>>>>
[PanTool(id='450f2e26-e0e5-4d90-89c4-44d20f2f688b', ...),
WheelZoomTool(id='84011ce1-4f73-4abb-abd1-26a830b70635', ...),
BoxZoomTool(id='adbfb29a-aa7d-4883-8f8f-d12b7c5af139', ...),
SaveTool(id='ff6ba8bb-c487-418d-82e5-28ff0402e2d6', ...),
ResetTool(id='dfac2559-da4c-4902-829d-f795ee0bfd56', ...),
HelpTool(id='1141330c-e9ff-4e5b-a737-3517c24f263e', ...)]
so you can see there is a helptool. Also it is in the documents
http://docs.bokeh.org/en/latest/docs/reference/models/tools.html#bokeh.models.tools.HelpTool
Therefore you can import this tool and add it to the plot in two ways:
from bokeh.plotting import figure, output_file, show
from bokeh.models import HelpTool
output_file("toolbar.html")
# create a new plot with the toolbar below
p = figure(plot_width=400, plot_height=400,
title=None, toolbar_location="below", tools="pan,wheel_zoom,box_zoom,reset")
p.add_tools(HelpTool())
p.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=10)
show(p)
Or more simply:
from bokeh.plotting import figure, output_file, show
output_file("toolbar.html")
# create a new plot with the toolbar below
p = figure(plot_width=400, plot_height=400,
title=None, toolbar_location="right",tools = "pan,wheel_zoom,box_zoom,reset,help")
p.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=10)
show(p)

R - Creating a fuzzy correspondance analysis

I have ecological data of benthic invertebrate species traits. I would like to do a fuzzy correspondance analysis with ade4-package. I have trait sub-categories as columns and benthic sampling places as rows.
I have prepared and plotted a matrix of fuzzy variables with my "painotettu" dataframe using prep.fuzzy.var from ade4-package.
library("ade4", lib.loc="~/Library/R/3.0/library")
w <- prep.fuzzy.var(
painotettu,
c(5, 5, 6, 4, 3, 4, 2, 3, 3,
5, 5, 5, 5, 3, 6, 3, 5, 3)
)
scatter(dudi.fpca(w, scann = FALSE, nf = 3), csub = 2, type = 0, clab.moda = 2)
The result:
The result is very unclear and to sort it out, I have been trying to:
Separate the places into one picture and traits into another
Delete the text boxes so that the names could be seen more easily.
I'm also searching for some function that would compute a matrix that would give numerical values of what the arrows represent.
I'm trying to sort the data to look more like this:
Please let me know if any further clarification is needed.

Bokeh how to add legend to figure created by multi_line method?

I'm trying to add legend to a figure, which contains two lines created by multi_line method.
Example:
p = figure(plot_width=300, plot_height=300)
p.multi_line(xs=[[4, 2, 5], [1, 3, 4]], ys=[[6, 5, 2], [6, 5, 7]], color=['blue','yellow'], legend="first")
In this case the legend is only for the first line. When the legend is defined as a list there is an error:
p.multi_line(xs=[[4, 2, 5], [1, 3, 4]], ys=[[6, 5, 2], [6, 5, 7]], color=['blue','yellow'], legend=["first","second"])
Is it possible to add legend to many lines?
Maintainer Note : PR #8218 which will be merged for Bokeh 1.0, allows legends to be created directly for multi line and patches, without any looping.
To make it faster, when you have a lot of data or a big table etc. You can make a for loop:
1) Make a list of colors and legends
You can always import bokeh paletts for your colors
from bokeh.palettes import "your palett"
Check this link: bokeh.palets
colors_list = ['blue', 'yellow']
legends_list = ['first', 'second']
xs=[[4, 2, 5], [1, 3, 4]]
ys=[[6, 5, 2], [6, 5, 7]]
2) Your figure
p = figure(plot_width=300, plot_height=300)
3) Make a for loop throgh the above lists and show
for (colr, leg, x, y ) in zip(colors_list, legends_list, xs, ys):
my_plot = p.line(x, y, color= colr, legend= leg)
show(p)
Maintainer Note: PR #8218 which will be merged for Bokeh 1.0, allows legends to be created directly for multi line and patches, without any looping or using separate line calls.
multi_line is intended for conceptually single things, that happen to have multiple sub-components. Think of the state of Texas, it is one logical thing, but it has several distinct (and disjoint) polygons. You might use Patches to draw all the polys for "Texas" but you'd only want one legend overall. Legends label logical things. If you want to label several lines as logically distinct things, you will have to draw them all separately with p.line(..., legend_label="...")
On more recent releases (since 0.12.15, I think) its possible to add legends to multi_line plots. You simple need to add a 'legend' entry to your data source. Here is an example taken from the Google Groups discussion forum:
data = {'xs': [np.arange(5) * 1, np.arange(5) * 2],
'ys': [np.ones(5) * 3, np.ones(5) * 4],
'labels': ['one', 'two']}
source = ColumnDataSource(data)
p = figure(width=600, height=300)
p.multi_line(xs='xs', ys='ys', legend='labels', source=source)

ggplot heatmap failing to fill tiles

This (minimal, self-contained) example is broken:
require(ggplot2)
min_input = c(1, 1, 1, 2, 2, 2, 4, 4, 4)
input_range = c(4, 470, 1003, 4, 470, 1003, 4, 470, 1003)
density = c(
1.875000e-01,
5.598958e-04,
0.000000e+00,
1.250000e-02,
3.841146e-04,
0.000000e+00,
1.250000e-02,
1.855469e-04,
0.000000e+00)
df = data.frame(min_input, input_range, density)
pdf(file='problemspace.pdf')
ggplot(df, aes(x=min_input, y=input_range, fill=density)) +
geom_tile()
dev.off()
Producing:
Why are there big gaps?
There are gaps because you don't have data for all of the tiles. If you want to try to fill them in, your only option is to interpolate (assuming you don't have access to additional data). In theory, geom_raster() (a close relative of geom_tile()) supports interpolation. However, according to this github issue, that feature is not currently functional.
As a workaround, however, you can use qplot, which is just a wrapper around ggplot:
qplot(min_input, input_range, data=df, geom="raster", fill=density, interpolate=TRUE)
If there is too much space between the points that you have data for, you will still end up with blank spaces in your graph, but this will extend the range that you can estimate values for.
EDIT:
Based on the example that you posted, this will be the output
As you can see, there is a vertical band of white running through the middle, due to the lack of data points between 2 and 4.

How do I Order Boxes on One boxplot graph? (r)

I've read the answers to several questions of this type, so sorry for the repeat, but I had trouble understanding how they applied to how my data is laid out.
What I would like is to order the boxes on my boxplot (which has, say, four boxes) in the order I wish as opposed to alphabetically.
This is a simplified version of my current code:
TotalPer = c(1, 4, 6, 17, 4, 12)
IntPer = c(3, 8, 10, 1, 4, 8)
DomPer = c(4, 5, 10, 20, 13, 12)
IntDomBox <- data.frame(y=c(TotalPer,IntPer,DomPer),
x=c(rep("Total",length(TotalPer)),rep("International",length(IntPer)),
rep("Domestic",length(DomPer))))
with(IntDomBox, boxplot(y~x, main = "Prediction Residuals", ylab="%",
par(cex.axis=0.7)))
This produces a boxplot in alphabetical order. If I prefer to have the boxplot in the order listed in the dataframe (Total, Int, Dom) how would I do this? I know about making it reverse alphabetical instead, but in some cases that is not what I want, so I'd prefer to be able to manually assign the order.
Thanks!
One possibility out of many: Reorder the factor levels before plotting by executing IntDomBox$x <- factor(IntDomBox$x, levels=unique(IntDomBox$x))

Resources