6. Drawing Line and bar chart using matplotlib.
import matplotlib.pyplot as plt
# Sample data for the charts
x_values = [1, 2, 3, 4, 5]
y_values_line = [5, 3, 8, 4, 6]
y_values_bar = [2, 6, 3, 8, 5]
# Create a figure to hold both line and bar charts
plt.figure(figsize=(10, 6))
# Plot the line chart
plt.subplot(1, 2, 1) # Create a subplot with 1 row, 2 columns, and select the first position
plt.plot(x_values, y_values_line, marker='o', linestyle='-', color='blue', label='Line Chart')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Line Chart Example')
plt.legend()
# Plot the bar chart
plt.subplot(1, 2, 2) # Select the second position in the subplot
plt.bar(x_values, y_values_bar, color='red', label='Bar Chart')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Bar Chart Example')
plt.legend()
# Adjust layout for better spacing
plt.tight_layout()
# Show both charts
plt.show()