简体   繁体   English

在无头服务器上运行的matplotlib的Python3字体

[英]Python3 fonts for matplotlib running on headless server

I wrote a python3 (3.4.3) script that generates some graphs using matplotlib and seaborn, and it works wonderfully on my laptop running ubuntu 14.04. 我编写了一个python3(3.4.3)脚本,该脚本使用matplotlib和seaborn生成了一些图形,并且在运行ubuntu 14.04的笔记本电脑上运行非常出色。

I want to schedule it to run as a cron job on my server, which is running ubuntu-server. 我想安排它在运行ubuntu-server的服务器上作为cron作业运行。

To get it to run at all, I had to change the matplotlib backend with matplotlib.use("Agg") 要使其完全运行,我必须使用matplotlib.use("Agg")更改matplotlib后端

The script now runs fine, but matplotlib can't find some fonts: 脚本现在可以正常运行,但是matplotlib找不到某些字体:

/usr/lib/python3/dist-packages/matplotlib/font_manager.py:1236:UserWarning: findfont: 
Font family ['sans-serif'] not found. Falling back to Bitstream Vera Sans
(prop.get_family(), self.defaultFamily[fontext]))

/usr/lib/python3/dist-packages/matplotlib/font_manager.py:1246: UserWarning: findfont: 
Could not match :family=Bitstream Vera Sans:style=normal:variant=normal:weight=normal:stretch=normal:size=10.0.
Returning /usr/share/matplotlib/mpl-data/fonts/ttf/cmtt10.ttf
UserWarning)

The substitute fonts are quite ugly, and I have not been able to figure out how to install the fonts that are used by matplotlib/seaborn by default. 替代字体非常丑陋,我无法弄清楚如何安装默认情况下由matplotlib / seaborn使用的字体。

How do I install the default fonts? 如何安装默认字体? Easy solutions (eg using apt-get or pip are preferred). 简单的解决方案(例如,首选apt-getpip )。

The default seaborn font is Arial, a Microsoft font. 默认的seaborn字体是Microsoft字体Arial。 I think you should be able to install it via apt-get with apt-get install msttcorefonts -qq . 我认为您应该能够使用apt-get install msttcorefonts -qq通过apt-get安装它。

The easiest solution I found was to download the font as a .ttf and set matplotlib to use that ttf: 我找到的最简单的解决方案是将字体下载为.ttf并设置matplotlib以使用该ttf:

fontprop = matplotlib.font_manager.FontProperties(fname = "path/fontname.ttf")
matplotlib.rcParams['font.family'] = fontprop.get_name()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM