You can download this code by clicking the button below.
This code is now available for download.
This function uses the Bokeh library to create a bar chart. It accepts data, x-axis and y-axis labels, and chart title as parameters. The data should be a dictionary containing x and y values, where x is the category and y is the corresponding value. The function creates a bar chart and saves it as an HTML file.
Technology Stack : Bokeh library; Data visualization; Bar chart; HTML file creation
Code Type : The type of code
Code Difficulty : Intermediate
def create_bar_chart(data, x_label, y_label, title):
from bokeh.plotting import figure, show
from bokeh.io import output_file, save
from bokeh.models import ColumnDataSource
# Create a ColumnDataSource from the data
source = ColumnDataSource(data=dict(x=data['x'], y=data['y']))
# Create a new plot with a title and axis labels
p = figure(title=title, x_axis_label=x_label, y_axis_label=y_label, plot_width=800, plot_height=600)
# Add a bar renderer to the plot
p.vbar(x='x', top='y', width=0.9, source=source)
# Save the plot to an HTML file
output_file("bar_chart.html")
save(p)
# Sample data
data = {
'x': ['A', 'B', 'C', 'D'],
'y': [28, 35, 45, 10]
}
# Function call to create the bar chart
create_bar_chart(data, 'Category', 'Values', 'Sample Bar Chart')