import matplotlib.pyplot as plt
import numpy as np
# Sample data: Fuel types and corresponding average car prices
fuel_types = ['Petrol', 'Diesel', 'Electric', 'Hybrid', 'CNG']
avg_prices = [25000, 27000, 35000, 30000, 20000]
# Assign different colors for each fuel type
colors = ['skyblue', 'orange', 'green', 'red', 'purple']
# Create the bar plot
plt.figure(figsize=(8, 5))
bars = plt.bar(fuel_types, avg_prices, color=colors)
# Rotate x-axis labels dynamically if they are long
rotation_angle = 0 if max(len(f) for f in fuel_types) < 5 else 45
plt.xticks(rotation=rotation_angle)
# Add gridlines for better readability
plt.grid(axis='y', linestyle='--', alpha=0.7)
# Add price annotations on top of bars
for bar, price in zip(bars, avg_prices):
plt.text(bar.get_x() + bar.get_width()/2, price + 500, f"${price}", ha='center', fontsize=10, fontweight='bold')
# Title and labels
plt.title("Average Car Prices by Fuel Type", fontsize=14, fontweight='bold')
plt.xlabel("Fuel Type", fontsize=12)
plt.ylabel("Average Price ($)", fontsize=12)
# Show the plot
plt.show()
import matplotlib.pyplot as plt
import numpy as np
# Sample data
car_age = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
car_price = [30000, 27000, 25000, 22000, 20000, 17000, 15000, 13000, 11000, 9000]
trend_line = np.poly1d(np.polyfit(car_age, car_price, 1))(car_age)
# Create scatter plot
plt.figure(figsize=(8, 5))
plt.scatter(car_age, car_price, color='blue', marker='o', edgecolors='black', s=100, alpha=0.75, label='Car Price')
plt.plot(car_age, trend_line, color='red', linestyle='--', label='Trend Line')
# Labels and title
plt.xlabel("Car Age (Years)", fontsize=12, fontweight='bold')
plt.ylabel("Car Price ($)", fontsize=12, fontweight='bold')
plt.title("Scatter Plot of Car Price vs Age", fontsize=14, fontweight='bold')
plt.xticks(np.arange(0, 11, 1))
plt.yticks(np.arange(0, 35000, 5000))
# Add grid and legend
plt.grid(True, linestyle='--', alpha=0.6)
plt.legend()
# Show plot
plt.show()
///////////////////////////////////////////////////////EXPLANATION///////////////////
2.a
import matplotlib.pyplot as plt
import numpy as np
# Define the linear function y = mx + b
m = 2 # Slope
b = 5 # Intercept
# Generate x values
x = np.linspace(-10, 10, 100) # 100 points from -10 to 10
# Compute corresponding y values
y = m * x + b
# Create plot
plt.figure(figsize=(8, 5))
plt.plot(x, y, color='blue', linestyle='-', linewidth=2, label=f'y = {m}x + {b}')
# Labels and title
plt.xlabel("X-axis", fontsize=12, fontweight='bold')
plt.ylabel("Y-axis", fontsize=12, fontweight='bold')
plt.title("Linear Plot using Matplotlib", fontsize=14, fontweight='bold')
# Add grid and legend
plt.grid(True, linestyle='--', alpha=0.6)
plt.legend()
# Show plot
plt.show()
/////////
2.b
import matplotlib.pyplot as plt
import numpy as np
# Define multiple linear functions
x = np.linspace(-10, 10, 100) # Generate x values
# Function 1: y = 2x + 5
y1 = 2 * x + 5
# Function 2: y = -x + 3
y2 = -x + 3
# Function 3: y = 0.5x - 2
y3 = 0.5 * x - 2
# Create plot
plt.figure(figsize=(8, 5))
plt.plot(x, y1, color='blue', linestyle='-', linewidth=2, label='y = 2x + 5')
plt.plot(x, y2, color='red', linestyle='--', linewidth=2, label='y = -x + 3')
plt.plot(x, y3, color='green', linestyle='-.', linewidth=2, label='y = 0.5x - 2')
# Labels and title
plt.xlabel("X-axis", fontsize=12, fontweight='bold')
plt.ylabel("Y-axis", fontsize=12, fontweight='bold')
plt.title("Multiple Linear Plots using Matplotlib", fontsize=14, fontweight='bold')
# Add grid and legend
plt.grid(True, linestyle='--', alpha=0.6)
plt.legend()
# Show plot
plt.show()
3.
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
# Sample data: Simulated hourly flight counts
flight_data = {
'hour': list(range(24)),
'flight_count': [50, 30, 20, 15, 10, 5, 8, 12, 25, 60, 90, 120, 150, 160, 155, 140, 130, 110, 100, 80, 70, 60, 55, 50]
}
df = pd.DataFrame(flight_data)
# Create Histogram
fig = px.histogram(df, x='hour', y='flight_count', nbins=24,
title='Hourly Distribution of Flights',
labels={'hour': 'Hour of the Day', 'flight_count': 'Number of Flights'},
template='plotly_white')
# Add Line Plot
fig.add_trace(go.Scatter(x=df['hour'], y=df['flight_count'],
mode='lines+markers', name='Line Plot', line=dict(color='red')))
# Update layout
fig.update_layout(hovermode='x')
# Show interactive plot
fig.show()
4.
import pandas as pd
import plotly.express as px
# Sample flight data
flight_data = {
'Carrier': ['Airline A', 'Airline B', 'Airline C', 'Airline D', 'Airline E'],
'Flights': [250, 300, 150, 200, 100]
}
df = pd.DataFrame(flight_data)
# Create Bar Chart
fig = px.bar(df,
x='Carrier',
y='Flights',
title='Flight Distribution by Carrier',
color='Carrier',
text_auto=True,
template='plotly_white')
fig.show()
//////////////////////////////////
Horizontal Bar Chart
Justification:
✅ Great for large datasets → Easier to read when there are many carriers.
✅ Better label readability → Airline names don’t overlap as in vertical bars.
✅ Improves visual clarity → Long names fit better horizontally.
fig = px.bar(df,
x='Flights',
y='Carrier',
title='Flight Distribution by Carrier',
orientation='h',
color='Carrier',
text_auto=True,
template='plotly_white')
fig.show()
////////////////////////////
Treemap
Justification:
✅ Visually appealing → Uses nested rectangles instead of slices like a pie chart.
✅ Great for proportions → Similar to a pie chart but easier to compare.
✅ Can include subcategories → Example: Domestic vs. International flights.
fig = px.treemap(df,
path=['Carrier'],
values='Flights',
title='Flight Distribution by Carrier',
color='Flights',
color_continuous_scale='Blues')--//Uses a blue gradient
fig.show()
6.
from bokeh.plotting import figure, show
from bokeh.models import Label, HoverTool, Span, ColumnDataSource
from bokeh.layouts import layout
import pandas as pd
import numpy as np
# Generate sample time series data
dates = pd.date_range(start="2024-01-01", periods=50)
values1 = np.cumsum(np.random.randn(50)) + 20
values2 = np.cumsum(np.random.randn(50)) + 10
# Create data sources
source1 = ColumnDataSource(data={'x': dates, 'y': values1})
source2 = ColumnDataSource(data={'x': dates, 'y': values2})
# Initialize the figure
p = figure(title="Enhanced Time Series Line Plot with Annotations",
x_axis_type="datetime",
width=900,
height=450,
sizing_mode='stretch_width',
tools="pan,wheel_zoom,box_zoom,reset,save",
toolbar_location="above")
# Plot the two lines
line1 = p.line('x', 'y', source=source1, line_width=3, color='blue', legend_label='Metric A')
line2 = p.line('x', 'y', source=source2, line_width=3, color='green', legend_label='Metric B', line_dash="dashed")
# Add a hover tool
hover = HoverTool(
tooltips=[
("Date", "@x{%F}"),
("Value", "@y{0.2f}")
],
formatters={'@x': 'datetime'},
mode='vline'
)
p.add_tools(hover)
# Add an annotation at the maximum value of the first line
max_index = np.argmax(values1)
max_date = dates[max_index]
max_value = values1[max_index]
# Add label
label = Label(x=max_date, y=max_value, text=f"Peak: {max_value:.2f}",
text_font_size='10pt', background_fill_color='white',
text_color='black', text_baseline="bottom")
p.add_layout(label)
# Add vertical marker line
vline = Span(location=max_date.timestamp() * 1000, # convert datetime to ms timestamp
dimension='height', line_color='red',
line_dash='dotted', line_width=2)
p.add_layout(vline)
# Customize axes
p.xaxis.axis_label = "Date"
p.yaxis.axis_label = "Metric Value"
p.xaxis.axis_label_text_font_size = "12pt"
p.yaxis.axis_label_text_font_size = "12pt"
p.xaxis.major_label_orientation = 0.8
# Customize legend
p.legend.title = "Metrics"
p.legend.label_text_font_size = "10pt"
p.legend.title_text_font_style = "bold"
p.legend.location = "top_left"
p.legend.click_policy = "hide" # Click to hide/show lines
# Display the plot
show(layout([p], sizing_mode='stretch_width'))

No comments:
Post a Comment