Advance Python Program Unit III
Advance Python Program Unit III
1. Line Chart
Used for visualizing trends over time or continuous data.
import matplotlib.pyplot as plt
# Example data
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 35]
plt.plot(x, y, label='Trend', color='blue')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Line Chart')
plt.legend()
plt.show()
2. Bar Chart
Ideal for comparing quantities across categories.
categories = ['A', 'B', 'C', 'D']
values = [3, 7, 5, 8]
plt.bar(categories, values, color='purple')
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Chart')
plt.show()
3. Histogram
Used for showing the distribution of data.
data = [1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5]
plt.hist(data, bins=5, color='green', edgecolor='black')
plt.xlabel('Bins')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.show()
4. Scatter Plot
Visualizes relationships between two variables.
x = [5, 7, 8, 9, 10]
y = [10, 14, 15, 19, 21]
plt.scatter(x, y, color='red')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Scatter Plot')
plt.show()
5. Pie Chart
Displays proportions between categories.
sizes = [20, 30, 25, 25]
labels = ['Category A', 'Category B', 'Category C', 'Category D']
colors = ['gold', 'lightblue', 'lightgreen', 'pink']
plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
plt.title('Pie Chart')
plt.show()
6.Line Chart and subplots
Let's combine a line chart with subplots to showcase multiple visualizations
within the same figure! Here's an example using Matplotlib:
Code Example:
import matplotlib.pyplot as plt
# Data for Line Charts
x1 = [1, 2, 3, 4, 5]
y1 = [10, 20, 30, 40, 50]
x2 = [1, 2, 3, 4, 5]
y2 = [50, 40, 30, 20, 10]
# Create Subplots
fig, axes = plt.subplots(2, 1, figsize=(8, 6)) # 2 rows, 1 column
# First Line Chart (subplot 1)
axes[0].plot(x1, y1, color='blue', label='Line 1')
axes[0].set_title('Line Chart 1')
axes[0].set_xlabel('X-axis')
axes[0].set_ylabel('Y-axis')
axes[0].legend()
# Second Line Chart (subplot 2)
axes[1].plot(x2, y2, color='red', linestyle='dashed', label='Line 2')
axes[1].set_title('Line Chart 2')
axes[1].set_xlabel('X-axis')
axes[1].set_ylabel('Y-axis')
axes[1].legend()
# Adjust layout
plt.tight_layout()
plt.show()
7.Box Plot:
A box plot (or box-and-whisker plot) is a graphical representation of a dataset
that shows its distribution through five key summary statistics: minimum, first
quartile (Q1), median (Q2), third quartile (Q3), and maximum. It's an excellent
way to visualize the spread, central tendency, and potential outliers in data.
Key Components of a Box Plot:
1. Box: The rectangle in the middle represents the interquartile range (IQR),
which is the distance between Q1 (25th percentile) and Q3 (75th
percentile).
2. Median Line: A line inside the box indicates the median (Q2), which is the
50th percentile.
3. Whiskers: Lines extending from the box to the minimum and maximum
values within 1.5 times the IQR.
4. Outliers: Data points that fall outside the whiskers are plotted as individual
dots or circles.
Example:
import matplotlib.pyplot as plt
# Sample data
data = [7, 8, 9, 10, 12, 14, 15, 18, 20, 22, 28]
# Create a box plot
plt.boxplot(data)
# Add labels
plt.title("Box Plot Example")
plt.ylabel("Values")
# Show the plot
plt.show()