【附代码】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]
相关推荐
上不如老下不如小20 分钟前
2025年第七届全国高校计算机能力挑战赛初赛 Python组 编程题汇总
开发语言·python·算法
Q_Q51100828527 分钟前
python+django/flask的结合人脸识别和实名认证的校园论坛系统
spring boot·python·django·flask·node.js·php
Q_Q51100828528 分钟前
python+django/flask的选课系统与课程评价整合系统
spring boot·python·django·flask·node.js·php
charlie11451419133 分钟前
勇闯前后端Week2:后端基础——Flask API速览
笔记·后端·python·学习·flask·教程
豐儀麟阁贵44 分钟前
8.2异常的抛出与捕捉
java·开发语言·python
interception1 小时前
爬虫js逆向,jsdom补环境,抖音,a_bogus
javascript·爬虫·python
林炳然1 小时前
Python-Basic Day-5 函数-生成器&装饰器
python
Yue丶越1 小时前
【Python】基础语法入门(四)
linux·开发语言·python
AI街潜水的八角5 小时前
Python电脑屏幕&摄像头录制软件(提供源代码)
开发语言·python