matplotlib
matplotlib
import numpy as np
import pandas as pd
import matplotlib.pyplot 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']
plt.figure(figsize=(8, 5))
plt.bar(products, sales, color=colors)
plt.xlabel('Product Name')
plt.ylabel('Sales in USD')
plt.title('Sales Data for Products')
plt.show()
# 2. Histogram
student_scores = [45, 78, 89, 56, 90, 67, 88, 92, 55, 70]
plt.figure(figsize=(8, 5))
plt.hist(student_scores, bins=5, color='skyblue', edgecolor='black')
plt.xlabel('Score Ranges')
plt.ylabel('Frequency')
plt.title('Distribution of Student Scores')
plt.show()
# 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]
plt.figure(figsize=(8, 5))
plt.scatter(ages, incomes, c='red', alpha=0.7)
plt.xlabel('Age')
plt.ylabel('Income (USD)')
plt.title('Relationship Between Age and Income')
plt.show()
# 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']
plt.figure(figsize=(8, 5))
plt.pie(market_share, labels=companies, colors=colors, autopct='%1.1f%%', startangle=140)
plt.title('Market Share Distribution')
plt.axis('equal')
plt.show()
# 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]
plt.figure(figsize=(8, 5))
plt.stackplot(days, traffic_source1, traffic_source2, labels=['Source 1', 'Source 2'], colors=['lightblue',
'lightgreen'])
plt.xlabel('Days')
plt.ylabel('Visitors')
plt.title('Daily Website Traffic')
plt.legend(loc='upper left')
plt.show()
# 10. 3D Plotting
x = np.linspace(-5, 5, 50)
y = np.linspace(-5, 5, 50)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(x, y, z, cmap='viridis', edgecolor='none')
ax.contour(x, y, z, zdir='z', offset=-2, cmap='viridis')
ax.set_title('3D Surface Plot')
ax.view_init(30, 60)
fig.colorbar(surf)
plt.show()