8 a) Write a Python program to explain working with bokeh line graph
using Annotations and Legends.
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, Label, LabelSet
from bokeh.io import output_file
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Create a Bokeh figure
p = figure(title="Line Graph with Annotations and Legends",
x_axis_label='X-axis', y_axis_label='Y-axis')
# Create a data source
source = ColumnDataSource(data={'x': x, 'y': y})
# Create a line plot
p.line('x', 'y', source=source, legend_label='Line Plot', line_color='blue',
line_width=2)
# Create annotations
label = Label(x=3, y=6, x_units='data', y_units='data', text='Annotation',
background_fill_color='lightgray', background_fill_alpha=0.7)
p.add_layout(label)
# Create legends
p.legend.title = 'Legend'
p.legend.label_text_font_size = '12pt'
p.legend.background_fill_alpha = 0.7
# Create a label set
labels = LabelSet(x='x', y='y', text='y', level='glyph', source=source,
text_baseline='middle',
text_align='center', text_font_size='12pt')
p.add_layout(labels)
# Output the plot to an HTML file
output_file('line_graph_with_annotations_and_legends.html')
# Show the plot
show(p)
8 b) Write a Python program for plotting different types of plots using
Bokeh.
from bokeh.plotting import figure, show, output_file
from math import pi
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Create a Bokeh figure
output_file("bokeh_plots.html")
# Create a line plot
p1 = figure(title="Line Plot", x_axis_label='X-axis', y_axis_label='Y-axis')
p1.line(x, y, line_color='blue', line_width=2)
# Create a scatter plot
p2 = figure(title="Scatter Plot", x_axis_label='X-axis', y_axis_label='Y-axis')
p2.circle(x, y, size=10, color='green', alpha=0.6)
# Sample data for bar plot
categories = ['Category A', 'Category B', 'Category C', 'Category D']
values = [30, 45, 15, 10]
# Create a bar plot
p3 = figure(x_range=categories, title="Bar Plot", x_axis_label='Categories',
y_axis_label='Values')
p3.vbar(x=categories, top=values, width=0.5, color='orange')
# Sample data for a pie chart
data = {'Categories': ['Category A', 'Category B', 'Category C', 'Category D'],
'Values': [30, 45, 15, 10]}
angles = [pi/4, pi/2, pi, 1.5*pi]
# Create a pie chart
p4 = figure(title="Pie Chart")
p4.wedge(x=0, y=0, radius=0.4, start_angle=angles, end_angle=angles[1:] +
[2*pi],
line_color="white", fill_color=['red', 'green', 'blue', 'orange'],
legend_label="Categories",
source=data)
# Show the plots
show(p1)
show(p2)
show(p3)
show(p4)