Matplotlib
Matplotlib
A Python visualization library. Creates static charts.
Matplotlib
A Python visualization library. Creates static charts.
Matplotlib is a Python visualization library developed by John Hunter in 2003, which implements MATLAB-style plotting in Python. It has been used as a standard in scientific computing and data analysis for more than 20 years, and together with NumPy it forms the foundation of the Python data ecosystem.
The pyplot interface provides a state-based API familiar to MATLAB users, while the object-oriented API enables fine-grained control by directly manipulating Figure and Axes objects. plt.figure() creates a canvas, and plt.subplots() arranges multiple graphs in a grid.
Supported chart types are diverse, including line plot, scatter plot, bar chart, histogram, pie chart, box plot, heatmap, and contour plot. rcParams settings customize fonts, colors, and styles globally, and high-quality output is possible in various formats such as PNG, PDF, and SVG.
Seaborn, Plotly, and Pandas plot use Matplotlib internally or are compatible with it. It is best suited for static visualizations for papers and reports; if interactivity is needed, consider Plotly or Bokeh.
import matplotlib.pyplot as plt
import numpy as np
# ์คํ์ผ ์ค์
plt.style.use('seaborn-v0_8-whitegrid')
plt.rcParams['font.family'] = 'DejaVu Sans'
plt.rcParams['figure.dpi'] = 100
# ๋ฐ์ดํฐ ์ค๋น
np.random.seed(42)
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
# Figure์ Axes ์์ฑ (2x2 ์๋ธํ๋กฏ)
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
fig.suptitle('Matplotlib ์๊ฐํ ์์ ', fontsize=16, fontweight='bold')
# 1. ๋ผ์ธ ํ๋กฏ
ax1 = axes[0, 0]
ax1.plot(x, y1, 'b-', label='sin(x)', linewidth=2)
ax1.plot(x, y2, 'r--', label='cos(x)', linewidth=2)
ax1.set_xlabel('X์ถ')
ax1.set_ylabel('Y์ถ')
ax1.set_title('๋ผ์ธ ํ๋กฏ')
ax1.legend(loc='upper right')
ax1.grid(True, alpha=0.3)
# 2. ์ค์บํฐ ํ๋กฏ (์์ ๋งคํ)
ax2 = axes[0, 1]
scatter_x = np.random.randn(200)
scatter_y = np.random.randn(200)
colors = np.sqrt(scatter_x**2 + scatter_y**2)
scatter = ax2.scatter(scatter_x, scatter_y, c=colors,
cmap='viridis', alpha=0.7, s=50)
ax2.set_title('์ค์บํฐ ํ๋กฏ')
plt.colorbar(scatter, ax=ax2, label='๊ฑฐ๋ฆฌ')
# 3. ํ์คํ ๊ทธ๋จ
ax3 = axes[1, 0]
data = np.random.normal(0, 1, 1000)
ax3.hist(data, bins=30, edgecolor='white', alpha=0.7, color='steelblue')
ax3.axvline(data.mean(), color='red', linestyle='--', label=f'ํ๊ท : {data.mean():.2f}')
ax3.set_xlabel('๊ฐ')
ax3.set_ylabel('๋น๋')
ax3.set_title('ํ์คํ ๊ทธ๋จ')
ax3.legend()
# 4. ํํธ๋งต
ax4 = axes[1, 1]
heatmap_data = np.random.rand(10, 10)
im = ax4.imshow(heatmap_data, cmap='coolwarm', aspect='auto')
ax4.set_title('ํํธ๋งต')
plt.colorbar(im, ax=ax4)
plt.tight_layout()
plt.savefig('visualization.png', dpi=300, bbox_inches='tight')
plt.show()
Senior: "Draw charts for reports with Matplotlib. If you output to PDF as vector graphics, they won't get blurry even when you zoom in."
Junior: "My Korean fonts are broken. What should I do?"
Senior: "Set font.family to 'Malgun Gothic' in rcParams and set axes.unicode_minus to False."
Interviewer: "Please tell me about your data visualization experience."
Candidate: "I built a function that automatically generates EDA charts with Matplotlib. It uses subplots to show the distributions and correlations of multiple variables at once, and savefig lets me insert them into reports. For dashboards that needed interactivity, I used Plotly."
Take a mock interview on this topic โ WIA SOOM AI Interview ยท Start for free
Reviewer: "Call plt.tight_layout() before plt.show(). It prevents labels from overlapping between subplots."
Developer: "Yes, and I'll also enlarge figsize to (12, 8) to improve readability."