Chuyển đến nội dung
Skillpedia
Hình ảnh và biểu tượng51k · #3497

matplotlib

Skill này làm gì

Matplotlib là công cụ tạo biểu đồ và trực quan hóa dữ liệu bằng Python. Nó cho phép vẽ mọi thứ từ biểu đồ đường đơn giản đến trực quan hóa khoa học phức tạp, với khả năng kiểm soát đầy đủ về giao diện.

Khi nào nên dùng

  • ✓Tạo biểu đồ đường, cột, phân tán, v.v.
  • ✓Tùy chỉnh giao diện biểu đồ (màu sắc, nhãn, chú giải).
  • ✓Lưu biểu đồ ở các định dạng như PNG hoặc PDF để dùng trong tài liệu.

Liên kết skill

https://github.com/K-Dense-AI/scientific-agent-skills/blob/main/skills/matplotlib/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/K-Dense-AI/scientific-agent-skills/blob/main/skills/matplotlib/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Tạo biểu đồ đường thể hiện xu hướng nhiệt độ trung bình ở São Paulo trong 5 năm qua.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Bạn cung cấp dữ liệu và loại biểu đồ mong muốn. Matplotlib dùng dữ liệu này để vẽ biểu đồ, cho phép bạn điều chỉnh các chi tiết như màu sắc, tiêu đề và trục.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Matplotlib

Overview

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.

When to Use This Skill

This skill should be used when:

  • Creating any type of plot or chart (line, scatter, bar, histogram, heatmap, contour, etc.)
  • Generating scientific or statistical visualizations
  • Customizing plot appearance (colors, styles, labels, legends)
  • Creating multi-panel figures with subplots
  • Exporting visualizations to various formats (PNG, PDF, SVG, etc.)
  • Building interactive plots or animations
  • Working with 3D visualizations
  • Integrating plots into Jupyter notebooks or GUI applications

Setup

For project work, install Matplotlib with uv:

uv add "matplotlib==3.11.2" numpy scipy

For notebook interactivity:

uv add "matplotlib==3.11.2" ipympl

Then enable the widget backend in Jupyter with %matplotlib widget or %matplotlib ipympl.

Targets Matplotlib 3.11.2 (Python 3.11+), reviewed 2026-10-01. The bundled scripts and representative examples were executed using Agg and PNG/PDF/SVG output. GUI windows, Jupyter widgets, and external LaTeX are environment-dependent and were not exercised. Fragment examples assume imports and named data; adapt and validate them before use. Check the 3.11 API changes when migrating older code: use tick_labels and orientation for box plots, mpl.colormaps[name] for colormaps, and label contour lines rather than contourf.

File output needs no GUI. Use MPLBACKEND=Agg for batch scripts, or select Agg before importing pyplot. Interactive output requires an installed GUI toolkit such as PySide6 (QtAgg) or working Tk (TkAgg); plt.ioff() does not remove GUI thread requirements. See backends.

Core Concepts

The Matplotlib Hierarchy

Matplotlib uses a hierarchical structure of objects:

  1. Figure - The top-level container for all plot elements
  2. Axes - The actual plotting area where data is displayed (one Figure can contain multiple Axes)
  3. Artist - Everything visible on the figure (lines, text, ticks, etc.)
  4. Axis - The number line objects (x-axis, y-axis) that handle ticks and labels

Two Interfaces

1. pyplot Interface (Implicit, MATLAB-style)

import matplotlib.pyplot as plt

plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
plt.show()
  • Convenient for quick, simple plots
  • Maintains state automatically
  • Good for interactive work and simple scripts

2. Object-Oriented Interface (Explicit)

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4])
ax.set_ylabel('some numbers')
plt.show()
  • Recommended for most use cases
  • More explicit control over figure and axes
  • Better for complex figures with multiple subplots
  • Easier to maintain and debug

Common Workflows

1. Basic Plot Creation

Single plot workflow:

import matplotlib.pyplot as plt
import numpy as np

# Create figure and axes (OO interface - RECOMMENDED)
fig, ax = plt.subplots(figsize=(10, 6))

# Generate and plot data
x = np.linspace(0, 2*np.pi, 100)
ax.plot(x, np.sin(x), label='sin(x)')
ax.plot(x, np.cos(x), label='cos(x)')

# Customize
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('Trigonometric Functions')
ax.legend()
ax.grid(True, alpha=0.3)

# Save and/or display
fig.savefig('plot.png', dpi=300, bbox_inches='tight')
plt.show()

2. Multiple Subplots

Creating subplot layouts:

# Method 1: Regular grid
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
axes[0, 0].plot(x, y1)
axes[0, 1].scatter(x, y2)
axes[1, 0].bar(categories, values)
axes[1, 1].hist(data, bins=30)

# Method 2: Mosaic layout (more flexible)
fig, axes = plt.subplot_mosaic([['left', 'right_top'],
                                 ['left', 'right_bottom']],
                                figsize=(10, 8))
axes['left'].plot(x, y)
axes['right_top'].scatter(x, y)
axes['right_bottom'].hist(data)

# Method 3: GridSpec (maximum control)
from matplotlib.gridspec import GridSpec
fig = plt.figure(figsize=(12, 8))
gs = GridSpec(3, 3, figure=fig)

…
Câu hỏi thường gặp

Khác biệt giữa pyplot và giao diện hướng đối tượng là gì?

Pyplot đơn giản hơn để vẽ nhanh, còn giao diện hướng đối tượng cho phép kiểm soát nhiều hơn với biểu đồ phức tạp và tùy chỉnh chi tiết.

Làm sao để lưu biểu đồ?

Bạn có thể dùng hàm `savefig()` để lưu biểu đồ thành các tệp như PNG, PDF hoặc SVG.

Tôi có thể tạo nhiều biểu đồ trong cùng một hình không?

Có. Matplotlib cho phép tạo hình có nhiều biểu đồ con (subplot), sắp xếp các biểu đồ cạnh nhau hoặc theo lưới.

Chi tiết

Mô tả gốc của tác giả

Creates and customizes scientific plots with Matplotlib. Used for fine-grained control over plot elements, novel plot types, and scientific workflows. Export to PNG/PDF/SVG for publication. For quick statistical plots use seaborn; for interactive plots use plotly; for publication-ready multi-panel figures with journal styling, use scientific-visualization.

repo: K-Dense-AI/scientific-agent-skills

Độ phổ biến

Sao GitHub
51k
Vị trí xếp hạng
#3497
Giấy phép
MIT
Cập nhật
15/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự