How to draw a categorical bar chart using the vbar method in the Bokeh build module

I want to draw a bar graph using the vbar method in building Bokeh, where the x axis takes categorical values, not numeric ones. The example presented on the manual page ( http://bokeh.pydata.org/en/latest/docs/reference/plotting.html# ) has only the numerical axis x.

The stroke line should be updated through the widget, and therefore it seems that Bar () cannot be used, but instead I tried to use the vbar () method, where I can transfer the source data.

I found several similar questions and answers from the story, but still they don't seem to reflect the problem I have.

I tried the following code snippet, but it made a mistake with some errors:

from bokeh.plotting import figure, output_file
from bokeh.io import show
from bokeh.models import ColumnDataSource, ranges
from bokeh.plotting import figure
import pandas as pd

output_file("test_bar_plot.html")

dat = pd.DataFrame([['A',20],['B',20],['C',30]], columns=['category','amount'])

source = ColumnDataSource(dict(x=[],y=[]))

x_label = "Category"
y_label = "Amount"
title = "Test bar plot"

plot = figure(plot_width=600, plot_height=300,
        x_axis_label = x_label,
        y_axis_label = y_label,
        title=title
        )

plot.vbar(source=source,x='x',top='y',bottom=0,width=0.3)

def update():
        source.data = dict(
            x = dat.category,
            y = dat.amount
        )
        plot.x_range = source.data['x']

update()

show(plot)

, , x_range figure(), , , - , , x_range .

, .

+1
1

, x_range.

plot = figure(plot_width=600, plot_height=300,
              x_axis_label=x_label,
              y_axis_label=y_label,
              title=title,
              x_range=FactorRange(factors=list(dat.category))
              )

x_range .

def update():
        source.data = dict(
            x = dat.category,
            y = dat.amount
        )
        plot.x_range.factors = list(source.data['x'])
+2

Source: https://habr.com/ru/post/1683202/


All Articles