Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python matplotlib to draw xkcd Animation style Chart

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use Python matplotlib to draw xkcd animation style charts, the article is very detailed, has a certain reference value, interested friends must read it!

XKCD

Xkcd is the screen name of Randall Monroe (Randall Munroe) and the name of his cartoons. Author Randall Munroe defines the work as "an online cartoon about romance, satire, mathematics and language" (A webcomic of romance,sarcasm, math, and language), which is praised by netizens as a deep homesick online cartoon. XKCD official website https://xkcd.com/.

Matplotlib's support for XKCD style

The matplotlib.pyplot.xkcd function can draw XKCD-style charts.

The principle is very simple: save the original rcParams settings when calling the function, then update the rcParams to make the preset XKCD style effective, and restore the rcParams settings when you exit.

The relevant definitions of xkcd are as follows:

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) create XKCD-style charts

It is officially recommended that you use the context manager to call the xkcd function.

Import matplotlib.pyplot as pltwith plt.xkcd (): plt.bar ([1 test' 2 plt.show 3], [1 recorder 2) plt.show ()

Use Chinese fonts to create XKCD-style charts

Official documents suggest downloading Humor Sans fonts, according to the source code, 'font.family': [' xkcd', 'xkcd Script',' Humor Sans', 'Comic Neue',' Comic Sans MS'], as long as these fonts are installed on the computer, English can be displayed in XKCD style. Now the Windows operating system is basically pre-installed with Comic Sans MS fonts, so there is no need to download fonts to display English.

Several fonts configured by default in xkcd do not support Chinese. If you use cartoon-like Chinese in XKCD-style charts, you need to download Chinese fonts. It is generally recommended to try simplified founder cartoon fonts. After downloading and installing the font, just overload the font cache and modify rcParams ['font.family'] to make the Chinese font effective.

1. Install Font

Download founder cartoon simplified font and install it.

two。 Update Chinese fonts

Get the system name of founder cartoon simplified font

The name of founder cartoon simplified font in the system is FZKaTong-M19S.

Set the founder cartoon simplified font to the Chinese default font

Import matplotlib.pyplot as pltplt.xkcd () plt.rcParams.update ({'font.family': "FZKaTong-M19S"}) plt.bar ([1 font.family': 2 FZKaTong-M19S 3]) plt.title ("Test")

After running, the title Chinese can not be displayed properly, and the debug message shows that the 'FZKaTong-M19S', can not be found, so the default DejaVu Sans font is used.

Findfont: Font family ['FZKaTong-M19S'] not found. Falling back to DejaVu Sans.

Verified by the following code, it is known that the simplified FZKaTong-M19S' cartoon font does not appear in ttflist, so the font cannot be found. Ttflist is built by reading the font cache, so rebuilding the font cache may solve this problem.

From matplotlib.font_manager import fontManagerprint ([i.name for i in fontManager.ttflist if 'FZKaTong-M19S' in i.name])

Solve the problem

The default findfont function is looked up from the font cache, but not in the newly installed font cache, so you need to recreate the cache and load it.

# rebuild the font cache from matplotlib.font_manager import _ rebuild_rebuild () import matplotlib.pyplot as pltplt.xkcd () plt.rcParams.update ({'font.family': "FZKaTong-M19S"}) # plt.rcParams [' font.family'] = 'FZKaTong-M19S'# plt.rc (' font', * * {'family':' FZKaTong-M19S'}) plt.bar ([1J 2M 3], [1J 2M 3]) plt.title ("test") plt.show ()

The above is all the content of this article "how to use Python matplotlib to draw xkcd Animation style Chart". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report