Matplotlib text will not display in xkcd font

When using xkcd () with matplotpib, none of the fonts appear in a regular comic font. Has something changed or am I doing something wrong?

x = df['Time'] y = df['Adjustment'] fig = plt.figure() ax = fig.add_subplot(1,1,1) ax1 = fig.add_subplot(1,1,1) ax1.plot(x,y) ax1.xaxis.set_visible(False) ax1.yaxis.set_visible(False) plt.axvline(x=2.3, color='k', ls='dashed') plt.axvline(x=6, color='k', ls='dashed') ax.text(4,4,'Culture Shock', size=16) plt.title('Test title') plt.xkcd() plt.show() 

Thanks for any help.

I must clarify that the graph will be displayed in xkcd style, and not in any font. It prints something similar to Times New Roman.

+3
source share
1 answer

As shown, you need to put plt.xkcd() at the beginning of the code in front of all build commands. Thus:

 from matplotlib import pyplot as plt import numpy as np x = np.arange(10) y = np.sin(x) plt.xkcd() fig = plt.figure() ax = fig.add_subplot(1,1,1) ax1 = fig.add_subplot(1,1,1) ax1.plot(x,y) ax1.xaxis.set_visible(False) ax1.yaxis.set_visible(False) plt.axvline(x=2.3, color='k', ls='dashed') plt.axvline(x=6, color='k', ls='dashed') ax.text(4,4,'Culture Shock', size=16) plt.title('Test title') plt.show() 

which leads to this figure for me:

enter image description here

As you can see, there are shaky lines, but the font is incorrect, simply because it is not available on my Linux machine (I also get a warning about this on the command line). Putting plt.xkcd() at the end of the code leads to a simple matplotlib drawing without shaky lines.

Here is a brief description of what pyplot.xkcd() does under the hood; it just sets a lot of resource parameters:

 rcParams['font.family'] = ['Humor Sans', 'Comic Sans MS'] rcParams['font.size'] = 14.0 rcParams['path.sketch'] = (scale, length, randomness) rcParams['path.effects'] = [ patheffects.withStroke(linewidth=4, foreground="w")] rcParams['axes.linewidth'] = 1.5 rcParams['lines.linewidth'] = 2.0 rcParams['figure.facecolor'] = 'white' rcParams['grid.linewidth'] = 0.0 rcParams['axes.unicode_minus'] = False rcParams['axes.color_cycle'] = ['b', 'r', 'c', 'm'] rcParams['xtick.major.size'] = 8 rcParams['xtick.major.width'] = 3 rcParams['ytick.major.size'] = 8 rcParams['ytick.major.width'] = 3 
+3
source

Source: https://habr.com/ru/post/1203814/


All Articles