$npx -y skills add ai4protein/VenusFactory2 --skill matplotlibLow-level plotting library for full customization. Use when you need fine-grained control over every plot element, creating novel plot types, or integrating with specific scientific workflows. Export to PNG/PDF/SVG for publication. For quick statistical plots use seaborn; for int
| 1 | # Matplotlib |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Matplotlib is Python's foundational visualization library for creating static, animated, and interactive plots. This skill provides guidance on using matplotlib effectively, covering both the pyplot interface (MATLAB-style) and the object-oriented API (Figure/Axes), along with best practices for creating publication-quality visualizations. |
| 6 | |
| 7 | ## When to Use This Skill |
| 8 | |
| 9 | This skill should be used when: |
| 10 | - Creating any type of plot or chart (line, scatter, bar, histogram, heatmap, contour, etc.) |
| 11 | - Generating scientific or statistical visualizations |
| 12 | - Customizing plot appearance (colors, styles, labels, legends) |
| 13 | - Creating multi-panel figures with subplots |
| 14 | - Exporting visualizations to various formats (PNG, PDF, SVG, etc.) |
| 15 | - Building interactive plots or animations |
| 16 | - Working with 3D visualizations |
| 17 | - Integrating plots into Jupyter notebooks or GUI applications |
| 18 | |
| 19 | ## Core Concepts |
| 20 | |
| 21 | ### The Matplotlib Hierarchy |
| 22 | |
| 23 | Matplotlib uses a hierarchical structure of objects: |
| 24 | |
| 25 | 1. **Figure** - The top-level container for all plot elements |
| 26 | 2. **Axes** - The actual plotting area where data is displayed (one Figure can contain multiple Axes) |
| 27 | 3. **Artist** - Everything visible on the figure (lines, text, ticks, etc.) |
| 28 | 4. **Axis** - The number line objects (x-axis, y-axis) that handle ticks and labels |
| 29 | |
| 30 | ### Two Interfaces |
| 31 | |
| 32 | **1. pyplot Interface (Implicit, MATLAB-style)** |
| 33 | ```python |
| 34 | import matplotlib.pyplot as plt |
| 35 | |
| 36 | plt.plot([1, 2, 3, 4]) |
| 37 | plt.ylabel('some numbers') |
| 38 | plt.show() |
| 39 | ``` |
| 40 | - Convenient for quick, simple plots |
| 41 | - Maintains state automatically |
| 42 | - Good for interactive work and simple scripts |
| 43 | |
| 44 | **2. Object-Oriented Interface (Explicit)** |
| 45 | ```python |
| 46 | import matplotlib.pyplot as plt |
| 47 | |
| 48 | fig, ax = plt.subplots() |
| 49 | ax.plot([1, 2, 3, 4]) |
| 50 | ax.set_ylabel('some numbers') |
| 51 | plt.show() |
| 52 | ``` |
| 53 | - **Recommended for most use cases** |
| 54 | - More explicit control over figure and axes |
| 55 | - Better for complex figures with multiple subplots |
| 56 | - Easier to maintain and debug |
| 57 | |
| 58 | ## Common Workflows |
| 59 | |
| 60 | ### 1. Basic Plot Creation |
| 61 | |
| 62 | **Single plot workflow:** |
| 63 | ```python |
| 64 | import matplotlib.pyplot as plt |
| 65 | import numpy as np |
| 66 | |
| 67 | # Create figure and axes (OO interface - RECOMMENDED) |
| 68 | fig, ax = plt.subplots(figsize=(10, 6)) |
| 69 | |
| 70 | # Generate and plot data |
| 71 | x = np.linspace(0, 2*np.pi, 100) |
| 72 | ax.plot(x, np.sin(x), label='sin(x)') |
| 73 | ax.plot(x, np.cos(x), label='cos(x)') |
| 74 | |
| 75 | # Customize |
| 76 | ax.set_xlabel('x') |
| 77 | ax.set_ylabel('y') |
| 78 | ax.set_title('Trigonometric Functions') |
| 79 | ax.legend() |
| 80 | ax.grid(True, alpha=0.3) |
| 81 | |
| 82 | # Save and/or display |
| 83 | plt.savefig('plot.png', dpi=300, bbox_inches='tight') |
| 84 | plt.show() |
| 85 | ``` |
| 86 | |
| 87 | ### 2. Multiple Subplots |
| 88 | |
| 89 | **Creating subplot layouts:** |
| 90 | ```python |
| 91 | # Method 1: Regular grid |
| 92 | fig, axes = plt.subplots(2, 2, figsize=(12, 10)) |
| 93 | axes[0, 0].plot(x, y1) |
| 94 | axes[0, 1].scatter(x, y2) |
| 95 | axes[1, 0].bar(categories, values) |
| 96 | axes[1, 1].hist(data, bins=30) |
| 97 | |
| 98 | # Method 2: Mosaic layout (more flexible) |
| 99 | fig, axes = plt.subplot_mosaic([['left', 'right_top'], |
| 100 | ['left', 'right_bottom']], |
| 101 | figsize=(10, 8)) |
| 102 | axes['left'].plot(x, y) |
| 103 | axes['right_top'].scatter(x, y) |
| 104 | axes['right_bottom'].hist(data) |
| 105 | |
| 106 | # Method 3: GridSpec (maximum control) |
| 107 | from matplotlib.gridspec import GridSpec |
| 108 | fig = plt.figure(figsize=(12, 8)) |
| 109 | gs = GridSpec(3, 3, figure=fig) |
| 110 | ax1 = fig.add_subplot(gs[0, :]) # Top row, all columns |
| 111 | ax2 = fig.add_subplot(gs[1:, 0]) # Bottom two rows, first column |
| 112 | ax3 = fig.add_subplot(gs[1:, 1:]) # Bottom two rows, last two columns |
| 113 | ``` |
| 114 | |
| 115 | ### 3. Plot Types and Use Cases |
| 116 | |
| 117 | **Line plots** - Time series, continuous data, trends |
| 118 | ```python |
| 119 | ax.plot(x, y, linewidth=2, linestyle='--', marker='o', color='blue') |
| 120 | ``` |
| 121 | |
| 122 | **Scatter plots** - Relationships between variables, correlations |
| 123 | ```python |
| 124 | ax.scatter(x, y, s=sizes, c=colors, alpha=0.6, cmap='viridis') |
| 125 | ``` |
| 126 | |
| 127 | **Bar charts** - Categorical comparisons |
| 128 | ```python |
| 129 | ax.bar(categories, values, color='steelblue', edgecolor='black') |
| 130 | # For horizontal bars: |
| 131 | ax.barh(categories, values) |
| 132 | ``` |
| 133 | |
| 134 | **Histograms** - Distributions |
| 135 | ```python |
| 136 | ax.hist(data, bins=30, edgecolor='black', alpha=0.7) |
| 137 | ``` |
| 138 | |
| 139 | **Heatmaps** - Matrix data, correlations |
| 140 | ```python |
| 141 | im = ax.imshow(matrix, cmap='coolwarm', aspect='auto') |
| 142 | plt.colorbar(im, ax=ax) |
| 143 | ``` |
| 144 | |
| 145 | **Contour plots** - 3D data on 2D plane |
| 146 | ```python |
| 147 | contour = ax.contour(X, Y, Z, levels=10) |
| 148 | ax.clabel(contour, inline=True, fontsize=8) |
| 149 | ``` |
| 150 | |
| 151 | **Box plots** - Statistical distributions |
| 152 | ```python |
| 153 | ax.boxplot([data1, data2, data3], labels=['A', 'B', 'C']) |
| 154 | ``` |
| 155 | |
| 156 | **Violin plots** - Distribution |