6.
b) Write a Python program to illustrate liner plotting with line formatting using
Matplotlib
import [Link] as plt
import numpy as np
# Generate sample data
x = [Link](0, 10, 100) # Generate 100 points between 0 and 10
y = 2 * x + 5 # Linear equation: y = 2x + 5
# Plot the data with line formatting
[Link](x, y, color='blue', linestyle='--', linewidth=2, marker='o', label='Linear Function')
# Add labels and title
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Linear Plot with Line Formatting')
# Add a grid
[Link](True)
# Add a legend
[Link]()
# Show the plot
[Link]()
OR
import [Link] as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Plotting with line formatting
[Link](x, y, marker='o', linestyle='--', color='b', label='Linear Plot')
# Adding labels and title
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Linear Plot with Line Formatting')
# Adding a legend
[Link]()
# Display the plot
[Link]()
7. Write a Python program which explains uses of customizing seaborn plots with
Aesthetic functions.
import seaborn as sns
import [Link] as plt
# Load a sample dataset
tips = sns.load_dataset("tips")
# Set the style for the entire session
[Link](style="whitegrid")
# Create a scatter plot with customizations
[Link](figsize=(8, 6))
scatter_plot = [Link](x="total_bill", y="tip", hue="day", data=tips, palette="Set2",
size="size", sizes=(20, 200))
# Set plot title and axis labels
[Link]('Scatter Plot of Total Bill vs Tip Amount')
[Link]('Total Bill ($)')
[Link]('Tip Amount ($)')
# Customize legend
scatter_plot.legend(title='Day', loc='upper left', bbox_to_anchor=(1, 1))
# Customize axis ticks
[Link](rotation=45)
# Customize grid
[Link](axis='both', linestyle='--', alpha=0.7)
# Show the plot
[Link]()
In this example, we use the scatterplot function from Seaborn to create a scatter plot of the
"total_bill" vs "tip" from the "tips" dataset. Aesthetic functions are then used to customize
various aspects of the plot:
[Link](style="whitegrid"): Sets the overall style of the plot to a white grid background.
palette="Set2": Specifies the color palette to use for the plot.
size="size" and sizes=(20, 200): Specifies the column to use for marker size and the range
of sizes.
[Link], [Link], [Link]: Set plot title and axis labels.
scatter_plot.legend: Customizes the legend by setting its title, position, and anchor point.
[Link](rotation=45): Rotates x-axis labels for better readability.
[Link]: Adds a grid with a dashed line style and reduced opacity.
This program demonstrates how to use aesthetic functions in Seaborn to create a visually
appealing and informative scatter plot
Data set
"total_bill","tip","sex","smoker","day","time","size"
16.99,1.01,"Female","No","Sun","Dinner",2
10.34,1.66,"Male","No","Sun","Dinner",3
21.01,3.5,"Male","No","Sun","Dinner",3
23.68,3.31,"Male","No","Sun","Dinner",2
24.59,3.61,"Female","No","Sun","Dinner",4
25.29,4.71,"Male","No","Sun","Dinner",4
8.77,2,"Male","No","Sun","Dinner",2
26.88,3.12,"Male","No","Sun","Dinner",4
15.04,1.96,"Male","No","Sun","Dinner",2
14.78,3.23,"Male","No","Sun","Dinner",2
10.27,1.71,"Male","No","Sun","Dinner",2
35.26,5,"Female","No","Sun","Dinner",4
15.42,1.57,"Male","No","Sun","Dinner",2
18.43,3,"Male","No","Sun","Dinner",4
14.83,3.02,"Female","No","Sun","Dinner",2
21.58,3.92,"Male","No","Sun","Dinner",2
10.33,1.67,"Female","No","Sun","Dinner",3
16.29,3.71,"Male","No","Sun","Dinner",3
16.97,3.5,"Female","No","Sun","Dinner",3
20.65,3.35,"Male","No","Sat","Dinner",3
17.92,4.08,"Male","No","Sat","Dinner",2
20.29,2.75,"Female","No","Sat","Dinner",2
15.77,2.23,"Female","No","Sat","Dinner",2
39.42,7.58,"Male","No","Sat","Dinner",4
19.82,3.18,"Male","No","Sat","Dinner",2
17.81,2.34,"Male","No","Sat","Dinner",4
13.37,2,"Male","No","Sat","Dinner",2
12.69,2,"Male","No","Sat","Dinner",2
21.7,4.3,"Male","No","Sat","Dinner",2
19.65,3,"Female","No","Sat","Dinner",2
9.55,1.45,"Male","No","Sat","Dinner",2
18.35,2.5,"Male","No","Sat","Dinner",4
15.06,3,"Female","No","Sat","Dinner",2
20.69,2.45,"Female","No","Sat","Dinner",4
17.78,3.27,"Male","No","Sat","Dinner",2
24.06,3.6,"Male","No","Sat","Dinner",3
16.31,2,"Male","No","Sat","Dinner",3
16.93,3.07,"Female","No","Sat","Dinner",3
18.69,2.31,"Male","No","Sat","Dinner",3
31.27,5,"Male","No","Sat","Dinner",3
16.04,2.24,"Male","No","Sat","Dinner",3
17.46,2.54,"Male","No","Sun","Dinner",2
13.94,3.06,"Male","No","Sun","Dinner",2
9.68,1.32,"Male","No","Sun","Dinner",2
30.4,5.6,"Male","No","Sun","Dinner",4
18.29,3,"Male","No","Sun","Dinner",2
22.23,5,"Male","No","Sun","Dinner",2
32.4,6,"Male","No","Sun","Dinner",4
28.55,2.05,"Male","No","Sun","Dinner",3
18.04,3,"Male","No","Sun","Dinner",2
12.54,2.5,"Male","No","Sun","Dinner",2
10.29,2.6,"Female","No","Sun","Dinner",2
34.81,5.2,"Female","No","Sun","Dinner",4
9.94,1.56,"Male","No","Sun","Dinner",2
25.56,4.34,"Male","No","Sun","Dinner",4
19.49,3.51,"Male","No","Sun","Dinner",2
38.01,3,"Male","Yes","Sat","Dinner",4
26.41,1.5,"Female","No","Sat","Dinner",2
11.24,1.76,"Male","Yes","Sat","Dinner",2
48.27,6.73,"Male","No","Sat","Dinner",4
20.29,3.21,"Male","Yes","Sat","Dinner",2
13.81,2,"Male","Yes","Sat","Dinner",2
11.02,1.98,"Male","Yes","Sat","Dinner",2
18.29,3.76,"Male","Yes","Sat","Dinner",4
17.59,2.64,"Male","No","Sat","Dinner",3
20.08,3.15,"Male","No","Sat","Dinner",3
16.45,2.47,"Female","No","Sat","Dinner",2
3.07,1,"Female","Yes","Sat","Dinner",1
20.23,2.01,"Male","No","Sat","Dinner",2
15.01,2.09,"Male","Yes","Sat","Dinner",2
12.02,1.97,"Male","No","Sat","Dinner",2
17.07,3,"Female","No","Sat","Dinner",3
26.86,3.14,"Female","Yes","Sat","Dinner",2
25.28,5,"Female","Yes","Sat","Dinner",2
14.73,2.2,"Female","No","Sat","Dinner",2
10.51,1.25,"Male","No","Sat","Dinner",2
17.92,3.08,"Male","Yes","Sat","Dinner",2
27.2,4,"Male","No","Thur","Lunch",4
22.76,3,"Male","No","Thur","Lunch",2
17.29,2.71,"Male","No","Thur","Lunch",2
19.44,3,"Male","Yes","Thur","Lunch",2
16.66,3.4,"Male","No","Thur","Lunch",2
10.07,1.83,"Female","No","Thur","Lunch",1
32.68,5,"Male","Yes","Thur","Lunch",2
15.98,2.03,"Male","No","Thur","Lunch",2
34.83,5.17,"Female","No","Thur","Lunch",4
13.03,2,"Male","No","Thur","Lunch",2
18.28,4,"Male","No","Thur","Lunch",2
24.71,5.85,"Male","No","Thur","Lunch",2
21.16,3,"Male","No","Thur","Lunch",2
28.97,3,"Male","Yes","Fri","Dinner",2
22.49,3.5,"Male","No","Fri","Dinner",2
5.75,1,"Female","Yes","Fri","Dinner",2
16.32,4.3,"Female","Yes","Fri","Dinner",2
22.75,3.25,"Female","No","Fri","Dinner",2
40.17,4.73,"Male","Yes","Fri","Dinner",4
27.28,4,"Male","Yes","Fri","Dinner",2
12.03,1.5,"Male","Yes","Fri","Dinner",2
21.01,3,"Male","Yes","Fri","Dinner",2
12.46,1.5,"Male","No","Fri","Dinner",2
11.35,2.5,"Female","Yes","Fri","Dinner",2
15.38,3,"Female","Yes","Fri","Dinner",2
44.3,2.5,"Female","Yes","Sat","Dinner",3
22.42,3.48,"Female","Yes","Sat","Dinner",2
20.92,4.08,"Female","No","Sat","Dinner",2
15.36,1.64,"Male","Yes","Sat","Dinner",2
20.49,4.06,"Male","Yes","Sat","Dinner",2
25.21,4.29,"Male","Yes","Sat","Dinner",2
18.24,3.76,"Male","No","Sat","Dinner",2
14.31,4,"Female","Yes","Sat","Dinner",2
14,3,"Male","No","Sat","Dinner",2
7.25,1,"Female","No","Sat","Dinner",1
38.07,4,"Male","No","Sun","Dinner",3
23.95,2.55,"Male","No","Sun","Dinner",2
25.71,4,"Female","No","Sun","Dinner",3
17.31,3.5,"Female","No","Sun","Dinner",2
29.93,5.07,"Male","No","Sun","Dinner",4
10.65,1.5,"Female","No","Thur","Lunch",2
12.43,1.8,"Female","No","Thur","Lunch",2
24.08,2.92,"Female","No","Thur","Lunch",4
11.69,2.31,"Male","No","Thur","Lunch",2
13.42,1.68,"Female","No","Thur","Lunch",2
14.26,2.5,"Male","No","Thur","Lunch",2
15.95,2,"Male","No","Thur","Lunch",2
12.48,2.52,"Female","No","Thur","Lunch",2
29.8,4.2,"Female","No","Thur","Lunch",6
8.52,1.48,"Male","No","Thur","Lunch",2
14.52,2,"Female","No","Thur","Lunch",2
11.38,2,"Female","No","Thur","Lunch",2
22.82,2.18,"Male","No","Thur","Lunch",3
19.08,1.5,"Male","No","Thur","Lunch",2
20.27,2.83,"Female","No","Thur","Lunch",2
11.17,1.5,"Female","No","Thur","Lunch",2
12.26,2,"Female","No","Thur","Lunch",2
18.26,3.25,"Female","No","Thur","Lunch",2
8.51,1.25,"Female","No","Thur","Lunch",2
10.33,2,"Female","No","Thur","Lunch",2
14.15,2,"Female","No","Thur","Lunch",2
16,2,"Male","Yes","Thur","Lunch",2
13.16,2.75,"Female","No","Thur","Lunch",2
17.47,3.5,"Female","No","Thur","Lunch",2
34.3,6.7,"Male","No","Thur","Lunch",6
41.19,5,"Male","No","Thur","Lunch",5
27.05,5,"Female","No","Thur","Lunch",6
16.43,2.3,"Female","No","Thur","Lunch",2
8.35,1.5,"Female","No","Thur","Lunch",2
18.64,1.36,"Female","No","Thur","Lunch",3
11.87,1.63,"Female","No","Thur","Lunch",2
9.78,1.73,"Male","No","Thur","Lunch",2
7.51,2,"Male","No","Thur","Lunch",2
14.07,2.5,"Male","No","Sun","Dinner",2
13.13,2,"Male","No","Sun","Dinner",2
17.26,2.74,"Male","No","Sun","Dinner",3
24.55,2,"Male","No","Sun","Dinner",4
19.77,2,"Male","No","Sun","Dinner",4
29.85,5.14,"Female","No","Sun","Dinner",5
48.17,5,"Male","No","Sun","Dinner",6
25,3.75,"Female","No","Sun","Dinner",4
13.39,2.61,"Female","No","Sun","Dinner",2
16.49,2,"Male","No","Sun","Dinner",4
21.5,3.5,"Male","No","Sun","Dinner",4
12.66,2.5,"Male","No","Sun","Dinner",2
16.21,2,"Female","No","Sun","Dinner",3
13.81,2,"Male","No","Sun","Dinner",2
17.51,3,"Female","Yes","Sun","Dinner",2
24.52,3.48,"Male","No","Sun","Dinner",3
20.76,2.24,"Male","No","Sun","Dinner",2
31.71,4.5,"Male","No","Sun","Dinner",4
10.59,1.61,"Female","Yes","Sat","Dinner",2
10.63,2,"Female","Yes","Sat","Dinner",2
50.81,10,"Male","Yes","Sat","Dinner",3
15.81,3.16,"Male","Yes","Sat","Dinner",2
7.25,5.15,"Male","Yes","Sun","Dinner",2
31.85,3.18,"Male","Yes","Sun","Dinner",2
16.82,4,"Male","Yes","Sun","Dinner",2
32.9,3.11,"Male","Yes","Sun","Dinner",2
17.89,2,"Male","Yes","Sun","Dinner",2
14.48,2,"Male","Yes","Sun","Dinner",2
9.6,4,"Female","Yes","Sun","Dinner",2
34.63,3.55,"Male","Yes","Sun","Dinner",2
34.65,3.68,"Male","Yes","Sun","Dinner",4
23.33,5.65,"Male","Yes","Sun","Dinner",2
45.35,3.5,"Male","Yes","Sun","Dinner",3
23.17,6.5,"Male","Yes","Sun","Dinner",4
40.55,3,"Male","Yes","Sun","Dinner",2
20.69,5,"Male","No","Sun","Dinner",5
20.9,3.5,"Female","Yes","Sun","Dinner",3
30.46,2,"Male","Yes","Sun","Dinner",5
18.15,3.5,"Female","Yes","Sun","Dinner",3
23.1,4,"Male","Yes","Sun","Dinner",3
15.69,1.5,"Male","Yes","Sun","Dinner",2
19.81,4.19,"Female","Yes","Thur","Lunch",2
28.44,2.56,"Male","Yes","Thur","Lunch",2
15.48,2.02,"Male","Yes","Thur","Lunch",2
16.58,4,"Male","Yes","Thur","Lunch",2
7.56,1.44,"Male","No","Thur","Lunch",2
10.34,2,"Male","Yes","Thur","Lunch",2
43.11,5,"Female","Yes","Thur","Lunch",4
13,2,"Female","Yes","Thur","Lunch",2
13.51,2,"Male","Yes","Thur","Lunch",2
18.71,4,"Male","Yes","Thur","Lunch",3
12.74,2.01,"Female","Yes","Thur","Lunch",2
13,2,"Female","Yes","Thur","Lunch",2
16.4,2.5,"Female","Yes","Thur","Lunch",2
20.53,4,"Male","Yes","Thur","Lunch",4
16.47,3.23,"Female","Yes","Thur","Lunch",3
26.59,3.41,"Male","Yes","Sat","Dinner",3
38.73,3,"Male","Yes","Sat","Dinner",4
24.27,2.03,"Male","Yes","Sat","Dinner",2
12.76,2.23,"Female","Yes","Sat","Dinner",2
30.06,2,"Male","Yes","Sat","Dinner",3
25.89,5.16,"Male","Yes","Sat","Dinner",4
48.33,9,"Male","No","Sat","Dinner",4
13.27,2.5,"Female","Yes","Sat","Dinner",2
28.17,6.5,"Female","Yes","Sat","Dinner",3
12.9,1.1,"Female","Yes","Sat","Dinner",2
28.15,3,"Male","Yes","Sat","Dinner",5
11.59,1.5,"Male","Yes","Sat","Dinner",2
7.74,1.44,"Male","Yes","Sat","Dinner",2
30.14,3.09,"Female","Yes","Sat","Dinner",4
12.16,2.2,"Male","Yes","Fri","Lunch",2
13.42,3.48,"Female","Yes","Fri","Lunch",2
8.58,1.92,"Male","Yes","Fri","Lunch",1
15.98,3,"Female","No","Fri","Lunch",3
13.42,1.58,"Male","Yes","Fri","Lunch",2
16.27,2.5,"Female","Yes","Fri","Lunch",2
10.09,2,"Female","Yes","Fri","Lunch",2
20.45,3,"Male","No","Sat","Dinner",4
13.28,2.72,"Male","No","Sat","Dinner",2
22.12,2.88,"Female","Yes","Sat","Dinner",2
24.01,2,"Male","Yes","Sat","Dinner",4
15.69,3,"Male","Yes","Sat","Dinner",3
11.61,3.39,"Male","No","Sat","Dinner",2
10.77,1.47,"Male","No","Sat","Dinner",2
15.53,3,"Male","Yes","Sat","Dinner",2
10.07,1.25,"Male","No","Sat","Dinner",2
12.6,1,"Male","Yes","Sat","Dinner",2
32.83,1.17,"Male","Yes","Sat","Dinner",2
35.83,4.67,"Female","No","Sat","Dinner",3
29.03,5.92,"Male","No","Sat","Dinner",3
27.18,2,"Female","Yes","Sat","Dinner",2
22.67,2,"Male","Yes","Sat","Dinner",2
17.82,1.75,"Male","No","Sat","Dinner",2
18.78,3,"Female","No","Thur","Dinner",2
8. Write a Python program for plotting different types of plots using Bokeh.
from [Link] import figure, show
from [Link] import output_file
from [Link] import ColumnDataSource
from [Link] import Category10_4
from [Link] import gridplot
import numpy as np
# Output to an HTML file
output_file("bokeh_plots.html")
# Example 1: Line Plot
x = [Link](0, 4 * [Link], 100)
y = [Link](x)
plot1 = figure(title="Line Plot", width=400, height=300)
[Link](x, y, line_width=2, line_color="blue")
# Example 2: Scatter Plot
x = [Link](100)
y = [Link](100)
plot2 = figure(title="Scatter Plot", width=400, height=300)
[Link](x, y, size=8, color="green", alpha=0.8)
# Example 3: Bar Plot
fruits = ['Apple', 'Banana', 'Orange', 'Grapes']
counts = [30, 25, 40, 20]
source = ColumnDataSource(data=dict(fruits=fruits, counts=counts, color=Category10_4))
plot3 = figure(title="Bar Plot", x_range=fruits, width=400, height=300)
[Link](x='fruits', top='counts', width=0.8, color='color', legend_field="fruits",
source=source)
# Example 4: Grid Plot
grid = gridplot([[plot1, plot2], [None, plot3]])
# Show the plots
show(grid)
Bokeh is an interactive visualization library for Python that enables users to create high-
quality, web-based data visualizations. It’s particularly useful for creating detailed plots,
dashboards, and applications that require real-time interactivity and customization
Key Features of Bokeh
1. Interactivity: Bokeh supports various interactive tools like zooming, panning,
selecting, and hovering. This makes it ideal for data exploration, allowing users to
interact with data directly within the plots.
2. Web-Based: Bokeh generates visualizations as JavaScript and HTML, which means
the plots can be embedded directly into web pages or dashboards and viewed in any
modern web browser.
3. Flexible and Scalable: Bokeh can handle small to large datasets efficiently. It is
capable of rendering complex, high-performance visualizations suitable for a range of
applications, from exploratory data analysis to enterprise-level dashboards.
4. Integration with Web Applications: Bokeh can be used as part of a web application
framework like Flask or Django and can also be embedded in Jupyter notebooks,
making it versatile for both standalone scripts and large applications.
5. Pythonic Syntax: Bokeh's syntax is intuitive and Pythonic, which makes it accessible
to data scientists, analysts, and developers who are familiar with Python. It’s
comparable to Matplotlib or Seaborn in terms of ease of use but is specifically
designed for creating interactive and web-based visuals.
In this program, we create four different types of plots using Bokeh:
Line Plot: Plots a sine wave using the line function.
Scatter Plot: Generates random data points and plots them using the circle function.
Bar Plot: Creates a bar plot with fruit counts using the vbar function.
Grid Plot: Combines the three plots into a grid layout using the gridplot function.
The resulting plots are saved to an HTML file (bokeh_plots.html) and can be opened in a web
browser. You can customize the plots further by adjusting parameters and exploring the
Bokeh documentation for additional features and options.
Bokeh is an interactive visualization library for Python that enables users to create high-
quality, web-based data visualizations. It’s particularly useful for creating detailed plots,
dashboards, and applications that require real-time interactivity and customization
Key Features of Bokeh
6. Interactivity: Bokeh supports various interactive tools like zooming, panning,
selecting, and hovering. This makes it ideal for data exploration, allowing users to
interact with data directly within the plots.
7. Web-Based: Bokeh generates visualizations as JavaScript and HTML, which means
the plots can be embedded directly into web pages or dashboards and viewed in any
modern web browser.
8. Flexible and Scalable: Bokeh can handle small to large datasets efficiently. It is
capable of rendering complex, high-performance visualizations suitable for a range of
applications, from exploratory data analysis to enterprise-level dashboards.
9. Integration with Web Applications: Bokeh can be used as part of a web application
framework like Flask or Django and can also be embedded in Jupyter notebooks,
making it versatile for both standalone scripts and large applications.
10. Pythonic Syntax: Bokeh's syntax is intuitive and Pythonic, which makes it accessible
to data scientists, analysts, and developers who are familiar with Python. It’s
comparable to Matplotlib or Seaborn in terms of ease of use but is specifically
designed for creating interactive and web-based visuals.
9. Write a Python program to draw 3D Plots using Plotly Libraries.
import plotly.graph_objects as go
# Create a 3D scatter plot
trace = go.Scatter3d(
x=[1, 2, 3, 4, 5],
y=[5, 6, 7, 8, 9],
z=[10, 11, 12, 13, 14],
mode='markers',
marker=dict(
size=12,
color='blue', # set color to an array/list of desired values
colorscale='Viridis', # choose a colorscale
opacity=0.8
)
)
# Create layout
layout = [Link](
scene=dict(
xaxis=dict(title='X-axis'),
yaxis=dict(title='Y-axis'),
zaxis=dict(title='Z-axis'),
),
margin=dict(l=0, r=0, b=0, t=0)
)
# Create figure
fig = [Link](data=[trace], layout=layout)
# Show the plot
[Link]()
output:
10. a Write a Python program to draw Time Series using Plotly Libraries.
import plotly.graph_objects as go
from datetime import datetime
import pandas as pd
# Sample time series data
date_strings = ['2024-01-01', '2024-01-02', '2024-01-03', '2024-01-04', '2024-01-05']
dates = [[Link](date, '%Y-%m-%d') for date in date_strings]
values = [10, 15, 13, 17, 20]
# Create a DataFrame
df = [Link]({'Date': dates, 'Values': values})
# Create a time series plot
trace = [Link](x=df['Date'], y=df['Values'], mode='lines+markers', name='Time
Series')
# Create layout
layout = [Link](
title='Time Series Plot',
xaxis=dict(title='Date'),
yaxis=dict(title='Values'),
showlegend=True
)
# Create figure
fig = [Link](data=[trace], layout=layout)
# Show the plot
[Link]()
10. b Write a Python program for creating Maps using Plotly Libraries
import [Link] as px
# Sample data for the map
data = dict(
lat=[37.7749, 40.7128, 34.0522], # Latitude coordinates
lon=[-122.4194, -74.0060, -118.2437], # Longitude coordinates
text=['San Francisco', 'New York City', 'Los Angeles'], # Text labels for points
)
# Create a map figure
fig = px.scatter_geo(
data,
lat='lat',
lon='lon',
text='text',
title='Simple Map Example',
)
# Show the map
[Link]()