惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
D
DataBreaches.Net
D
Docker
云风的 BLOG
云风的 BLOG
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
罗磊的独立博客
Martin Fowler
Martin Fowler
U
Unit 42
Engineering at Meta
Engineering at Meta
IT之家
IT之家
Vercel News
Vercel News
B
Blog RSS Feed
人人都是产品经理
人人都是产品经理
博客园 - Franky
博客园 - 【当耐特】
Stack Overflow Blog
Stack Overflow Blog
G
Google Developers Blog
MongoDB | Blog
MongoDB | Blog

博客园 - 提高效率!

Python多进程处理数据 内网配置深度学习环境 Vscode个人配置喜好记录 python读取tfrecord单个数据 用python直接提取gee数据的value(无需下载) 从tiff影像中提取多个点的像素值 WSL下挂载移动硬盘 ubuntu统计当前目录下的文件数量 常见的距离算法和相似度计算方法 常用的坐标系及其EPSG编码 仿 GEE 的平台汇总 stata 学习 matplotlib 颜色速查 Matplotlib 设置画布 Landsat 使用QA波段去云 将社会脆弱性纳入高分辨率全球洪水风险绘图 Python绘图代码snippets Micro-Estimates of Wealth for all Low 数据搜集 Latex 符号速查表 Ubuntu 服务器常用命令
解决proplot和Matplotlib版本冲突问题
提高效率! · 2024-12-25 · via 博客园 - 提高效率!

解决proplot和Matplotlib版本冲突问题
在环境内运行PY代码

import importlib
import os
import re
import subprocess
import sys
import logging
 
 
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s -%(message)s')
 
 
# Just need matplotlib and numpy :)
subprocess.check_output([sys.executable, "-m", "pip", "install", 'numpy', 'matplotlib'])
subprocess.check_output([sys.executable, "-m", "pip", "install", 'proplot', '--no-dependencies'])

proplot_path = importlib.machinery.PathFinder().find_spec('proplot').submodule_search_locations[0]
 
 
def alter(file, old_str, new_str):
    with open(file, "r", encoding="utf-8") as f1, open(f"{file}.bak", "w", encoding="utf-8") as f2:
        txt = f1.read()
        changed_txt = re.sub(old_str, new_str, txt, re.S | re.M)
        f2.write(changed_txt)
 
 
    os.remove(file)
    os.rename(f"{file}.bak", file)
 
 
ori_path_1 = os.path.join(proplot_path, 'colors.py')
ori_str_1 = \
"""    attr = '_cmap_registry' if hasattr\(mcm, '_cmap_registry'\) else 'cmap_d'"""
new_str_1 = \
"""    # attr = '_cmap_registry' if hasattr(mcm, '_cmap_registry') else 'cmap_d'
    attr = '_colormaps' if hasattr(mcm, '_colormaps') else 'cmap_d'"""
 
 
ori_path_2 = os.path.join(proplot_path, 'figure.py')
ori_str_2 = \
"""        if self\._cachedRenderer:
            renderer = self\._cachedRenderer"""
new_str_2 = \
"""        # if self._cachedRenderer:
        # renderer = self._cachedRenderer
        # if self.canvas._get_renderer():
        # renderer = self.canvas._get_renderer()
        # else:
        canvas = self.canvas
        if canvas and hasattr(canvas, 'get_renderer'):
            renderer = canvas.get_renderer()"""
 
 
ori_path_3 = os.path.join(proplot_path, 'figure.py')
ori_str_3 = \
"""            canvas = self\.canvas
            if canvas and hasattr\(canvas, 'get_renderer'\):
                renderer = canvas\.get_renderer\(\)
            else:
                from matplotlib\.backends.backend_agg import FigureCanvasAgg
                canvas = FigureCanvasAgg\(self\)
                renderer = canvas\.get_renderer\(\)"""
new_str_3 = \
"""            # from matplotlib.backends.backend_agg import FigureCanvasAgg
            # canvas = FigureCanvasAgg(self)
            # renderer = canvas.get_renderer()
            from matplotlib import backend_bases
            renderer = backend_bases._get_renderer(self)"""
 
 
ori_path_4 = os.path.join(proplot_path, 'internals/rcsetup.py')
ori_str_4 = "CMAPSEQ = 'Fire'"
new_str_4 = "CMAPSEQ = 'magma'"
ori_path_5 = os.path.join(proplot_path, '__init__.py')
ori_str_5 = "import pkg_resources as pkg"
new_str_5 = "import importlib_metadata"
 
 
ori_path_6 = os.path.join(proplot_path, '__init__.py')
ori_str_6 = \
"""try:
    version = __version__ = pkg\.get_distribution\(__name__\)\.version
except pkg\.DistributionNotFound:
    version = __version__ = 'unknown'"""
new_str_6 = "version = __version__ = importlib_metadata.metadata('proplot').get('version')"
 
 
alter(ori_path_1, ori_str_1, new_str_1)
alter(ori_path_2, ori_str_2, new_str_2)
alter(ori_path_3, ori_str_3, new_str_3)
alter(ori_path_4, ori_str_4, new_str_4)
alter(ori_path_5, ori_str_5, new_str_5)
alter(ori_path_6, ori_str_6, new_str_6)
 
 
try:
    proplot = importlib.import_module('proplot')
    print(f"proplot安装成功 version: {proplot.__version__}")
except Exception as e:
    print(f"proplot安装失败: {e}")