I know I can turn axes on and off in bokeh with p1.xaxis.visible = None or p1.yaxis.visible = None from Hide Axis in Bokeh. What if I have an extra y range I want to change the visibility of? I've defined my extra axis like this:
plot.extra_y_ranges = {'ORP': Range1d(start=0, end=plot_d['y_axis2_max'])}
plot.add_layout(LinearAxis(y_range_name='ORP', axis_label='ORP, mV'), 'left')
I tried plot.extra_y_ranges.visible = None but it has no effect, and I wasn't able to find anything in the documentation. Have I missed something?
You need to change the visibility of the lines, not the axis.
I've done this in a project on Github that displays temperature and humidity data (amongst other things). The humidity data is the extra y axis and I have check boxes to show/hide temperature and/or humidity. Here's the function that shows/hides the lines on the chart:
def h_t_lines_changed(self, active):
"""Helper function for h_t_tab - turns lines on and off"""
for index in range(len(self.h_t_line)):
self.h_t_line[index].visible = index in active
Here's the line definitions:
self.h_t_line[0] = self.h_t_fig.line(x='Timestamp',
y='Temperature (C)',
source=self.source,
color="blue",
legend="Temperature",
line_width=2)
self.h_t_line[1] = self.h_t_fig.line(x="Timestamp",
y="Relative humidity (%)",
source=self.source,
y_range_name="humidity",
color="green",
legend="Humidity",
line_width=2)
and here's the checkbox code, including the callback:
resp_b = [0, 1]
h_t_check_head = Div(text="Responses")
h_t_check = CheckboxGroup(labels=["Temperature", "Humidity"],
active=resp_b,
name="Lines")
h_t_check.on_click(self.h_t_lines_changed)
I'm updating my project now. If you want me to post a link to it, let me know.
Related
I'm trying to understand Shiny apps in particular this example of the bus dashboard located at
https://github.com/rstudio/shiny-examples/tree/master/086-bus-dashboard
I'd like to modify the update rule so colors are displayed depending on whether or not locations$Bearing >= 200.
I tried modifying the locations dataframe which has Bearing but can't figure out how to chnage the color on map when the leaflet map is updated. It looks like it may have something to do with color = ~dirPal(Direction)
addTiles('http://{s}.tile.thunderforest.com/transport/{z}/{x}/{y}.png') %>%
addCircleMarkers(
~VehicleLongitude,
~VehicleLatitude,
color = ~dirPal(Direction),
opacity = 0.8,
radius = 16
)```
where dirColors is defined at beginning of script
dirColors <-c("1"="#595490", "2"="#527525", "3"="#A93F35", "4"="#BA48AA", "5" = "#BA48AA")
I'd like to basically add a color here but conditionally displayed only for buses where Bearing >= 200 in location dataframe.
routeVehicleLocations <- reactive({
if (is.null(input$routeNum))
return()
locations <- vehicleLocations()
anomalies <- locations[locations$Bearing > 200,]
if (as.numeric(input$routeNum) == 0)
return(locations)
locations[locations$Route == input$routeNum, ]
})
The rest of the app is fine but would like to overwrite the color if condition above is met (Bearing >=200). Eventually would like to replace this rule with my own model and own data sources.
I'm facing issues with multi-line figure in bokeh. i can't get the values when i show my graph. i'm using series data type.
Code:
df = pandas.read_csv("Data.csv", parse_dates=["time"])
result = df.groupby(['time','up','down'], as_index = False)['up', 'down'].sum()
p = Figure(width=500, height=250,logo =None,
sizing_mode='scale_width',
tools="pan, box_zoom, wheel_zoom, save, reset, box_select",
x_axis_type="datetime",
title="Graph:",
x_axis_label="Time Frame",
y_axis_label="Utilization (GB)",
toolbar_location="below",
toolbar_sticky=False)
up = result["up"]
time = result["time"]
down = result["down"]
p.multi_line(xs = [time, time], ys = [up, down], color=['#2828B0', '#BC0096'], line_width=1, legend='graph_1')
hover = HoverTool(tooltips = [('Time', '#time'),
('data', '#up')])
p.add_tools(hover)
p.show()
The # fields of hover tooltips refer to columns in Bokeh ColumnDataSource objects. Since you have not created a CDS explicitly with column names of your choice, Bokeh makes one for you with the standard column name xs and ys in this case. So:
HoverTool(tooltips = [
('Time', '#xs'),
('data', '#ys')]
)
That will put a hover that displays over all segments in the multi-line. There is no way to have a hover work for just one or the other. If you need that, you will have to use separate calls to line instead of multi_line.
I have tried to follow this tutorial (https://shuaiw.github.io/2016/12/22/topic-modeling-and-tsne-visualzation.html) of visualizing LDA with t-sne and bokeh.
But i run into a bit of problem.
When i tried to run the following code:
plot_lda.scatter(x=tsne_lda[:, 0], y=tsne_lda[:, 1],
color=colormap[_lda_keys][:num_example],
source=bp.ColumnDataSource({
"content": text[:num_example],
"topic_key": _lda_keys[:num_example]
}))
NB: In the tutorial the content is called news, in mine it is called text
i get this error:
Supplying a user-defined data source AND iterable values to glyph methods is
not possibe. Either:
Pass all data directly as literals:
p.circe(x=a_list, y=an_array, ...)
Or, put all data in a ColumnDataSource and pass column names:
source = ColumnDataSource(data=dict(x=a_list, y=an_array))
p.circe(x='x', y='x', source=source, ...)
To me this do not make so much sense and i have not succeded in finding any annswer to it ethier here, github or else where. Hope that some on can help. best Niels
I've been also battling with that piece of code and I've found two problems with it.
First, when you pass a source to the scatter function, like the error states, you must include all data in the dictionary, i.e., x and y axes, colors, labels, and any other information that you want to include in the tooltip.
Second, the x and y axes have a different shape than the information passed to the tooltip, so you also have to slice both arrays in the axes with the num_example variable.
The following code got me running:
# create the dictionary with all the information
plot_dict = {
'x': tsne_lda[:num_example, 0],
'y': tsne_lda[:num_example, 1],
'colors': colormap[_lda_keys][:num_example],
'content': text[:num_example],
'topic_key': _lda_keys[:num_example]
}
# create the dataframe from the dictionary
plot_df = pd.DataFrame.from_dict(plot_dict)
# declare the source
source = bp.ColumnDataSource(data=plot_df)
title = 'LDA viz'
# initialize bokeh plot
plot_lda = bp.figure(plot_width=1400, plot_height=1100,
title=title,
tools="pan,wheel_zoom,box_zoom,reset,hover,previewsave",
x_axis_type=None, y_axis_type=None, min_border=1)
# build scatter function from the columns of the dataframe
plot_lda.scatter('x', 'y', color='colors', source=source)
I am trying to implement an interactive dashboard in Bokeh with a "play" function that loops through all value pairs for two indicators selected by widgets.
Screen cap of dashboard
While the loop works, the dashboard resets the axis values for each step of the loop. So what I need is to set axis values based on the widget.value selected. To this end, I have built a data frame "ranges" that has the name of the indicator as index and the min/max value for each indicator as columns.
The updates for controls work thusly (x_axis,etc. are the names of the widgets):
controls = [x_axis, y_axis, start_yr, end_yr, years]
for control in controls:
control.on_change('value', lambda attr, old, new: update())
The update function is supposed to update the ranges upon change in the controls like this:
def update():
p.x_range = Range1d(start = ranges.loc[x_axis.value,"Min"],
end = ranges.loc[x_axis.value,"Max"])
p.y_range = Range1d(start = ranges.loc[y_axis.value,"Min"],
end = ranges.loc[y_axis.value,"Max"])
What should happen: Whenever I change the value of the widget, the ranges should update, but other than that, they should remain constant
What does happen: The ranges are set based on the value of the widget initially set and don't change on update.
I've tried to find examples trying to achieve something similar but no luck.
This is a working example:
import numpy as np
from bokeh.plotting import figure
from bokeh.models import Range1d
from bokeh.io import curdoc
x = np.linspace(0, 100, 1000)
y = np.sin(x)
p = figure(x_range=(0, 100))
p.circle(x, y)
def cb():
# this works:
p.x_range.start += 1
p.x_range.end += 1
# this also works:
#p.x_range = Range1d(p.x_range.start+1, p.x_range.end+1)
curdoc().add_periodic_callback(cb, 200)
curdoc().add_root(p)
Begin Edit
After initial post I continued playing with my code. In my subplots I am making four plots of the same data set, with each subplot having a different time range. However, if I give each subplot the SAME time range then the minor ticks do not disappear. This may be why Deditos could not reproduce my issue.
That being said, if I manually create each subplot (with each having a different x-axis range), set the minor tick locations, THEN set each subplot's xrange I do not see the minor ticks disappear until I set ax3's (i.e. the last subplot) range.
It seems the issue is in having different x-axis ranges. Bizarre, I would think that by setting each axis' properties individually they would not all be tied together.
End Edit
I am creating one figure that has four sub-plots, all of which are time series. I have the xaxis major ticks spaced every four hours, and want minor ticks every hour. When I set the minor ticks for the first subplot (called ax1) the minor ticks appear, as they should. However, when I set the minor ticks in ax2 they show up in ax2, but the minor ticks in ax1 disappear. This repeats for ax3, and ax4. So, in the end I have only minor xaxis ticks in the fourth subplot. I had the same problem with the yaxis, but resolved this issue using yaxis.set_minor_locator(MultipleLocator(5)) for each axis (see below). However, MultipleLocator does not seem to work for time series data. Does anyone know how I can keep my minor xaxis ticks?
from pylab import *
from matplotlib.ticker import AutoMinorLocator, MultipleLocator
minor = AutoMinorLocator()
# Start plotting
fig = figure( figsize=(22,11) )
ax1 = fig.add_subplot(221) # 8-August 2011
ax2 = fig.add_subplot(222) # 9-August 2011
ax3 = fig.add_subplot(223) # 23-August 2011
ax4 = fig.add_subplot(224) # 24-August 2011
# This is repeated for ax2, ax3, and ax4, yielding a 2x2 grid of subplots.
# Plot 8-August 2011 data
ax1.plot(tpan.index,tpan.no2,'.-',markersize=10)
ax1.errorbar(tacam.index,tacam.no2,yerr=0.15,fmt='r.',markersize=12)
# Format plots
suptitle('Pandora/ACAM NO$_2$ Comparison', fontsize=22)
# Define xtick locations/string labels
xtickloc = [dt.datetime.combine(dates[0],dt.time())+dt.timedelta(hours=h) for h in range(0,25,4)]
xticklab = [dt.datetime.strftime(h,'%H:%M') for h in xtickloc]
ax1.set_xlabel('Hour of Day (UTC, EST+5)',fontsize=14)
ax1.set_ylabel('NO$_2$ Column Density (molec*cm$^{-2}$ E16)',fontsize=14)
ax1.xaxis.set_ticks(xtickloc)
ax1.yaxis.set_ticks(linspace(0,1.5,7))
ax1.xaxis.set_minor_locator(minor)
ax1.yaxis.set_minor_locator(MultipleLocator(5))
ax1.set_xticklabels(xticklab,fontsize=12,fontweight='bold')
ax1.set_yticklabels(linspace(0,1.5,7),fontsize=12,fontweight='bold')
ax1.axis( (dates[0],dates[0]+dt.timedelta(days=1),-0.05,1.5),fontsize=6,fontweight='bold')
ax1.tick_params(which='both',width=2,top='on')
ax1.tick_params(which='major',length=7)
ax1.tick_params(which='minor',length=4)
ax1.grid(linestyle='-',which='major',linewidth=1)
ax1.set_title('08-August 2011',fontsize=16)
ax1.legend( ('Pandora VCD','ACAM dSCD'),loc=2,ncol=2)
I was facing the same problem. I think what you need to do is:
ax1.xaxis.set_minor_locator(AutoMinorLocator())
instead of
ax1.xaxis.set_minor_locator(minor)
You are passing the same object to each of your axes. This object's contents are modified when you plot on ax4 based on the range on that subplot. Hope it helps.