網站首頁 編程語言 正文
XKCD
xkcd是蘭道爾·門羅(Randall Munroe)的網名,又是他所創作的漫畫的名稱。作者蘭道爾·門羅(Randall Munroe)給作品的定義是一部“關于浪漫、諷刺、數學和語言的網絡漫畫”(A webcomic of romance,sarcasm, math, and language),被網友譽為深度宅向網絡漫畫。XKCD官方網站https://xkcd.com/。
matplotlib對XKCD風格的支持
matplotlib.pyplot.xkcd函數可繪制XKCD風格的圖表。
原理非常簡單,調用函數時保存原有rcParams設置,再更新rcParams使預置的XKCD風格的生效,退出時還原rcParams設置。
xkcd相關定義如下:
def xkcd(scale=1, length=100, randomness=2): return _xkcd(scale, length, randomness) class _xkcd: # This cannot be implemented in terms of rc_context() because this needs to # work as a non-contextmanager too. def __init__(self, scale, length, randomness): self._orig = rcParams.copy() if rcParams['text.usetex']: raise RuntimeError( "xkcd mode is not compatible with text.usetex = True") from matplotlib import patheffects rcParams.update({ 'font.family': ['xkcd', 'xkcd Script', 'Humor Sans', 'Comic Neue', 'Comic Sans MS'], 'font.size': 14.0, 'path.sketch': (scale, length, randomness), 'path.effects': [ patheffects.withStroke(linewidth=4, foreground="w")], 'axes.linewidth': 1.5, 'lines.linewidth': 2.0, 'figure.facecolor': 'white', 'grid.linewidth': 0.0, 'axes.grid': False, 'axes.unicode_minus': False, 'axes.edgecolor': 'black', 'xtick.major.size': 8, 'xtick.major.width': 3, 'ytick.major.size': 8, 'ytick.major.width': 3, }) def __enter__(self): return self def __exit__(self, *args): dict.update(rcParams, self._orig)
創建XKCD風格的圖表
官方建議使用上下文管理器調用xkcd函數。
import matplotlib.pyplot as plt with plt.xkcd(): plt.bar([1,2,3],[1,2,3]) plt.title('test') plt.show()
使用中文字體創建XKCD風格的圖表
官方文檔建議下載Humor Sans字體,根據源碼可知,'font.family': ['xkcd', 'xkcd Script', 'Humor Sans', 'Comic Neue', 'Comic Sans MS'],只要計算機上安裝這幾個字體,英文都可以顯示為XKCD風格,現在Windows操作系統中基本都預裝有Comic Sans MS字體,因此,不用下載字體即可顯示英文。
xkcd默認配置的幾個字體都不支持中文,如果像在XKCD風格圖表中使用類似漫畫風格的中文就需要下載中文字體,一般大家都推薦試用方正卡通簡體字體。下載安裝該字體后,只用重載字體緩存,修改rcParams['font.family']使中文字體生效即可。
1.安裝字體
下載方正卡通簡體字體,并進行安裝。
2.更新中文字體
獲取方正卡通簡體字體的系統名稱
方正卡通簡體字體在系統中的名稱為FZKaTong-M19S。
設置方正卡通簡體字體為中文默認字體
import matplotlib.pyplot as plt plt.xkcd() plt.rcParams.update({'font.family': "FZKaTong-M19S"}) plt.bar([1,2,3],[1,2,3]) plt.title("測試") plt.show()
運行后,標題中文不能正常顯示,調試信息顯示找不到'FZKaTong-M19S',所以使用默認的DejaVu Sans的字體。
findfont: Font family ['FZKaTong-M19S'] not found. Falling back to DejaVu Sans.
通過以下代碼驗證,可知'FZKaTong-M19S'即方正卡通簡體字體沒有出現在ttflist當中,所以找不到該字體。而ttflist是讀取字體緩存而構建的,因此,重建字體緩存可能解決這個問題。
from matplotlib.font_manager import fontManager print([i.name for i in fontManager.ttflist if 'FZKaTong-M19S' in i.name])
解決問題
默認findfont函數是從字體緩存中查找的,新安裝的字體緩存中沒有,因此,需要重新創建緩存,并加載。
# 重建字體緩存 from matplotlib.font_manager import _rebuild _rebuild()
import matplotlib.pyplot as plt plt.xkcd() plt.rcParams.update({'font.family': "FZKaTong-M19S"}) # plt.rcParams['font.family'] ='FZKaTong-M19S' # plt.rc('font', **{'family' : 'FZKaTong-M19S'}) plt.bar([1,2,3],[1,2,3]) plt.title("測試") plt.show()
原文鏈接:https://blog.csdn.net/mighty13/article/details/111873960
相關推薦
- 2022-11-01 詳解批處理文件語法_DOS/BAT
- 2023-01-18 React?Fiber?樹思想解決業務實際場景詳解_React
- 2022-09-21 Android開發Activity的生命周期詳解_Android
- 2023-03-27 基于Unity3D實現仿真時鐘詳解_C#教程
- 2022-08-02 C#如何使用Task類解決線程的等待問題_C#教程
- 2022-12-19 批處理bat腳本獲取打包發布問題記錄_DOS/BAT
- 2022-04-12 Python實現批量向PDF文件添加中文水印_python
- 2022-05-29 C++中的類的大小詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支