NOTE FROM BOKEH MAINTAINER: The MPL compatibility layer in Bokeh was deprecated and removed a long time ago. Nothing in this question is relevant to any recent or future versions of Bokeh.
I have a a violin plot created with mpl and bokeh which uses a dataframe as a data source. Example:
ax = sns.violinplot(x="Week of", y="Total Time",data=df, palette="muted", split=False, scale="count", inner="box", bw=0.1)
Then I create my plot with plot = mpl.to_bokeh()
The X axis are dates. I want to dynamically update this bokeh plot (the values being used on the x axis) when a user changes a slider widget. For example I have the slider:
beginSlider = Slider(start=0, end=10, value=1, step=.1, title="Start Date", callback=callback)
When the user changes the date on the slider I want the data source for the plot to be changed thus updating the dates on the xaxis of the plot. I am familiar with bokeh callbacks, however because the source for this plot is a dataframe and not a ColumnDataSource I can not figure out how to trigger a change for the data source/plot in a widget callback. Any thoughts?
Related
The instructions from this question don't work for Seaborn FacetPlots. Would it be possible to get the method to do the same?
A facetgrid legend is not part of the axes, but part of the facetgrid object. The legend is still a standard matplotlib legend and can be manipulated as such.
plt.setp(g._legend.get_title(), fontsize=20)
Where g is your facetgrid object returned after you call the function making it.
If you're using a newer version of matplotlib there's an easier way to change legend font sizes -
plt.legend(fontsize='x-large', title_fontsize='40')
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.legend.html
Might depend on the version of matplotlib you're using. I'm using 2.2.3 and it has the fontsize parameter but not the title_fontsize.
As in the linked answer you may use setp to set the properties (in this case the fontsize of the legend).
The only difference to the linked question is that you need to do that for each axes of the FacetGrid
g = FacetGrid( ... )
for ax in g.axes.flat:
plt.setp(ax.get_legend().get_texts(), fontsize=22) # for legend text
plt.setp(ax.get_legend().get_title(), fontsize=32) # for legend title
I am trying to add certain callbacks to a circles which are plotted on bokeh plot. Each circle is associated with certain record from columndatasource. I want to access that record whenever corresponding circle is clicked. Is there any way to add callbacks to circles in bokeh?
How can i do it?
I am using following code
fig =figure(x_range=(-bound, bound), y_range=(-bound, bound),
plot_width=800, plot_height=500,output_backend="webgl")
fig.circle(x='longitude',y='latitude',size=2,source=source,fill_color="blue",
fill_alpha=1, line_color=None)
Then you want to add an on_change callback to the selected property of the data source. Here is a minimal example. As stated above, python callbacks require the Bokeh server (that is where python callbacks actually get run, since the browser knows nothing of python), so this must be run e.g. bokeh serve --show example.py (Or, if you are in a notebook, following the pattern in this example notebook).
# example.py
from bokeh.io import curdoc
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
source = ColumnDataSource(data=dict(x=[1,2,3], y=[4,6,5]))
p = figure(title="select a circle", tools="tap")
p.circle('x', 'y', size=25, source=source)
def callback(attr, old, new):
# This uses syntax for Bokeh >= 0.13
print("Indices of selected circles: ", source.selected.indices)
source.selected.on_change('indices', callback)
curdoc().add_root(p)
I'm new to Bokeh and was wondering if anyone could lend a little help tell me why my plot is not updating? The code is very simple, and can be found here:
http://pastebin.com/MLAigEG6
The code is just supposed to grab some data using the function "get_dataset", plot a bar chart, and let me update the plot using a dropdown box and slider. The two small dataframes can be found here:
https://github.com/degravek/bdata
The slider is set to default at 15 (30 total values plotted). If the slider is moved, or if the dropdown box is changed, the axes for the plot don't update for some reason. For example, if the slider is set to 2, there should only be 2 bars shown, and the axes should adjust accordingly. Thanks a lot for taking a look.
Nice code. In your update function, you also need to update the x_range.factors of the plot. And global asdata is not needed here.
def update_samples_or_dataset(attrname, old, new):
dataset = dataset_select.value
n_samples = int(samples_slider.value)
asdata = get_dataset(dataset, n_samples)
plot.x_range.factors = asdata['aspects'].tolist() # this was missing
source.data = dict(x=asdata['aspects'].tolist(), y=asdata['importance'].values)
Is it possible to show and update Pandas plots in Bokeh without using show()? Are there any examples of this online? I can't seem to find any. For example, something like:
def bar_plot(fig, source):
p = pd.DataFrame()
p = p.from_dict(source.data)
fig = p.plot.bar()
return fig
def update_data():
data = source.data
data['y'] = random.sample(range(0,100),len(data['y']))
source.data = data
button.on_click(update_data)
source = ColumnDataSource(data)
fig = bar_plot(fig, source)
layout = layout([[button,fig]])
curdoc().add_root(layout)
Pandas' built-in .plot method uses Matplotlib to generate images. The Bokeh server has no way of synchronizing or updating MPL plots across the Python/JS boundary. The Bokeh server can only show and update plots created using native Bokeh APIs (i.e. you can create a bar plot from your data frame using Figure.vbar or similar Bokeh functions).
I am trying to Create a bokeh bar chart using Python. The data2 is the values
from bokeh.plotting import figure, output_file, show,hplot
from bokeh.charts import Bar
data2=[65.75, 48.400000000000006, 58.183333333333337, 14.516666666666666]
bar = Bar(values=data2,xlabel="beef,pork,fowl,fish",ylabel="Avg consumtion", width=400)
show(bar)
Error
TypeError: Bar() takes at least 1 argument (1 given)
What am I doing wrong Here?
The bokeh.charts API (including Bar) was deprecated and removed from Bokeh in 2017. It is unmaintained and unsupported and should not be used for any reason at this point. A simple bar chart can be accomplished using the well-supported bokeh.plotting API:
from bokeh.plotting import figure, show
categories = ["beef", "pork", "fowl", "fish"]
data = [65.75, 48.4, 58.183, 14.517]
p = figure(x_range=categories)
p.vbar(x=categories, top=data, width=0.9)
show(p)
For more information on the vastly improved support for bar and categorical plots in bokeh.plotting, see the extensive user guide section Handling Categorical Data
Note from Bokeh project maintainers: This answer refers to an obsolete and deprecated API. For information about creating bar charts with modern and fully supported Bokeh APIs, see the other response.
You might want to put all of your data in a data frame.
To directly plot what you want without doing that, you need to remove the "values" keyword.
bar = Bar(data2,xlabel="beef,pork,fowl,fish",ylabel="Avg consumtion", width=400)
This won't add your x-labels though. To do that you can do the following:
from bokeh.plotting import figure, output_file, show,hplot
from bokeh.charts import Bar
from pandas import DataFrame as df
data2=[65.75, 48.400000000000006, 58.183333333333337, 14.516666666666666]
myDF = df(
dict(
data=data2,
label=["beef", "pork", "fowl", "fish"],
))
bar = Bar(myDF, values="data",label="label",ylabel="Avg consumption", width=400)
show(bar)