0% found this document useful (0 votes)
2 views

matplotlib

The document provides a series of examples demonstrating various plotting techniques using Matplotlib in Python, including bar plots, histograms, scatter plots, pie charts, hexagonal bin plots, area plots, and 3D surface plots. Each example includes code snippets that visualize different types of data, such as sales figures, student scores, age vs. income, market share, and stock price trends. The document also showcases how to create multiple subplots and use NumPy and Pandas for data visualization.

Uploaded by

vishnurajhanmn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

matplotlib

The document provides a series of examples demonstrating various plotting techniques using Matplotlib in Python, including bar plots, histograms, scatter plots, pie charts, hexagonal bin plots, area plots, and 3D surface plots. Each example includes code snippets that visualize different types of data, such as sales figures, student scores, age vs. income, market share, and stock price trends. The document also showcases how to create multiple subplots and use NumPy and Pandas for data visualization.

Uploaded by

vishnurajhanmn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Ex 5: 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()

# 5. Hexagonal Bin Plot


x = np.random.randn(1000)
y = np.random.randn(1000)
plt.figure(figsize=(8, 5))
plt.hexbin(x, y, gridsize=30, cmap='Blues')
plt.colorbar(label='Density')
plt.title('Hexagonal Bin Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
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()

# 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 = pd.DataFrame(data)
plt.figure(figsize=(8, 5))
plt.plot(df['Date'], df['Stock Price'], marker='o', linestyle='-', color='blue')
plt.xlabel('Date')
plt.ylabel('Stock Price (USD)')
plt.title('Stock Price Trends')
plt.xticks(rotation=45)
plt.show()
# 8. Plotting with NumPy
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
plt.figure(figsize=(8, 5))
plt.plot(x, y, label='sin(x)', color='blue')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.title('Sine Wave')
plt.legend()
plt.show()

# 9. Plotting Multiple Subplots


x = np.linspace(0, 2 * np.pi, 100)
sin_y = np.sin(x)
cos_y = np.cos(x)
plt.figure(figsize=(10, 6))
# Sine subplot
plt.subplot(2, 1, 1)
plt.plot(x, sin_y, label='sin(x)', color='blue')
plt.title('Sine Function')
plt.legend()
# Cosine subplot
plt.subplot(2, 1, 2)
plt.plot(x, cos_y, label='cos(x)', color='red')
plt.title('Cosine Function')
plt.legend()
plt.tight_layout()
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()

You might also like