LAB PROGRAM 08 (a)
8 a) Write a Python program to explain working with bokeh line graph using Annotations
and Legends.
pip install Bokeh
PROGRAM
from bokeh.plotting import figure, output_file, show
from bokeh.models import Legend, LegendItem, Title, Span
# Output to an HTML file
output_file("line_graph_with_annotations.html")
# Sample data
x = [1, 2, 3, 4, 5]
y1 = [2, 5, 7, 2, 8]
y2 = [1, 4, 5, 3, 6]
# Create a Bokeh figure
p = figure(title="Line Graph with Annotations and Legends",
x_axis_label='X-axis', y_axis_label='Y-axis')
# Add lines to the figure
line1 = p.line(x, y1, line_width=2, color="blue", legend_label="Line
1")
line2 = p.line(x, y2, line_width=2, color="red", legend_label="Line
2")
# Add annotations (vertical line and a text label)
annotation = Span(location=3, dimension='width', line_color='black',
line_width=8)
# Add the annotation to the figure
p.add_layout(annotation)
# Create a legend
legend = Legend(items=[
LegendItem(label="Line 1", renderers=[line1]),
LegendItem(label="Line 2", renderers=[line2]),
])
p.add_layout(legend)
# Show the plot
show(p)
OUTPUT
8 b) Write a Python program for plotting different types of plots using Bokeh.
PROGRAM
import numpy as np
from bokeh.io import output_file
from bokeh.io import show
from bokeh.layouts import row, column
from bokeh.plotting import figure
# Create a new plot object.
fig = figure(plot_width=500, plot_height=300, title='Different Types
of Plots')
# Add data to the plot.
x = np.linspace(0, 10, 100)
y = np.sin(x)
# Add a line glyph to the plot.
fig.line(x, y, line_width=2, legend_label='Line Plot')
# Add a bar chart to the plot.
fig.vbar(x=x, top=y, legend_label='Bar Chart', width=0.5, bottom=0)
# Add a scatter plot to the plot.
fig.circle(x, y, size=10, color='red', legend_label='Scatter Plot')
# Customize the plot appearance.
fig.xaxis.axis_label = 'X Axis'
fig.yaxis.axis_label = 'Y Axis'
fig.legend.location = 'top_left'
# Show the plot.
show(fig)
OUTPUT
9) Write a Python program to draw 3D Plots using Plotly Libraries.
PROGRAM
import plotly.graph_objects as go
import plotly.offline as pyo
import pandas as pd
pubg = pd.read_csv('D:\dvwithpython_lab\Custom-Data-PUBG.csv')
#pubg.head(10)
df_pubg = pubg.apply(pd.to_numeric, errors='ignore')
df_new_pubg = df_pubg.head(10)
df_bar_pubg = df_pubg.head(30)
x = df_bar_pubg['solo_Wins']
y = df_bar_pubg['solo_TimeSurvived']
z = df_bar_pubg['solo_RoundsPlayed']
trace1 = go.Scatter3d(
x=x,
y=y,
z=z,
mode='markers',
marker=dict(
size=12,
color=z,
colorscale='Viridis',
opacity=0.8))
data = [trace1]
layout = go.Layout(
margin=dict(
l=0,
r=0,
b=0,
t=0)
fig = go.Figure(data=data, layout=layout)
pyo.plot(fig, filename='3d-pubg-plot.html')
OUTPUT
10 a) Write a Python program to draw Time Series using Plotly Libraries.
PROGRAM
import plotly.graph_objects as go
import plotly.offline as pyo
import pandas as pd
# Sample time series data
tesla = pd.read_csv("D:\dvwithpython_lab\Time-Series-Tesla-Stock-
Price.csv")
trace_one = go.Scatter(
x=tesla.date,
y=tesla['high'],
name="Tesla High",
line=dict(color='#17BECF'),
opacity = 0.8)
trace_two = go.Scatter(
x=tesla.date,
y=tesla['low'],
name="Tesla Low",
line=dict(color='#7F7F7F'),
opacity = 0.8)
data = [trace_one, trace_two]
layout = dict (
title = 'Tesla Stock Price - High vs Low')
fig = dict(data=data, layout=layout)
pyo.plot(fig, filename = 'Tesla Stock Comparison.html')
OUTPUT
10 b) Write a Python program for creating Maps using Plotly Libraries.
PROGRAM
#import libraries
import pandas as pd
import plotly.express as px
#import data
data = pd.read_csv('D:\\dvwithpython_lab\\2011_us_ag_exports.csv')
# create choropleth map for the data
# color will be the column to be color-coded
# locations is the column with sppatial coordinates
fig = px.choropleth(data, locations='code',
locationmode="USA-states",
color='total exports', scope="usa")
fig.show()
OUTPUT