【附代码】Jupyter 多进程调用 seaborn 并保留格式

文章目录

Jupyter 多进程调用 seaborn 格式会和单进程使用不统一,本文将解决以上问题。

Jupyter 多进程的简单例子

Jupyter 中直接使用多进程可能会有诸多问题,推荐把 function 放在 .py文件里,然后在 Jupyter 中多进程调用。

文件目录

bash 复制代码
PythonProject
├─csdn_jupyter_multiprocessing
│    example_multiprocessing.ipynb
│    function.py

代码

python 复制代码
# example_multiprocessing.ipynb

from concurrent.futures import ProcessPoolExecutor
from tqdm import tqdm
from csdn_jupyter_multiprocessing.function import square

if __name__ == '__main__':
    with ProcessPoolExecutor(max_workers=2) as executor:
        results = list(tqdm(
            executor.map(square, [1, 2, 3, 4, 5]),
            total=5,
            desc="计算中",
            unit="任务"
        ))
    print(results)
python 复制代码
# function.py

def square(x):
    return x * x

运行结果

bash 复制代码
计算中: 100%|██████████| 5/5 [00:00<00:00, 92.59任务/s]
[1, 4, 9, 16, 25]

Jupyter 多进程调用 seaborn(保留格式)

核心思路是主进程中把 sns.axes_style() 传递到子进程。

文件目录

bash 复制代码
PythonProject
├─csdn_jupyter_multiprocessing
│    example_multiprocessing.ipynb
│    function.py

代码

python 复制代码
# example_multiprocessing.ipynb

from concurrent.futures import ProcessPoolExecutor
from tqdm import tqdm
from csdn_jupyter_multiprocessing.function import seaborn_example
import seaborn as sns

if __name__ == '__main__':
    seaborn_style = sns.axes_style()
    with ProcessPoolExecutor(max_workers=2) as executor:
        list(tqdm(
            executor.map(seaborn_example, [('0.svg', seaborn_style), ('1.svg', seaborn_style)]),
            total=2,
            desc="计算中",
            unit="任务"
        ))
python 复制代码
# function.py

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

def seaborn_example(args):
    save_name, seaborn_style = args
    sns.set_style(seaborn_style)

    # 创建示例数据:模拟某产品每日销售额
    days = np.arange(1, 31)  # 1到30天
    sales = np.random.normal(loc=100, scale=15, size=30).cumsum() + 1000  # 模拟销售额趋势

    # 构建DataFrame
    data = pd.DataFrame({
        'Day': days,
        'Sales': sales
    })

    # 绘制基础线图
    sns.lineplot(data=data, x='Day', y='Sales')
    plt.savefig(save_name)
    plt.close()

运行结果

bash 复制代码
计算中: 100%|██████████| 2/2 [00:01<00:00,  1.66任务/s]
相关推荐
deephub5 小时前
FastMCP 入门:用 Python 快速搭建 MCP 服务器接入 LLM
服务器·人工智能·python·大语言模型·mcp
南宫乘风5 小时前
基于 Flask + APScheduler + MySQL 的自动报表系统设计
python·mysql·flask
番石榴AI5 小时前
基于机器学习优化的主图选择方法(酒店,景点,餐厅等APP上的主图展示推荐)
图像处理·人工智能·python·机器学习
qq7422349845 小时前
Python操作数据库之pyodbc
开发语言·数据库·python
2401_841495646 小时前
【自然语言处理】轻量版生成式语言模型GPT
人工智能·python·gpt·深度学习·语言模型·自然语言处理·transformer
云和数据.ChenGuang6 小时前
tensorflow生成随机数和张量
人工智能·python·tensorflow
测试老哥7 小时前
python+requests+excel 接口测试
自动化测试·软件测试·python·测试工具·测试用例·excel·接口测试
AI纪元故事会7 小时前
冰泪与雨丝:一个AI的Python挽歌
开发语言·人工智能·python
ColderYY7 小时前
Python连接MySQL数据库
数据库·python·mysql
寒秋丶8 小时前
Milvus:数据库层操作详解(二)
数据库·人工智能·python·ai·ai编程·milvus·向量数据库