3.1 颜色系统与配色方案
3.1.1 Matplotlib颜色系统
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
import seaborn as sns
class ColorSystemDemo:
"""颜色系统演示类"""
def __init__(self):
plt.rcParams['font.sans-serif'] = ['SimHei', 'Arial Unicode MS']
plt.rcParams['axes.unicode_minus'] = False
def basic_colors(self):
"""基本颜色系统"""
fig, axes = plt.subplots(2, 2, figsize=(16, 12))
# 1. 基本颜色名称
basic_colors = ['red', 'green', 'blue', 'yellow', 'orange',
'purple', 'brown', 'pink', 'gray', 'olive']
y_pos = np.arange(len(basic_colors))
axes[0, 0].barh(y_pos, [1]*len(basic_colors), color=basic_colors)
axes[0, 0].set_yticks(y_pos)
axes[0, 0].set_yticklabels(basic_colors)
axes[0, 0].set_title('基本颜色名称')
# 2. 单字母颜色代码
single_letter_colors = ['r', 'g', 'b', 'c', 'm', 'y', 'k']
color_names = ['red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'black']
axes[0, 1].barh(range(len(single_letter_colors)), [1]*len(single_letter_colors),
color=single_letter_colors)
axes[0, 1].set_yticks(range(len(single_letter_colors)))
axes[0, 1].set_yticklabels([f"'{c}' - {name}" for c, name in
zip(single_letter_colors, color_names)])
axes[0, 1].set_title('单字母颜色代码')
# 3. 十六进制颜色
hex_colors = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00',
'#FF00FF', '#00FFFF', '#FFA500', '#800080']
axes[1, 0].barh(range(len(hex_colors)), [1]*len(hex_colors),
color=hex_colors)
axes[1, 0].set_yticks(range(len(hex_colors)))
axes[1, 0].set_yticklabels(hex_colors)
axes[1, 0].set_title('十六进制颜色代码')
# 4. RGB元组颜色
rgb_colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 0),
(1, 0, 1), (0, 1, 1), (0.5, 0.5, 0.5), (1, 0.5, 0)]
rgb_labels = [f'RGB{color}' for color in rgb_colors]
axes[1, 1].barh(range(len(rgb_colors)), [1]*len(rgb_colors),
color=rgb_colors)
axes[1, 1].set_yticks(range(len(rgb_colors)))
axes[1, 1].set_yticklabels(rgb_labels)
axes[1, 1].set_title('RGB元组颜色')
plt.tight_layout()
plt.show()
def color_maps_demo(self):
"""颜色映射演示"""
# 生成示例数据
x = np.linspace(0, 10, 100)
y = np.linspace(0, 10, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) * np.cos(Y)
# 常用颜色映射
cmaps = ['viridis', 'plasma', 'inferno', 'magma',
'coolwarm', 'RdYlBu', 'seismic', 'rainbow']
fig, axes = plt.subplots(2, 4, figsize=(20, 10))
axes = axes.flatten()
for i, cmap in enumerate(cmaps):
im = axes[i].contourf(X, Y, Z, levels=20, cmap=cmap)
axes[i].set_title(f'颜色映射: {cmap}')
plt.colorbar(im, ax=axes[i])
plt.tight_layout()
plt.show()
def custom_colormap(self):
"""自定义颜色映射"""
fig, axes = plt.subplots(2, 2, figsize=(16, 12))
# 生成示例数据
data = np.random.randn(50, 50)
# 1. 从颜色列表创建颜色映射
colors = ['#000080', '#0000FF', '#00FFFF', '#FFFF00', '#FF0000']
n_bins = 100
cmap1 = LinearSegmentedColormap.from_list('custom1', colors, N=n_bins)
im1 = axes[0, 0].imshow(data, cmap=cmap1)
axes[0, 0].set_title('自定义线性颜色映射')
plt.colorbar(im1, ax=axes[0, 0])
# 2. 分段颜色映射
colors = ['red', 'yellow', 'green', 'blue']
cmap2 = ListedColormap(colors)
# 创建分类数据
categorical_data = np.random.randint(0, 4, (20, 20))
im2 = axes[0, 1].imshow(categorical_data, cmap=cmap2)
axes[0, 1].set_title('分类颜色映射')
plt.colorbar(im2, ax=axes[0, 1], ticks=range(4),
label='类别')
# 3. 渐变颜色映射
from matplotlib.colors import LinearSegmentedColormap
# 定义颜色字典
cdict = {
'red': [(0.0, 0.0, 0.0),
(0.5, 1.0, 1.0),
(1.0, 1.0, 1.0)],
'green': [(0.0, 0.0, 0.0),
(0.25, 0.0, 0.0),
(0.75, 1.0, 1.0),
(1.0, 1.0, 1.0)],
'blue': [(0.0, 0.0, 0.0),
(0.5, 0.0, 0.0),
(1.0, 1.0, 1.0)]
}
cmap3 = LinearSegmentedColormap('custom3', cdict)
im3 = axes[1, 0].imshow(data, cmap=cmap3)
axes[1, 0].set_title('自定义渐变颜色映射')
plt.colorbar(im3, ax=axes[1, 0])
# 4. 双色调颜色映射
from matplotlib.colors import TwoSlopeNorm
# 创建以0为中心的标准化
norm = TwoSlopeNorm(vmin=data.min(), vcenter=0, vmax=data.max())
im4 = axes[1, 1].imshow(data, cmap='RdBu_r', norm=norm)
axes[1, 1].set_title('双色调颜色映射(以0为中心)')
plt.colorbar(im4, ax=axes[1, 1])
plt.tight_layout()
plt.show()
def color_harmony_schemes(self):
"""配色方案演示"""
fig, axes = plt.subplots(2, 3, figsize=(18, 12))
# 生成示例数据
categories = ['A', 'B', 'C', 'D', 'E']
values = [23, 45, 56, 78, 32]
# 1. 单色配色方案
base_color = '#3498db'
monochromatic = ['#85c1e9', '#5dade2', '#3498db', '#2e86c1', '#2874a6']
axes[0, 0].bar(categories, values, color=monochromatic)
axes[0, 0].set_title('单色配色方案')
axes[0, 0].set_ylabel('数值')
# 2. 类似色配色方案
analogous = ['#3498db', '#2ecc71', '#1abc9c', '#16a085', '#138d75']
axes[0, 1].bar(categories, values, color=analogous)
axes[0, 1].set_title('类似色配色方案')
axes[0, 1].set_ylabel('数值')
# 3. 互补色配色方案
complementary = ['#3498db', '#e74c3c', '#85c1e9', '#ec7063', '#5dade2']
axes[0, 2].bar(categories, values, color=complementary)
axes[0, 2].set_title('互补色配色方案')
axes[0, 2].set_ylabel('数值')
# 4. 三角色配色方案
triadic = ['#3498db', '#e74c3c', '#f39c12', '#85c1e9', '#ec7063']
axes[1, 0].bar(categories, values, color=triadic)
axes[1, 0].set_title('三角色配色方案')
axes[1, 0].set_ylabel('数值')
# 5. 分裂互补色配色方案
split_complementary = ['#3498db', '#e67e22', '#e74c3c', '#85c1e9', '#f39c12']
axes[1, 1].bar(categories, values, color=split_complementary)
axes[1, 1].set_title('分裂互补色配色方案')
axes[1, 1].set_ylabel('数值')
# 6. 四角色配色方案
tetradic = ['#3498db', '#e74c3c', '#2ecc71', '#f39c12', '#9b59b6']
axes[1, 2].bar(categories, values, color=tetradic)
axes[1, 2].set_title('四角色配色方案')
axes[1, 2].set_ylabel('数值')
plt.tight_layout()
plt.show()
# 使用示例
color_demo = ColorSystemDemo()
color_demo.basic_colors()
color_demo.color_maps_demo()
color_demo.custom_colormap()
color_demo.color_harmony_schemes()
3.2 字体与文本样式
3.2.1 字体设置与管理
class FontStyleDemo:
"""字体样式演示类"""
def __init__(self):
plt.rcParams['font.sans-serif'] = ['SimHei', 'Arial Unicode MS']
plt.rcParams['axes.unicode_minus'] = False
def font_families_demo(self):
"""字体族演示"""
fig, axes = plt.subplots(2, 2, figsize=(16, 12))
# 示例文本
text = "Matplotlib字体演示 Font Demo"
x = [1, 2, 3, 4, 5]
y = [2, 5, 3, 8, 7]
# 1. 不同字体族
font_families = {
'serif': 'Times New Roman',
'sans-serif': 'Arial',
'monospace': 'Courier New',
'fantasy': 'Comic Sans MS'
}
for i, (family, font) in enumerate(font_families.items()):
row, col = i // 2, i % 2
axes[row, col].plot(x, y, 'o-', linewidth=2, markersize=8)
axes[row, col].set_title(f'{family.title()} 字体族',
fontfamily=family, fontsize=16)
axes[row, col].set_xlabel('X轴标签', fontfamily=family, fontsize=12)
axes[row, col].set_ylabel('Y轴标签', fontfamily=family, fontsize=12)
axes[row, col].text(3, 6, text, fontfamily=family, fontsize=14,
ha='center', va='center',
bbox=dict(boxstyle='round', facecolor='lightblue', alpha=0.5))
plt.tight_layout()
plt.show()
def font_properties_demo(self):
"""字体属性演示"""
fig, axes = plt.subplots(2, 3, figsize=(18, 12))
x = np.linspace(0, 10, 100)
y = np.sin(x)
# 1. 字体大小
sizes = [8, 12, 16, 20, 24]
axes[0, 0].plot(x, y)
for i, size in enumerate(sizes):
axes[0, 0].text(2*i, 0.5, f'Size {size}', fontsize=size,
ha='center', va='center')
axes[0, 0].set_title('字体大小演示')
# 2. 字体粗细
weights = ['ultralight', 'light', 'normal', 'bold', 'heavy']
axes[0, 1].plot(x, y)
for i, weight in enumerate(weights):
axes[0, 1].text(2*i, 0.5, weight.title(), fontweight=weight,
fontsize=12, ha='center', va='center')
axes[0, 1].set_title('字体粗细演示')
# 3. 字体样式
styles = ['normal', 'italic', 'oblique']
axes[0, 2].plot(x, y)
for i, style in enumerate(styles):
axes[0, 2].text(3*i+1, 0.5, style.title(), fontstyle=style,
fontsize=14, ha='center', va='center')
axes[0, 2].set_title('字体样式演示')
# 4. 字体拉伸
stretches = ['ultra-condensed', 'condensed', 'normal', 'expanded', 'ultra-expanded']
axes[1, 0].plot(x, y)
for i, stretch in enumerate(stretches):
axes[1, 0].text(2*i, 0.5, stretch.replace('-', '\n'),
fontstretch=stretch, fontsize=10,
ha='center', va='center')
axes[1, 0].set_title('字体拉伸演示')
# 5. 字体变体
variants = ['normal', 'small-caps']
axes[1, 1].plot(x, y)
for i, variant in enumerate(variants):
axes[1, 1].text(4*i+2, 0.5, f'{variant} Text',
fontvariant=variant, fontsize=14,
ha='center', va='center')
axes[1, 1].set_title('字体变体演示')
# 6. 综合字体属性
axes[1, 2].plot(x, y)
font_props = {
'fontfamily': 'serif',
'fontsize': 16,
'fontweight': 'bold',
'fontstyle': 'italic',
'color': 'red'
}
axes[1, 2].text(5, 0, '综合字体效果', **font_props,
ha='center', va='center',
bbox=dict(boxstyle='round,pad=0.5',
facecolor='yellow', alpha=0.7))
axes[1, 2].set_title('综合字体属性')
plt.tight_layout()
plt.show()
def text_alignment_demo(self):
"""文本对齐演示"""
fig, axes = plt.subplots(2, 2, figsize=(16, 12))
# 1. 水平对齐
axes[0, 0].set_xlim(0, 10)
axes[0, 0].set_ylim(0, 10)
x_center = 5
y_positions = [2, 4, 6, 8]
h_alignments = ['left', 'center', 'right']
# 绘制参考线
axes[0, 0].axvline(x_center, color='red', linestyle='--', alpha=0.5)
for i, ha in enumerate(h_alignments):
axes[0, 0].text(x_center, y_positions[i], f'水平对齐: {ha}',
ha=ha, va='center', fontsize=12,
bbox=dict(boxstyle='round', facecolor='lightblue', alpha=0.5))
axes[0, 0].set_title('水平对齐演示')
# 2. 垂直对齐
axes[0, 1].set_xlim(0, 10)
axes[0, 1].set_ylim(0, 10)
y_center = 5
x_positions = [2, 4, 6, 8]
v_alignments = ['bottom', 'center', 'top']
# 绘制参考线
axes[0, 1].axhline(y_center, color='red', linestyle='--', alpha=0.5)
for i, va in enumerate(v_alignments):
axes[0, 1].text(x_positions[i], y_center, f'垂直\n对齐\n{va}',
ha='center', va=va, fontsize=10,
bbox=dict(boxstyle='round', facecolor='lightgreen', alpha=0.5))
axes[0, 1].set_title('垂直对齐演示')
# 3. 文本旋转
axes[1, 0].set_xlim(0, 10)
axes[1, 0].set_ylim(0, 10)
center_x, center_y = 5, 5
angles = [0, 45, 90, 135, 180, 225, 270, 315]
for angle in angles:
axes[1, 0].text(center_x, center_y, f'{angle}°',
ha='center', va='center', rotation=angle,
fontsize=12, color='blue')
axes[1, 0].plot(center_x, center_y, 'ro', markersize=8)
axes[1, 0].set_title('文本旋转演示')
# 4. 文本框样式
axes[1, 1].set_xlim(0, 10)
axes[1, 1].set_ylim(0, 10)
box_styles = [
('round', 'lightblue'),
('square', 'lightgreen'),
('round,pad=0.5', 'lightcoral'),
('sawtooth', 'lightyellow')
]
positions = [(2, 8), (8, 8), (2, 2), (8, 2)]
for (style, color), (x, y) in zip(box_styles, positions):
axes[1, 1].text(x, y, f'样式:\n{style}', ha='center', va='center',
fontsize=10, bbox=dict(boxstyle=style,
facecolor=color, alpha=0.7))
axes[1, 1].set_title('文本框样式演示')
plt.tight_layout()
plt.show()
def mathematical_text_demo(self):
"""数学文本演示"""
fig, axes = plt.subplots(2, 2, figsize=(16, 12))
# 1. 基本数学符号
axes[0, 0].text(0.5, 0.8, r'$\alpha + \beta = \gamma$',
transform=axes[0, 0].transAxes, fontsize=16, ha='center')
axes[0, 0].text(0.5, 0.6, r'$\sum_{i=1}^{n} x_i = \bar{x} \cdot n$',
transform=axes[0, 0].transAxes, fontsize=16, ha='center')
axes[0, 0].text(0.5, 0.4, r'$\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}$',
transform=axes[0, 0].transAxes, fontsize=16, ha='center')
axes[0, 0].text(0.5, 0.2, r'$\frac{\partial f}{\partial x} = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h}$',
transform=axes[0, 0].transAxes, fontsize=14, ha='center')
axes[0, 0].set_title('数学公式演示')
axes[0, 0].axis('off')
# 2. 上标和下标
x = np.linspace(0, 10, 100)
y1 = np.exp(-x/5)
y2 = x**2
axes[0, 1].plot(x, y1, label=r'$e^{-x/5}$')
axes[0, 1].plot(x, y2, label=r'$x^2$')
axes[0, 1].set_xlabel(r'$x$ (时间)', fontsize=12)
axes[0, 1].set_ylabel(r'$f(x)$ (函数值)', fontsize=12)
axes[0, 1].set_title(r'函数图像: $f_1(x) = e^{-x/5}$ 和 $f_2(x) = x^2$')
axes[0, 1].legend()
axes[0, 1].grid(True, alpha=0.3)
# 3. 分数和根号
axes[1, 0].text(0.5, 0.8, r'$\frac{a}{b} + \frac{c}{d} = \frac{ad + bc}{bd}$',
transform=axes[1, 0].transAxes, fontsize=16, ha='center')
axes[1, 0].text(0.5, 0.6, r'$\sqrt{a^2 + b^2} = c$',
transform=axes[1, 0].transAxes, fontsize=16, ha='center')
axes[1, 0].text(0.5, 0.4, r'$\sqrt[n]{x} = x^{\frac{1}{n}}$',
transform=axes[1, 0].transAxes, fontsize=16, ha='center')
axes[1, 0].text(0.5, 0.2, r'$\frac{\sqrt{x+1}}{\sqrt{x-1}} = \sqrt{\frac{x+1}{x-1}}$',
transform=axes[1, 0].transAxes, fontsize=14, ha='center')
axes[1, 0].set_title('分数和根号演示')
axes[1, 0].axis('off')
# 4. 矩阵和向量
axes[1, 1].text(0.5, 0.8, r'$\mathbf{A} = \begin{pmatrix} a & b \\ c & d \end{pmatrix}$',
transform=axes[1, 1].transAxes, fontsize=16, ha='center')
axes[1, 1].text(0.5, 0.6, r'$\vec{v} = \begin{bmatrix} x \\ y \\ z \end{bmatrix}$',
transform=axes[1, 1].transAxes, fontsize=16, ha='center')
axes[1, 1].text(0.5, 0.4, r'$\det(\mathbf{A}) = ad - bc$',
transform=axes[1, 1].transAxes, fontsize=16, ha='center')
axes[1, 1].text(0.5, 0.2, r'$\mathbf{A}^{-1} = \frac{1}{\det(\mathbf{A})} \begin{pmatrix} d & -b \\ -c & a \end{pmatrix}$',
transform=axes[1, 1].transAxes, fontsize=12, ha='center')
axes[1, 1].set_title('矩阵和向量演示')
axes[1, 1].axis('off')
plt.tight_layout()
plt.show()
# 使用示例
font_demo = FontStyleDemo()
font_demo.font_families_demo()
font_demo.font_properties_demo()
font_demo.text_alignment_demo()
font_demo.mathematical_text_demo()
3.3 主题与样式表
3.3.1 内置样式表
class StyleThemeDemo:
"""样式主题演示类"""
def __init__(self):
plt.rcParams['font.sans-serif'] = ['SimHei', 'Arial Unicode MS']
plt.rcParams['axes.unicode_minus'] = False
# 生成示例数据
np.random.seed(42)
self.x = np.linspace(0, 10, 50)
self.y1 = np.sin(self.x) + np.random.normal(0, 0.1, 50)
self.y2 = np.cos(self.x) + np.random.normal(0, 0.1, 50)
self.categories = ['A', 'B', 'C', 'D', 'E']
self.values = [23, 45, 56, 78, 32]
def builtin_styles_demo(self):
"""内置样式演示"""
# 获取所有可用样式
available_styles = plt.style.available
print("可用样式:", available_styles)
# 选择几个代表性样式进行演示
demo_styles = ['default', 'seaborn', 'ggplot', 'bmh',
'classic', 'dark_background']
fig, axes = plt.subplots(2, 3, figsize=(18, 12))
axes = axes.flatten()
for i, style in enumerate(demo_styles):
with plt.style.context(style):
# 绘制线图
axes[i].plot(self.x, self.y1, 'o-', label='sin(x)', linewidth=2)
axes[i].plot(self.x, self.y2, 's-', label='cos(x)', linewidth=2)
axes[i].set_title(f'样式: {style}', fontsize=14)
axes[i].set_xlabel('X轴')
axes[i].set_ylabel('Y轴')
axes[i].legend()
axes[i].grid(True)
plt.tight_layout()
plt.show()
def seaborn_styles_demo(self):
"""Seaborn样式演示"""
# Seaborn样式
seaborn_styles = ['seaborn-darkgrid', 'seaborn-whitegrid',
'seaborn-dark', 'seaborn-white',
'seaborn-ticks', 'seaborn-bright']
# 过滤存在的样式
available_seaborn = [s for s in seaborn_styles if s in plt.style.available]
if not available_seaborn:
# 如果没有seaborn样式,使用替代方案
available_seaborn = ['default', 'classic', 'ggplot', 'bmh']
fig, axes = plt.subplots(2, 2, figsize=(16, 12))
axes = axes.flatten()
for i, style in enumerate(available_seaborn[:4]):
with plt.style.context(style):
# 绘制散点图
scatter = axes[i].scatter(self.x, self.y1, c=self.y2,
s=100, alpha=0.7, cmap='viridis')
axes[i].set_title(f'样式: {style}', fontsize=14)
axes[i].set_xlabel('X轴')
axes[i].set_ylabel('Y轴')
plt.colorbar(scatter, ax=axes[i])
plt.tight_layout()
plt.show()
def custom_style_demo(self):
"""自定义样式演示"""
# 创建自定义样式
custom_style = {
'figure.figsize': (12, 8),
'figure.facecolor': 'white',
'axes.facecolor': '#f8f9fa',
'axes.edgecolor': '#dee2e6',
'axes.linewidth': 1.2,
'axes.grid': True,
'axes.grid.axis': 'both',
'grid.color': '#e9ecef',
'grid.linestyle': '-',
'grid.linewidth': 0.8,
'grid.alpha': 0.8,
'xtick.direction': 'out',
'ytick.direction': 'out',
'xtick.major.size': 6,
'ytick.major.size': 6,
'xtick.color': '#495057',
'ytick.color': '#495057',
'axes.labelcolor': '#495057',
'axes.titlesize': 16,
'axes.titleweight': 'bold',
'axes.labelsize': 12,
'lines.linewidth': 2.5,
'lines.markersize': 8,
'font.size': 11,
'legend.frameon': True,
'legend.fancybox': True,
'legend.shadow': True,
'legend.framealpha': 0.9,
'legend.facecolor': 'white',
'legend.edgecolor': '#dee2e6'
}
# 应用自定义样式
with plt.rc_context(custom_style):
fig, axes = plt.subplots(2, 2, figsize=(16, 12))
# 1. 线图
axes[0, 0].plot(self.x, self.y1, 'o-', label='数据1', color='#007bff')
axes[0, 0].plot(self.x, self.y2, 's-', label='数据2', color='#28a745')
axes[0, 0].set_title('自定义样式线图')
axes[0, 0].set_xlabel('X轴')
axes[0, 0].set_ylabel('Y轴')
axes[0, 0].legend()
# 2. 柱状图
colors = ['#007bff', '#28a745', '#ffc107', '#dc3545', '#6f42c1']
bars = axes[0, 1].bar(self.categories, self.values, color=colors, alpha=0.8)
axes[0, 1].set_title('自定义样式柱状图')
axes[0, 1].set_xlabel('类别')
axes[0, 1].set_ylabel('数值')
# 添加数值标签
for bar, value in zip(bars, self.values):
axes[0, 1].text(bar.get_x() + bar.get_width()/2, bar.get_height() + 1,
str(value), ha='center', va='bottom')
# 3. 散点图
scatter = axes[1, 0].scatter(self.x, self.y1, c=self.y2,
s=120, alpha=0.7, cmap='RdYlBu_r',
edgecolors='white', linewidth=1)
axes[1, 0].set_title('自定义样式散点图')
axes[1, 0].set_xlabel('X轴')
axes[1, 0].set_ylabel('Y轴')
plt.colorbar(scatter, ax=axes[1, 0])
# 4. 饼图
wedges, texts, autotexts = axes[1, 1].pie(self.values, labels=self.categories,
autopct='%1.1f%%', colors=colors,
startangle=90,
wedgeprops=dict(edgecolor='white', linewidth=2))
axes[1, 1].set_title('自定义样式饼图')
# 美化饼图文本
for autotext in autotexts:
autotext.set_color('white')
autotext.set_fontweight('bold')
plt.tight_layout()
plt.show()
def dark_theme_demo(self):
"""深色主题演示"""
# 深色主题样式
dark_style = {
'figure.facecolor': '#2e3440',
'axes.facecolor': '#3b4252',
'axes.edgecolor': '#4c566a',
'axes.labelcolor': '#eceff4',
'axes.titlecolor': '#eceff4',
'text.color': '#eceff4',
'xtick.color': '#d8dee9',
'ytick.color': '#d8dee9',
'grid.color': '#4c566a',
'grid.alpha': 0.6,
'axes.grid': True,
'legend.facecolor': '#3b4252',
'legend.edgecolor': '#4c566a',
'legend.labelcolor': '#eceff4'
}
with plt.rc_context(dark_style):
fig, axes = plt.subplots(2, 2, figsize=(16, 12))
# 1. 线图
axes[0, 0].plot(self.x, self.y1, 'o-', label='数据1',
color='#88c0d0', linewidth=2.5)
axes[0, 0].plot(self.x, self.y2, 's-', label='数据2',
color='#a3be8c', linewidth=2.5)
axes[0, 0].set_title('深色主题线图', fontsize=16, fontweight='bold')
axes[0, 0].set_xlabel('X轴')
axes[0, 0].set_ylabel('Y轴')
axes[0, 0].legend()
# 2. 柱状图
nord_colors = ['#5e81ac', '#81a1c1', '#88c0d0', '#8fbcbb', '#a3be8c']
bars = axes[0, 1].bar(self.categories, self.values,
color=nord_colors, alpha=0.9)
axes[0, 1].set_title('深色主题柱状图', fontsize=16, fontweight='bold')
axes[0, 1].set_xlabel('类别')
axes[0, 1].set_ylabel('数值')
# 3. 热力图
data = np.random.randn(10, 10)
im = axes[1, 0].imshow(data, cmap='RdYlBu_r', aspect='auto')
axes[1, 0].set_title('深色主题热力图', fontsize=16, fontweight='bold')
plt.colorbar(im, ax=axes[1, 0])
# 4. 极坐标图
theta = np.linspace(0, 2*np.pi, 100)
r = 1 + 0.3*np.sin(5*theta)
ax_polar = plt.subplot(2, 2, 4, projection='polar')
ax_polar.plot(theta, r, color='#bf616a', linewidth=3)
ax_polar.fill(theta, r, alpha=0.3, color='#bf616a')
ax_polar.set_title('深色主题极坐标图', fontsize=16, fontweight='bold',
color='#eceff4', pad=20)
ax_polar.set_facecolor('#3b4252')
ax_polar.grid(True, color='#4c566a', alpha=0.6)
plt.tight_layout()
plt.show()
# 使用示例
style_demo = StyleThemeDemo()
style_demo.builtin_styles_demo()
style_demo.seaborn_styles_demo()
style_demo.custom_style_demo()
style_demo.dark_theme_demo()