Ex 5: Matplotlib
import numpy as np
import pandas as pd
import [Link] as plt
from mpl_toolkits.mplot3d import Axes3D
# 1. Bar Plot
products = ['Product A', 'Product B', 'Product C', 'Product D', 'Product E']
sales = [250, 400, 300, 450, 500]
colors = ['blue', 'green', 'red', 'purple', 'orange']
[Link](figsize=(8, 5))
[Link](products, sales, color=colors)
[Link]('Product Name')
[Link]('Sales in USD')
[Link]('Sales Data for Products')
[Link]()
# 2. Histogram
student_scores = [45, 78, 89, 56, 90, 67, 88, 92, 55, 70]
[Link](figsize=(8, 5))
[Link](student_scores, bins=5, color='skyblue', edgecolor='black')
[Link]('Score Ranges')
[Link]('Frequency')
[Link]('Distribution of Student Scores')
[Link]()
# 3. Scatter Plot
ages = [22, 25, 30, 35, 40, 45, 50, 55, 60, 65]
incomes = [2500, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 11000]
[Link](figsize=(8, 5))
[Link](ages, incomes, c='red', alpha=0.7)
[Link]('Age')
[Link]('Income (USD)')
[Link]('Relationship Between Age and Income')
[Link]()
# 4. Pie Plot
companies = ['Company A', 'Company B', 'Company C', 'Company D', 'Company E']
market_share = [20, 25, 30, 15, 10]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue', 'pink']
[Link](figsize=(8, 5))
[Link](market_share, labels=companies, colors=colors, autopct='%1.1f%%', startangle=140)
[Link]('Market Share Distribution')
[Link]('equal')
[Link]()
# 5. Hexagonal Bin Plot
x = [Link](1000)
y = [Link](1000)
[Link](figsize=(8, 5))
[Link](x, y, gridsize=30, cmap='Blues')
[Link](label='Density')
[Link]('Hexagonal Bin Plot')
[Link]('X-axis')
[Link]('Y-axis')
[Link]()
# 6. Area Plot
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
traffic_source1 = [120, 150, 100, 180, 200, 300, 250]
traffic_source2 = [80, 100, 60, 120, 150, 200, 150]
[Link](figsize=(8, 5))
[Link](days, traffic_source1, traffic_source2, labels=['Source 1', 'Source 2'], colors=['lightblue',
'lightgreen'])
[Link]('Days')
[Link]('Visitors')
[Link]('Daily Website Traffic')
[Link](loc='upper left')
[Link]()
# 7. Plotting with Pandas
data = {'Date': pd.date_range(start='2023-01-01', periods=10),
'Stock Price': [150, 152, 148, 145, 155, 160, 165, 170, 175, 180]}
df = [Link](data)
[Link](figsize=(8, 5))
[Link](df['Date'], df['Stock Price'], marker='o', linestyle='-', color='blue')
[Link]('Date')
[Link]('Stock Price (USD)')
[Link]('Stock Price Trends')
[Link](rotation=45)
[Link]()
# 8. Plotting with NumPy
x = [Link](0, 2 * [Link], 100)
y = [Link](x)
[Link](figsize=(8, 5))
[Link](x, y, label='sin(x)', color='blue')
[Link]('x')
[Link]('sin(x)')
[Link]('Sine Wave')
[Link]()
[Link]()
# 9. Plotting Multiple Subplots
x = [Link](0, 2 * [Link], 100)
sin_y = [Link](x)
cos_y = [Link](x)
[Link](figsize=(10, 6))
# Sine subplot
[Link](2, 1, 1)
[Link](x, sin_y, label='sin(x)', color='blue')
[Link]('Sine Function')
[Link]()
# Cosine subplot
[Link](2, 1, 2)
[Link](x, cos_y, label='cos(x)', color='red')
[Link]('Cosine Function')
[Link]()
plt.tight_layout()
[Link]()
# 10. 3D Plotting
x = [Link](-5, 5, 50)
y = [Link](-5, 5, 50)
x, y = [Link](x, y)
z = [Link]([Link](x**2 + y**2))
fig = [Link](figsize=(10, 6))
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(x, y, z, cmap='viridis', edgecolor='none')
[Link](x, y, z, zdir='z', offset=-2, cmap='viridis')
ax.set_title('3D Surface Plot')
ax.view_init(30, 60)
[Link](surf)
[Link]()