You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu系统下Matplotlib的xkcd()样式无法正常使用求助

Hey there, let's work through this xkcd style issue step by step—since you've already tried the basics, let's dive into some targeted fixes that might resolve the problem on your Ubuntu 16.04 + Python 2.7 setup.

1. Double-check if your fonts are actually recognized by the system

First, confirm that the Humor Sans or xkcd fonts are properly detected by your system. Run this command in your terminal:

fc-list | grep -i "humor\|xkcd"

If you get no output, the fonts aren't installed correctly. Move the .ttf font files to either /usr/share/fonts/ (system-wide) or ~/.fonts/ (user-specific), then re-run the font cache update:

fc-cache -fv

Repeat the fc-list command to verify the fonts show up.

2. Force Matplotlib to use the correct font explicitly

Sometimes Matplotlib doesn't auto-pick up the xkcd-compatible fonts, so you can hardcode it in your script. Here's how to modify the example code:

import numpy as np
import matplotlib.pyplot as plt

# Enable xkcd mode first
plt.xkcd()
# Manually set the font to Humor Sans or xkcd Script
plt.rcParams['font.family'] = 'Humor Sans'
# If you're using the xkcd Script font, use this instead:
# plt.rcParams['font.family'] = 'xkcd Script'

# Run the sample plot code
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
plt.xticks([])
plt.yticks([])
ax.set_ylim([-30, 10])

data = np.ones(100)
data[70:] -= np.arange(30)

plt.annotate(
    'THE DAY I REALIZED\nI COULD COOK BACON\nWHENEVER I WANTED',
    xy=(70, 1), arrowprops=dict(arrowstyle='->'), xytext=(15, -10))

plt.plot(data)
plt.xlabel('time')
plt.ylabel('my overall health')

plt.show()
3. Clear Matplotlib's cached font data

Matplotlib caches font information, and old cache files can prevent new fonts from being used. Delete the cache directory with this command:

rm -rf ~/.cache/matplotlib/

Restart your Python session and run the xkcd script again.

4. Try switching Matplotlib's backend

Some backends (like the default one on Ubuntu) might have issues rendering xkcd-style fonts. Test with a different backend by adding this at the very top of your script:

import matplotlib
matplotlib.use('TkAgg') # Or try 'Qt5Agg' if you have Qt installed

Then proceed with the rest of your xkcd code.

5. Update Matplotlib to the latest compatible version

You're using Matplotlib 2.2.2—Python 2.7 supports up to Matplotlib 2.2.5, which includes bug fixes that might resolve your issue. Upgrade with pip:

pip install matplotlib==2.2.5

Re-test the xkcd example after the update.

Give these steps a try one by one—most of the time, this issue boils down to font recognition problems or stale cached settings. Let me know if any of these get the xkcd style working for you!

内容的提问来源于stack exchange,提问作者Micha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:36:17