Skip to content

Instantly share code, notes, and snippets.

@WxBDM
Created September 10, 2025 02:02
Show Gist options
  • Select an option

  • Save WxBDM/7c817367939507653659fcc26875bc80 to your computer and use it in GitHub Desktop.

Select an option

Save WxBDM/7c817367939507653659fcc26875bc80 to your computer and use it in GitHub Desktop.
Python Snacks Matplotlib 6 common plots
import matplotlib.pyplot as plt
import numpy as np
import random
# Set up the figure with subplots
fig, axes = plt.subplots(2, 3, figsize=(15, 10))
fig.suptitle('6 Common Matplotlib Plot Types', fontsize=16, fontweight='bold')
# 1. line plot
x = np.linspace(0, 10, 50)
y = np.sin(x) + 0.1 * np.random.randn(50)
axes[0, 0].plot(x, y, 'b-', linewidth=2, marker='o', markersize=3)
axes[0, 0].set_title('Line Plot')
axes[0, 0].set_xlabel('X values')
axes[0, 0].set_ylabel('Y values')
axes[0, 0].grid(True, alpha=0.3)
# 2. scatter plot
np.random.seed(42)
x_scatter = np.random.randn(100)
y_scatter = 2 * x_scatter + np.random.randn(100)
axes[0, 1].scatter(x_scatter, y_scatter, c='red', alpha=0.6, s=50)
axes[0, 1].set_title('Scatter Plot')
axes[0, 1].set_xlabel('X values')
axes[0, 1].set_ylabel('Y values')
axes[0, 1].grid(True, alpha=0.3)
# 3. bar chart
categories = ['Category A', 'Category B', 'Category C', 'Category D', 'Category E']
values = [23, 45, 56, 78, 32]
axes[0, 2].bar(categories, values, color=['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7'])
axes[0, 2].set_title('Bar Chart')
axes[0, 2].set_xlabel('Categories')
axes[0, 2].set_ylabel('Values')
axes[0, 2].tick_params(axis='x', rotation=45)
# 4. histogram
np.random.seed(42)
data = np.random.normal(100, 15, 1000) # normal distribution
axes[1, 0].hist(data, bins=30, color='skyblue', alpha=0.7, edgecolor='black')
axes[1, 0].set_title('Histogram')
axes[1, 0].set_xlabel('Values')
axes[1, 0].set_ylabel('Frequency')
axes[1, 0].grid(True, alpha=0.3)
# 5. box plot
# Create sample data for multiple groups
group1 = np.random.normal(100, 10, 100)
group2 = np.random.normal(105, 15, 100)
group3 = np.random.normal(95, 8, 100)
group4 = np.random.normal(110, 12, 100)
box_data = [group1, group2, group3, group4]
box_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4']
axes[1, 1].boxplot(box_data, labels=box_labels, patch_artist=True,
boxprops=dict(facecolor='lightblue', alpha=0.7))
axes[1, 1].set_title('Box Plot')
axes[1, 1].set_xlabel('Groups')
axes[1, 1].set_ylabel('Values')
axes[1, 1].grid(True, alpha=0.3)
# 6. pie chart
labels = ['Python', 'JavaScript', 'Java', 'C++', 'Go']
sizes = [35, 25, 20, 15, 5]
colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7']
explode = (0.1, 0, 0, 0, 0) # explode the first slice
axes[1, 2].pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%',
explode=explode, startangle=90, shadow=True)
axes[1, 2].set_title('Pie Chart')
axes[1, 2].axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle
# Adjust layout to prevent overlapping
plt.tight_layout()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment