Python绘图与可视化Skill python-plotting

这个技能专注于使用Python进行数据可视化,涵盖matplotlib、seaborn和plotly三个主要库。matplotlib用于创建静态、高质量的图形,适合出版物;seaborn提供简洁的统计可视化,便于数据分析;plotly支持交互式图形和仪表板,适合Web应用和探索性分析。内容包括绘图类型、自定义选项、最佳实践和库选择指南,帮助用户有效传达数据见解。关键词:数据可视化、Python绘图、matplotlib、seaborn、plotly、统计图形、交互图、数据沟通。

数据可视化 0 次安装 0 次浏览 更新于 3/12/2026

name: python-plotting description: Python中的全面绘图与可视化 - matplotlib(静态出版物质量图)、seaborn(统计可视化)和plotly(交互图);包括绘图类型、自定义、最佳实践和库选择指南 allowed-tools: [“*”]

Python绘图与可视化

概述

通过三个互补的库掌握Python中的数据可视化:matplotlib(基础静态图)、seaborn(统计可视化)和plotly(交互图形)。每个库都有其独特优势,知道何时使用哪个——或如何结合使用——是有效数据沟通的关键。

核心价值: 使用正确的工具为每个可视化需求创建出版物质量的静态图、有洞察力的统计图形和交互式仪表板。

库选择指南

Matplotlib - 基础

使用时机:

  • 需要对每个绘图元素进行精细控制
  • 创建出版物质量的静态图形
  • 构建其他库中不可用的自定义可视化
  • 处理低级绘图需求
  • 需要最大兼容性(最广泛支持)

优势:

  • 对每个视觉元素的完全控制
  • 成熟、稳定、文档详尽
  • seaborn和其他库的基础
  • 导出到任何格式(PDF、SVG、PNG等)
  • 广泛的自定义选项

劣势:

  • 对于常见统计图冗长
  • 复杂图的学习曲线较陡
  • 静态输出(无交互性)
  • 默认美学较旧

Seaborn - 统计可视化

使用时机:

  • 创建统计图(分布、相关性、回归)
  • 需要美观的默认设置和最小代码
  • 处理pandas DataFrames
  • 想要自动统计估计
  • 需要分面/多面板图

优势:

  • 统计图的简洁语法
  • 美丽的默认主题
  • 内置统计估计
  • 无缝pandas集成
  • FacetGrid用于复杂多面板布局

劣势:

  • 控制少于matplotlib
  • 仅限于统计图类型
  • 仅静态输出
  • 深度自定义需要理解底层matplotlib

Plotly - 交互可视化

使用时机:

  • 需要交互图(缩放、悬停、平移)
  • 构建仪表板或Web应用
  • 想要3D可视化
  • 需要动画
  • 分享探索性分析

优势:

  • 丰富的开箱即用交互性
  • 美丽的默认设置
  • 3D和地理绘图
  • 与Dash集成用于Web应用
  • 导出到HTML用于分享

劣势:

  • 文件大小较大
  • 出版物图中控制少于matplotlib
  • 不同的API范式
  • 不适合静态出版物图形

快速决策树

Need interactivity?
├─ Yes → Plotly
└─ No → Statistical plot?
    ├─ Yes → Seaborn (can customize with matplotlib)
    └─ No → Complex customization needed?
        ├─ Yes → Matplotlib
        └─ No → Seaborn or Matplotlib (preference)

Matplotlib - 基础绘图

两种API:pyplot vs 面向对象

pyplot(MATLAB风格,隐式状态):

import matplotlib.pyplot as plt

# 快速和交互式
plt.plot([1, 2, 3], [1, 4, 9])
plt.xlabel('x')
plt.ylabel('y')
plt.title('Simple Plot')
plt.show()

面向对象(显式,推荐用于复杂图):

import matplotlib.pyplot as plt

# 显式控制
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 4, 9])
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('Simple Plot')
plt.show()

推荐: 使用OO API用于脚本、函数和复杂图。使用pyplot用于快速交互探索。

图形解剖

import matplotlib.pyplot as plt
import numpy as np

# 创建带子图的图形
fig, axes = plt.subplots(2, 2, figsize=(10, 8))

# fig = 图形(整个窗口)
# axes = 轴数组(单个图)

# 访问单个轴
ax1 = axes[0, 0]  # 左上
ax2 = axes[0, 1]  # 右上
ax3 = axes[1, 0]  # 左下
ax4 = axes[1, 1]  # 右下

# 在每个轴上绘图
ax1.plot([1, 2, 3], [1, 4, 9])
ax2.scatter([1, 2, 3], [1, 4, 9])
ax3.bar([1, 2, 3], [1, 4, 9])
ax4.hist(np.random.randn(1000))

# 调整布局
plt.tight_layout()
plt.show()

常见绘图类型

线图:

fig, ax = plt.subplots()

x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

ax.plot(x, y1, label='sin(x)', linewidth=2, color='blue', linestyle='-')
ax.plot(x, y2, label='cos(x)', linewidth=2, color='red', linestyle='--')

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('三角函数')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()

散点图:

fig, ax = plt.subplots()

x = np.random.randn(100)
y = 2*x + np.random.randn(100)*0.5
colors = np.random.rand(100)
sizes = 100 * np.random.rand(100)

scatter = ax.scatter(x, y, c=colors, s=sizes, alpha=0.6, cmap='viridis')

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title('带颜色和大小的散点图')

# 添加颜色条
plt.colorbar(scatter, ax=ax, label='颜色值')
plt.show()

柱状图:

fig, ax = plt.subplots()

categories = ['A', 'B', 'C', 'D', 'E']
values = [23, 45, 56, 78, 32]

bars = ax.bar(categories, values, color=['red', 'blue', 'green', 'orange', 'purple'])

# 注释柱
for bar, value in zip(bars, values):
    height = bar.get_height()
    ax.text(bar.get_x() + bar.get_width()/2., height,
            f'{value}',
            ha='center', va='bottom')

ax.set_ylabel('值')
ax.set_title('柱状图')
plt.show()

直方图:

fig, ax = plt.subplots()

data = np.random.randn(1000)

ax.hist(data, bins=30, alpha=0.7, color='skyblue', edgecolor='black')

ax.set_xlabel('值')
ax.set_ylabel('频率')
ax.set_title('直方图')
ax.axvline(data.mean(), color='red', linestyle='--', linewidth=2, label='均值')
ax.legend()
plt.show()

共享轴的子图:

fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(10, 6))

x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

ax1.plot(x, y1)
ax1.set_ylabel('sin(x)')
ax1.grid(True)

ax2.plot(x, y2, color='red')
ax2.set_xlabel('x')
ax2.set_ylabel('cos(x)')
ax2.grid(True)

plt.tight_layout()
plt.show()

自定义

样式:

import matplotlib.pyplot as plt

# 列出可用样式
print(plt.style.available)

# 使用样式
plt.style.use('seaborn-v0_8-darkgrid')
# 或
with plt.style.context('ggplot'):
    plt.plot([1, 2, 3], [1, 4, 9])
    plt.show()

颜色和色彩映射:

# 命名颜色
ax.plot(x, y, color='steelblue')
ax.plot(x, y, color='#FF6347')  # 十六进制
ax.plot(x, y, color=(0.2, 0.4, 0.6))  # RGB

# 色彩映射
from matplotlib import cm

colors = cm.viridis(np.linspace(0, 1, 10))
for i, color in enumerate(colors):
    ax.plot([i, i+1], [0, 1], color=color)

标记和线型:

ax.plot(x, y,
        marker='o',           # 圆形标记
        markersize=8,
        markerfacecolor='red',
        markeredgecolor='black',
        markeredgewidth=2,
        linestyle='--',       # 虚线
        linewidth=2,
        color='blue')

图例:

ax.plot(x, y1, label='数据1')
ax.plot(x, y2, label='数据2')

ax.legend(
    loc='upper right',    # 或'best', 'center'等
    frameon=True,
    shadow=True,
    fancybox=True,
    fontsize=12
)

注释:

ax.plot(x, y)

# 注释点
ax.annotate(
    '最大值',
    xy=(x[50], y[50]),       # 要注释的点
    xytext=(x[50]+1, y[50]+1),  # 文本位置
    arrowprops=dict(arrowstyle='->', color='red', lw=2),
    fontsize=12,
    color='red'
)

保存图形:

# 高分辨率PNG
fig.savefig('plot.png', dpi=300, bbox_inches='tight')

# 出版物向量格式
fig.savefig('plot.pdf', bbox_inches='tight')
fig.savefig('plot.svg', bbox_inches='tight')

# 透明背景
fig.savefig('plot.png', transparent=True, bbox_inches='tight')

Seaborn - 统计可视化

基本设置

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# 设置主题
sns.set_theme(style='whitegrid')  # whitegrid, darkgrid, white, dark, ticks

# 加载示例数据集
tips = sns.load_dataset('tips')  # 内置数据集用于示例

分布图

带KDE的直方图:

fig, ax = plt.subplots()

sns.histplot(data=tips, x='total_bill', kde=True, ax=ax)

ax.set_title('总账单分布')
plt.show()

KDE图:

fig, ax = plt.subplots()

sns.kdeplot(data=tips, x='total_bill', hue='time', fill=True, ax=ax)

ax.set_title('按时间分的总账单分布')
plt.show()

分布图(ECDF):

fig, ax = plt.subplots()

sns.ecdfplot(data=tips, x='total_bill', hue='sex', ax=ax)

ax.set_title('总账单累积分布')
plt.show()

分类图

箱线图:

fig, ax = plt.subplots(figsize=(10, 6))

sns.boxplot(data=tips, x='day', y='total_bill', hue='sex', ax=ax)

ax.set_title('按天和性别分的总账单')
plt.show()

小提琴图:

fig, ax = plt.subplots(figsize=(10, 6))

sns.violinplot(data=tips, x='day', y='total_bill', hue='sex',
               split=True, ax=ax)

ax.set_title('按天分的总账单分布')
plt.show()

条带图/蜂群图:

fig, axes = plt.subplots(1, 2, figsize=(14, 6))

# 条带图(带抖动)
sns.stripplot(data=tips, x='day', y='total_bill', hue='sex',
              dodge=True, alpha=0.6, ax=axes[0])
axes[0].set_title('条带图')

# 蜂群图(无重叠)
sns.swarmplot(data=tips, x='day', y='total_bill', hue='sex',
              dodge=True, ax=axes[1])
axes[1].set_title('蜂群图')

plt.tight_layout()
plt.show()

带误差条的柱状图:

fig, ax = plt.subplots()

sns.barplot(data=tips, x='day', y='total_bill', hue='sex',
            ci=95,  # 置信区间
            ax=ax)

ax.set_title('按天分的平均总账单')
plt.show()

关系图

散点图:

fig, ax = plt.subplots(figsize=(10, 6))

sns.scatterplot(data=tips, x='total_bill', y='tip',
                hue='time', size='size', style='sex',
                sizes=(50, 200), alpha=0.6, ax=ax)

ax.set_title('小费 vs 总账单')
plt.show()

线图:

# 创建时间序列数据
fmri = sns.load_dataset('fmri')

fig, ax = plt.subplots(figsize=(10, 6))

sns.lineplot(data=fmri, x='timepoint', y='signal',
             hue='event', style='region', ax=ax)

ax.set_title('fMRI信号随时间变化')
plt.show()

回归图

线性回归:

fig, ax = plt.subplots()

sns.regplot(data=tips, x='total_bill', y='tip', ax=ax)

ax.set_title('线性回归:小费 vs 总账单')
plt.show()

残差图:

fig, ax = plt.subplots()

sns.residplot(data=tips, x='total_bill', y='tip', ax=ax)

ax.set_title('残差图')
ax.axhline(0, color='red', linestyle='--')
plt.show()

热图和矩阵

相关性热图:

fig, ax = plt.subplots(figsize=(8, 6))

# 计算相关性矩阵
corr = tips[['total_bill', 'tip', 'size']].corr()

sns.heatmap(corr, annot=True, cmap='coolwarm', center=0,
            square=True, linewidths=1, ax=ax)

ax.set_title('相关性矩阵')
plt.show()

聚类热图:

# 层次聚类热图
iris = sns.load_dataset('iris')
iris_data = iris.drop('species', axis=1)

g = sns.clustermap(iris_data.T, cmap='viridis',
                   standard_scale=1, figsize=(8, 8))
plt.show()

多面板图

FacetGrid:

# 创建图网格
g = sns.FacetGrid(tips, col='time', row='sex',
                  margin_titles=True, height=4)

# 映射绘图类型到网格
g.map(sns.scatterplot, 'total_bill', 'tip', alpha=0.6)

# 自定义
g.set_axis_labels('总账单', '小费')
g.set_titles(col_template="{col_name}", row_template="{row_name}")
g.add_legend()

plt.show()

PairGrid:

# 成对关系
iris = sns.load_dataset('iris')

g = sns.PairGrid(iris, hue='species', height=2.5)
g.map_upper(sns.scatterplot)
g.map_lower(sns.kdeplot)
g.map_diag(sns.histplot)
g.add_legend()

plt.show()

Pairplot(简化):

sns.pairplot(iris, hue='species', diag_kind='kde', height=2.5)
plt.show()

主题和调色板

设置主题:

# 主题样式
sns.set_theme(style='whitegrid')  # white, dark, whitegrid, darkgrid, ticks

# 上下文(缩放元素)
sns.set_context('talk')  # paper, notebook, talk, poster

# 组合
sns.set_theme(style='darkgrid', context='poster',
              palette='deep', font_scale=1.2)

调色板:

# 定性(分类)
sns.set_palette('deep')  # deep, muted, pastel, bright, dark, colorblind

# 顺序
sns.set_palette('Blues')

# 发散
sns.set_palette('coolwarm')

# 自定义
custom_palette = ['#FF6347', '#4682B4', '#32CD32']
sns.set_palette(custom_palette)

# 查看调色板
sns.palplot(sns.color_palette('husl', 10))
plt.show()

Plotly - 交互可视化

Plotly Express - 高级API

散点图:

import plotly.express as px

df = px.data.iris()

fig = px.scatter(df, x='sepal_width', y='sepal_length',
                 color='species', size='petal_length',
                 hover_data=['petal_width'],
                 title='Iris数据集')

fig.show()

线图:

df = px.data.gapminder()

fig = px.line(df[df['country'] == 'Canada'],
              x='year', y='gdpPercap',
              title='加拿大人均GDP')

fig.show()

柱状图:

df = px.data.tips()

fig = px.bar(df, x='day', y='total_bill', color='sex',
             barmode='group',  # 或'stack', 'overlay'
             title='按天和性别分的总账单')

fig.show()

直方图:

fig = px.histogram(df, x='total_bill', color='sex',
                   marginal='box',  # 或'violin', 'rug'
                   title='总账单分布')

fig.show()

箱线图:

fig = px.box(df, x='day', y='total_bill', color='sex',
             title='按天分的总账单')

fig.show()

热图:

import numpy as np

z = np.random.randn(20, 20)

fig = px.imshow(z, color_continuous_scale='RdBu_r',
                title='热图')

fig.show()

3D散点图:

df = px.data.iris()

fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_length',
                    color='species', size='petal_width',
                    title='3D Iris数据集')

fig.show()

动画图:

df = px.data.gapminder()

fig = px.scatter(df, x='gdpPercap', y='lifeExp',
                 animation_frame='year',
                 animation_group='country',
                 size='pop', color='continent',
                 hover_name='country',
                 log_x=True, size_max=60,
                 range_x=[100, 100000], range_y=[25, 90],
                 title='Gapminder数据随时间变化')

fig.show()

Graph Objects - 低级API

基本散点图:

import plotly.graph_objects as go

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

fig = go.Figure(data=go.Scatter(x=x, y=y, mode='markers+lines'))

fig.update_layout(
    title='自定义散点图',
    xaxis_title='X轴',
    yaxis_title='Y轴'
)

fig.show()

多个轨迹:

fig = go.Figure()

# 添加多个轨迹
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[1, 4, 9],
                         mode='lines', name='系列1'))
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[2, 5, 10],
                         mode='lines+markers', name='系列2'))

fig.update_layout(title='多个系列')
fig.show()

子图:

from plotly.subplots import make_subplots

fig = make_subplots(
    rows=2, cols=2,
    subplot_titles=('图1', '图2', '图3', '图4')
)

# 添加轨迹到特定子图
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[1, 4, 9]), row=1, col=1)
fig.add_trace(go.Bar(x=[1, 2, 3], y=[2, 5, 8]), row=1, col=2)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[3, 6, 9]), row=2, col=1)
fig.add_trace(go.Box(y=[1, 2, 3, 4, 5, 6, 7]), row=2, col=2)

fig.update_layout(height=600, showlegend=False, title_text='子图')
fig.show()

交互功能:

fig = go.Figure()

fig.add_trace(go.Scatter(x=[1, 2, 3, 4], y=[10, 11, 12, 13]))

# 添加下拉菜单
fig.update_layout(
    updatemenus=[
        dict(
            buttons=list([
                dict(label="线性",
                     method="relayout",
                     args=[{"yaxis.type": "linear"}]),
                dict(label="对数",
                     method="relayout",
                     args=[{"yaxis.type": "log"}])
            ]),
            direction="down",
        )
    ]
)

fig.show()

导出:

# 到HTML
fig.write_html('plot.html')

# 到静态图像(需要kaleido)
fig.write_image('plot.png', width=1200, height=800)
fig.write_image('plot.pdf')

集成模式

Seaborn与Matplotlib自定义

import seaborn as sns
import matplotlib.pyplot as plt

# 创建seaborn图
fig, ax = plt.subplots(figsize=(10, 6))
sns.boxplot(data=tips, x='day', y='total_bill', ax=ax)

# 用matplotlib自定义
ax.set_title('自定义Seaborn图', fontsize=16, fontweight='bold')
ax.set_xlabel('星期几', fontsize=12)
ax.set_ylabel('总账单($)', fontsize=12)
ax.grid(axis='y', alpha=0.3)

# 添加自定义注释
ax.text(0.5, 0.95, '自定义注释',
        transform=ax.transAxes, ha='center')

plt.tight_layout()
plt.show()

Plotly与Pandas

import pandas as pd
import plotly.express as px

# Pandas DataFrame
df = pd.DataFrame({
    'x': range(10),
    'y': [i**2 for i in range(10)],
    'category': ['A', 'B'] * 5
})

# 直接从DataFrame绘图
fig = px.line(df, x='x', y='y', color='category')
fig.show()

最佳实践

1. 选择合适的绘图类型

分布:

  • 直方图、KDE图、箱线图、小提琴图
  • 用途:理解数据分布和形状

比较:

  • 柱状图、箱线图、条带图
  • 用途:比较组或类别

关系:

  • 散点图、线图、回归图
  • 用途:显示相关性或趋势

组合:

  • 堆叠柱状图、饼图(谨慎使用)、树图
  • 用途:部分到整体的关系

时间序列:

  • 线图、面积图
  • 用途:时间模式

2. 设计原则

少即是多:

# 不好:太多装饰
fig, ax = plt.subplots()
ax.plot(x, y, linewidth=5, color='red', linestyle='--',
        marker='o', markersize=15, markerfacecolor='yellow')
ax.grid(True, linewidth=2, color='blue')
ax.set_facecolor('lightgray')

# 好:清晰和专注
fig, ax = plt.subplots()
ax.plot(x, y, linewidth=2, color='steelblue')
ax.grid(True, alpha=0.3)

有效使用颜色:

# 有目的地使用颜色
# - 分类:不同的色调
# - 顺序:单一色调渐变
# - 发散:从中性中心开始的两个色调

# 色盲友好调色板
sns.set_palette('colorblind')
# 或
import plotly.express as px
fig = px.scatter(df, x='x', y='y', color='category',
                 color_discrete_sequence=px.colors.qualitative.Safe)

可读文本:

fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(x, y)

# 清晰、适当大小的文本
ax.set_title('清晰标题', fontsize=16, pad=20)
ax.set_xlabel('X轴标签', fontsize=12)
ax.set_ylabel('Y轴标签', fontsize=12)
ax.tick_params(labelsize=10)

3. 一致样式

# 在开始时设置样式
import matplotlib.pyplot as plt
import seaborn as sns

sns.set_theme(style='whitegrid', context='talk')
plt.rcParams['figure.figsize'] = (10, 6)
plt.rcParams['figure.dpi'] = 100

# 所有图都将使用这些设置

4. 保存高质量图形

# 用于出版物
fig.savefig('figure.pdf', dpi=300, bbox_inches='tight')
fig.savefig('figure.png', dpi=300, bbox_inches='tight')

# 用于演示
fig.savefig('figure.png', dpi=150, bbox_inches='tight')

# 用于网页
fig.savefig('figure.png', dpi=96, bbox_inches='tight', optimize=True)

常见陷阱

1. 图形大小和DPI

# ❌ 不好:默认大小太小
fig, ax = plt.subplots()

# ✅ 好:设置适当大小
fig, ax = plt.subplots(figsize=(10, 6))  # 宽度,高度(英寸)

2. 重叠标签

# ❌ 不好:标签重叠
fig, ax = plt.subplots()
ax.bar(range(10), values)
ax.set_xticklabels(long_labels)

# ✅ 好:旋转标签
ax.set_xticklabels(long_labels, rotation=45, ha='right')

# ✅ 更好:使用tight_layout
plt.tight_layout()

3. 颜色映射一致性

# ❌ 不好:跨图颜色不一致
sns.scatterplot(data=df1, x='x', y='y', hue='category')
sns.scatterplot(data=df2, x='x', y='y', hue='category')  # 不同颜色!

# ✅ 好:定义调色板
palette = {'A': 'red', 'B': 'blue', 'C': 'green'}
sns.scatterplot(data=df1, x='x', y='y', hue='category', palette=palette)
sns.scatterplot(data=df2, x='x', y='y', hue='category', palette=palette)

4. Seaborn不返回轴

# ❌ 不好:期望返回值
ax = sns.boxplot(data=df, x='x', y='y')  # 返回轴,但...

# ✅ 好:明确传递轴
fig, ax = plt.subplots()
sns.boxplot(data=df, x='x', y='y', ax=ax)
ax.set_title('我的标题')  # 现在可以自定义

5. Plotly大型数据集内存

# ❌ 不好:绘制数百万点
fig = px.scatter(huge_df)  # 慢,文件大

# ✅ 好:采样或聚合
fig = px.scatter(huge_df.sample(10000))
# 或对巨大数据集使用datashader

6. Matplotlib状态机混淆

# ❌ 不好:混合pyplot和OO API
plt.figure()
ax = plt.gca()
ax.plot(x, y)
plt.xlabel('x')  # 状态机调用
ax.set_ylabel('y')  # 面向对象调用

# ✅ 好:保持一致
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_xlabel('x')
ax.set_ylabel('y')

快速参考

Matplotlib

# 创建图形
fig, ax = plt.subplots(figsize=(10, 6))

# 绘图类型
ax.plot(x, y)                    # 线图
ax.scatter(x, y)                 # 散点图
ax.bar(x, y)                     # 柱状图
ax.hist(data, bins=30)           # 直方图
ax.boxplot([data1, data2])       # 箱线图

# 自定义
ax.set_xlabel('X标签')
ax.set_ylabel('Y标签')
ax.set_title('标题')
ax.legend()
ax.grid(True)

# 保存
fig.savefig('plot.png', dpi=300, bbox_inches='tight')

Seaborn

import seaborn as sns

# 设置主题
sns.set_theme(style='whitegrid')

# 绘图类型
sns.histplot(data=df, x='col')
sns.scatterplot(data=df, x='x', y='y', hue='category')
sns.boxplot(data=df, x='category', y='value')
sns.heatmap(corr_matrix, annot=True)

# 多面板
sns.pairplot(df, hue='species')

Plotly Express

import plotly.express as px

# 绘图类型
fig = px.scatter(df, x='x', y='y', color='category')
fig = px.line(df, x='x', y='y')
fig = px.bar(df, x='x', y='y', color='category')
fig = px.histogram(df, x='value')
fig = px.box(df, x='category', y='value')

# 显示/保存
fig.show()
fig.write_html('plot.html')

安装

# Matplotlib
pip install matplotlib

# Seaborn
pip install seaborn

# Plotly
pip install plotly

# 用于静态图像导出(plotly)
pip install kaleido

# 全部安装
pip install matplotlib seaborn plotly kaleido

额外资源

相关技能

  • pycse - 科学计算,带置信区间绘图
  • python-ase - 原子结构可视化
  • python-best-practices - 可视化脚本的代码质量