Basic Line Plot Using Matplotlib
Basic Line Plot Using Matplotlib
Function Description
plt.hist(data) Histogram
Function Description
Function Description
# 1. Line Plot
x=[2, 4, 6, 12, 9]
plt.figure(figsize=(6,4))
plt.plot(x, y, marker='o')
plt.xlabel("Day")
plt.ylabel("Stock Price")
plt.grid(True)
plt.show()
x---------------------------------------------------------------------x
# 2. Scatter Plot
study_hours = [1, 2, 3, 4, 5]
plt.figure(figsize=(6,4))
plt.xlabel("Study Hours")
plt.ylabel("Exam Score")
plt.grid(True)
plt.show()
x---------------------------------------------------------------------x
plt.figure(figsize=(6,4))
plt.bar(products, sales, color='green')
plt.xlabel("Product")
plt.ylabel("Sales")
plt.show()
x---------------------------------------------------------------------x
plt.figure(figsize=(6,4))
plt.xlabel("Population")
plt.ylabel("Country")
plt.show()
x---------------------------------------------------------------------x
# 5. Histogram
ages = [22, 25, 27, 25, 30, 35, 40, 23, 33, 25, 29, 40, 50]
plt.figure(figsize=(6,4))
plt.xlabel("Age")
plt.ylabel("Frequency")
plt.show()
x---------------------------------------------------------------------x
# 6. Pie Chart
plt.figure(figsize=(6,6))
x---------------------------------------------------------------------x
scores = [65, 70, 72, 68, 75, 80, 85, 90, 88, 95, 100]
plt.figure(figsize=(6,4))
plt.boxplot(scores)
plt.ylabel("Score")
plt.show()
x---------------------------------------------------------------------x
# 8. Stack Plot
plt.figure(figsize=(6,4))
plt.xlabel("Month")
plt.ylabel("Cost")
plt.legend(loc='upper left')
plt.show()
import numpy as np
days = np.arange(1, 6)
plt.xlabel("Day")
plt.ylabel("Price")
plt.legend()
plt.grid(True)
plt.show()
x---------------------------------------------------------------------x
study_hours = [1, 2, 3, 4, 5]
plt.figure(figsize=(6, 4))
plt.xlabel("Study Hours")
plt.ylabel("Score")
plt.grid(True)
plt.show()
x------------------------------------------------------------------------x
plt.title("Population by Country")
plt.xlabel("Population (Millions)")
plt.ylabel("Country")
plt.show()
Numpy
arr = np.arange(0, 11, 2)
x----------------------------------------------------------------x
arr = np.linspace(0, 1, 5)
x-----------------------------------------------------------------x
print("Sum:", np.sum(data))
print("Min:", np.min(data))
print("Max:", np.max(data))
Bar Chart Example
# Data
# Plot
plt.bar(categories, scores, color='skyblue')
plt.title('Student Scores')
plt.xlabel('Subjects')
plt.ylabel('Marks')
plt.show()
#Data
hours = [8, 9, 2, 5]
# Plot
plt.show()
python
CopyEdit
# Data
x = [5, 7, 8, 7, 2, 17, 2, 9]
# Plot
plt.title('Age vs Score')
plt.xlabel('Age')
plt.ylabel('Score')
plt.grid(True)
plt.show()
# Data
days = [1, 2, 3, 4, 5]
plt.title('Weather Report')
plt.xlabel('Day')
plt.ylabel('Measurement')
plt.legend()
plt.grid(True)
plt.show()