V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
lyricorpse
V2EX  ›  Python

有什么办法统一 macOS 和 Linux 下 Python 的 matplotlib 的字体配置

  •  
  •   lyricorpse · 2018-09-11 06:18:08 +08:00 · 2266 次点击
    这是一个创建于 2026 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我经常在两个平台切换,发现用 python 的 matplotlib 画图时,同样的代码生产的图片中字体不同大小也不同导致排版失真。用

    import matplotlib
    matplotlib.rcParams
    

    查看关于 font 的配置在两个平台下是一致的,区别大概是有些字体在某个平台下没装吧。

    请问有什么办法可以快速方便的统一两平台下的字体设置?比如有没有办法查询 macOS 下默认用了哪些字体?是不是安装上那些字体后 Linux 下就会默认用同样的字体了?谢谢!

    第 1 条附言  ·  2018-09-13 01:58:39 +08:00
    感谢各位的回复,最终发现问题在于:在 macOS 下 sans-serif 首选为 Arial,而 Linux 下首选为 Dejavu Sans,并且 Linux 下默认没有安装 Arial,所以 Linux 需要安装 Arial 然后设成首选就可以了
    7 条回复    2018-09-12 11:56:36 +08:00
    thedrwu
        1
    thedrwu  
       2018-09-11 06:44:55 +08:00 via Android   ❤️ 1
    1、不同系统用统一的字体(自己在每个系统上安装好)。免费的字体既全面(数学、希腊、Cyrillic )且好看的不多,为了排好看的字体,几百美元免不了。
    2、手动配置 rcparams,包括数学字体( mathtext ),设置好字号。
    3、 手动设置好 figure 的尺寸和 dpi。
    ( 4、我一般做成 svg,从 inkscape 里重新修改边界,做最后的调整,导出成别的格式。)

    图片完全可移植。插到文档里 kerning、ligature、大小、 行间距之类的都与文档很和谐。
    ipwx
        2
    ipwx  
       2018-09-11 09:39:24 +08:00   ❤️ 1
    考虑一下全面使用 LaTeX 渲染所有文本 。

    https://matplotlib.org/users/usetex.html

    然后你的问题就变成了,两个操作系统各装一遍 LaTeX。
    ipwx
        3
    ipwx  
       2018-09-11 09:40:24 +08:00
    @thedrwu 装了 LaTeX 可以用 pdfcrop 裁 pdf 白边。当然 png 可以用 imagemagick 裁白边。
    lyricorpse
        4
    lyricorpse  
    OP
       2018-09-11 10:02:01 +08:00
    @thedrwu 谢谢!我对 macOS 下默认的效果还是满意的,并不想在 Linux 下手动去全部配置一遍,但现在就是不清楚 macOS 下默认用了什么字体,有办法查询么? rcParams 里面都是给一个列表,如果我的理解没错的话,排在前面的字体优先选择,没装的话就用下一个。

    @ipwx 谢谢!是个好办法
    ipwx
        5
    ipwx  
       2018-09-11 11:25:14 +08:00 via iPhone
    @lyricorpse 要注意像 sans, serif, monospace 这种字体是操作系统 dependent 的。
    icymorn
        6
    icymorn  
       2018-09-11 22:51:12 +08:00   ❤️ 1
    lyricorpse
        7
    lyricorpse  
    OP
       2018-09-12 11:56:36 +08:00
    @ipwx Good point!
    @icymorn 谢谢!原来是那样查询字体的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5338 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 08:21 · PVG 16:21 · LAX 01:21 · JFK 04:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.