【附代码】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]
相关推荐
Java后端的Ai之路2 小时前
【Python 教程15】-Python和Web
python
冬奇Lab4 小时前
一天一个开源项目(第15篇):MapToPoster - 用代码将城市地图转换为精美的海报设计
python·开源
二十雨辰6 小时前
[python]-AI大模型
开发语言·人工智能·python
Yvonne爱编码6 小时前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
前端摸鱼匠7 小时前
YOLOv8 环境配置全攻略:Python、PyTorch 与 CUDA 的和谐共生
人工智能·pytorch·python·yolo·目标检测
WangYaolove13147 小时前
基于python的在线水果销售系统(源码+文档)
python·mysql·django·毕业设计·源码
AALoveTouch7 小时前
大麦网协议分析
javascript·python
ZH15455891317 小时前
Flutter for OpenHarmony Python学习助手实战:自动化脚本开发的实现
python·学习·flutter
xcLeigh8 小时前
Python入门:Python3 requests模块全面学习教程
开发语言·python·学习·模块·python3·requests
xcLeigh8 小时前
Python入门:Python3 statistics模块全面学习教程
开发语言·python·学习·模块·python3·statistics