Panning Bokeh GMap programmatically - python-3.x

Is it possible to adjust the centre coordinate of a bokeh google map plot and refresh the plot? I'm intending to centre the selected point according to user input in a bokeh TextInput field.
A piece of example code for displaying the map is shown below.
source = ColumnDataSource(data=dict(<...>))
origin = (<...>, <...>)
options = GMapOptions(lat=origin[0], lng=origin[1], map_type="roadmap", zoom=11)
tools = "crosshair,pan,wheel_zoom,reset,save"
location_map = gmap(environ.get('API_KEY'), options, title="Station locations", tools=tools, active_scroll="wheel_zoom")
r = location_map.circle(x="lng", y="lat", size=15, fill_color='color', fill_alpha=0.8, source=source)

Okay this is kind of old, but I think I found the answer. I know that GMapPlot used in Bokeh has an update function that allows you to update existing parameters. I'm pretty sure that gmap is very similar to GMapPlot so...
options = GMapOptions(lat=origin[0], lng=origin[1], map_type="roadmap", zoom=11)
location_map = gmap(environ.get('API_KEY'), map_options=options, title="Station locations", tools=tools, active_scroll="wheel_zoom")
updated_options = GMapOptions(lat=new_lat, lng=new_lng, zoom=new_zoom)
location_map.update(map_options=updated_options)
Sorry if the formatting is off. It's my first post.

Related

Is there a way to display the value of a mark next to the mark in Altair

I was playing around with the following example from the Altair Gallery:
https://altair-viz.github.io/gallery/airports_count.html
As of right now, the only way to display the actual count appears to be via the tooltip, as the example shows. However, I am trying to code a static visualization for which it would be very helpful if the exact value was displayed right next to the mark itself, without the user having to hover or interact in any way. Is there a way to achieve this?
You can do this by manually calculating offsets for text labels, though this is admittedly difficult when the points become crowded:
import altair as alt
from vega_datasets import data
airports = data.airports.url
states = alt.topo_feature(data.us_10m.url, feature='states')
# US states background
background = alt.Chart(states).mark_geoshape(
fill='lightgray',
stroke='white'
).properties(
width=500,
height=300
).project('albersUsa')
# airport positions on background
base = alt.Chart(airports).transform_aggregate(
latitude='mean(latitude)',
longitude='mean(longitude)',
count='count()',
groupby=['state']
).encode(
longitude='longitude:Q',
latitude='latitude:Q',
)
points = base.mark_circle().encode(
size=alt.Size('count:Q', title='Number of Airports'),
color=alt.value('steelblue'),
tooltip=['state:N','count:Q']
).properties(
title='Number of airports in US'
)
text = base.mark_text(
dx=15, dy=10
).encode(
text='count:Q'
)
background + points + text
Long-term, a better solution will be to use vega-label, which will be able to do this automatically once it's part of the Vega-Lite package. For Altair, this feature is tracked in this bug: https://github.com/altair-viz/altair/issues/1731

Altair chart not showing up in Streamlit with VConcat

I tried to project altair chart using streamlit. My requirement is to project two charts in such a way that if i select few points in the above chart with scattered points i should see the distribution of a variable('notes') in the below chart. For that i have written the below code where i am using vconcat in the function. But, The chart never shows up when i use vconcat. But, It works fine when i try to project single chart.
def altair_graph(embd_1):
selected = alt.selection_single(on="click", empty="none")
brush = alt.selection(type='interval')
dom = ['Other IDs', 'Slected ID','Sel Dims']
rng_clr = ['lightgrey', 'red','blue']
color_point=alt.Color('color', scale=alt.Scale(domain=dom, range=rng_clr))
color = alt.condition(selected, alt.value('red'), color_point,legend=None)
chart = alt.Chart(embd_1).mark_circle(size=30).encode(
x = 'dimention1:Q',
y = 'dimention2:Q',
tooltip=['ID','notes'] ,
color=color
).properties(width=600,height=600).add_selection(brush).add_selection(selected).interactive()
bars = alt.Chart(embd_1).mark_bar().encode(
y='notes:N',
color='notes:N',
x='count(notes):Q'
).transform_filter(brush).interactive()
#final_chart = ((chart & bars))
final_chart = alt.vconcat(chart,bars)
return final_chart
selected=altair_component(altair_chart=altair_graph(embd_1))
From your snippet I assume that you are using the altair-part of the streamlit-vega-lite custom component. Currently it seems like it is not possible to use the streamlit-vega-lite component to retrieve selections from compound charts.
That said, it is not entirely clear to me, why the chart is not showing at all. And without a minimal reproducible example, we can't test. I had a similar case lately, where it worked to plot the charts both, separately, as well as together as a compound. Also the selections work as such, however, values are not reflected back in the event dict that gets returned from the altair_component

Creating layout PDFs by iterating feature selection and setting extent

In ArcGIS Pro 2.4, I need to create a PDF page of a map layout where the map frame is zoomed to each row in a feature class. Each feature in this class is a polygon. I'm relatively new to ArcPy so I'm learning as I'm going.
So far I've been messing with arcpy.SearchCursor, to iterate the selection of the features. Inside the cursor, I need to use mf.camera.setExtent(mf.getLayerExtent(selectedfeature)) and mf.camera.scale *= 1.05 so the polygon shows its surroundings for context. Then I've been trying to export the layout (lyt) to a PDF somewhere. There are 700 of these polgyons (each labelled as a alphanumerical map page) so it's best to do this with arcpy.
import arcpy
aprx = arcpy.mp.ArcGISProject(r"G:\ArcGIS Projects\project.aprx")
m = aprx.listMaps("Map")[0]
lyr = m.listLayers("PLSS Quarter Sections*")[0]
lyt = aprx.listLayouts("Paper Maps*")[0]
mf = lyt.listElements("MAPFRAME_ELEMENT", "Sewer Sections*")[0]
fc = "PLSS Quarter Sections"
fields = ['OBJECTID']
cursor = arcpy.SearchCursor(fc)
row = cursor.next()
for row in cursor:
mf.camera.setExtent(mf.getLayerExtent(row, True, False))
mf.camera.scale *= 1.05
lyt.exportToPDF(r"G:\ArcGIS Projects\output.pdf")
It should move the map frame inside the layout to the selected feature, zoom out, and save that layout to a PDF. All it does it give a TypeError: 1. How would I go about doing this?
After asking around, ArcGIS Pro has a feature called Map Series that lets you make a series of maps based off a certain layer. In my case, it created 700 maps with a single polygon framed in the map frame in my layout. It's also much easier than scripting it by hand.

Change color and legend of plotLearnerPrediction ggplot2 object

I've been producing a number of nice plots with the plotLearnerPrediction function in the mlr package for R. They look like this. From looking into the source code of the plotLearnerPrediction function it looks like the color surfaces are made with geom_tile.
A plot can for example be made by:
library(mlr)
data(iris)
#make a learner
lrn <- "classif.qda"
#make a task
my.task <- makeClassifTask(data = iris, target = "Species")
#make plot
plotLearnerPrediction(learner = lrn, task = my.task)
Now I wish to change the colors, using another red, blue and green tone to match those of some other plots that I've made for a project. for this I tried scale_fill_continuous and scale_fill_manual without any luck (Error: Discrete value supplied to continuous scale) I also wish to change the legend title and the labels for each legend entry (Which I tried giving appropriate parameters to the above scale_fill's). There's a lot of info out there on how to set the geom_tile colours when producing the plot, but I haven't found any info on how to do this post-production (i.e. in somebody else's plot object). Any help would be much appreciated.
When you look into the source code you see how the plot is generated and then you can see which scale has to be overwritten or set.
In this example it's fairly easy:
g = plotLearnerPrediction(learner = lrn, task = my.task)
library(ggplot2)
g + scale_fill_manual(values = c(setosa = "yellow", versicolor = "blue", virginica = "red"))

two textplots in one plot

I have been trying to work with textplot in R and am unsure if my question is possible or not, I know that par() can't be used to place two textplots in one plot. I have been using a page and this code to try and figure things out.
My question is: Is it possible to have two textplots within the same plot?
For example, in the par(mfrow=c(1,1)) scenario below, plot 1 is a texplot of species length. Say I wanted to replicate that textplot twice in that plot. Is that possible?
based on this site:
http://svitsrv25.epfl.ch/R-doc/library/gplots/html/textplot.html
textplot(version)
data(iris)
par(mfrow=c(1,1))
info <- sapply( split(iris$Sepal.Length, iris$Species),
function(x) round(c(Mean=mean(x), SD=sd(x), N=gdata::nobs(x)),2) )
textplot( info, valign="top" )
title("Sepal Length by Species")
What I want to do is put a second textplot within that plot, underneath the original. For arguments sake, replicating that textplot twice in the plot.
Is this possible?
Thanks!
Maybe you've figured it out in the last four months but I thought I'd chip in an answer anyway.
The code provided is most of the way towards doing what you require already, you just have to provide some additional inputs to title() and/or par(). Namely specify that the title is to be above both of the plots by using title("your title", outer = TRUE) and you can further adjust the position of the title with an option in par(), use par(mfrow = c(2,1), oma = c(0,0,"top",0)). Hopefully this answers your question.
require('gplots')
data(iris)
info <- sapply(split(iris$Sepal.Length, iris$Species),
function(x) round(c(Mean = mean(x), SD = sd(x), N = gdata::nobs(x)),2))
## Replace top with a numerical value to control the position of the title with respect to the
## top of the page.
par(mfrow = c(2,1), oma = c(0,0, top ,0))
textplot(info, valign = "top")
textplot(info, valign = "top")
title("Sepal Length by Species", outer = TRUE)

Resources