|
| 1 | +""" |
| 2 | +=========================== |
| 3 | +Configuring the font family |
| 4 | +=========================== |
| 5 | +
|
| 6 | +You can explicitly set which font family is picked up, either by specifying |
| 7 | +family names of fonts installed on user's system, or generic-families |
| 8 | +(e.g., 'serif', 'sans-serif', 'monospace', 'fantasy' or 'cursive'), |
| 9 | +or a combination of both. |
| 10 | +(see :doc:`font tutorial </tutorials/text/text_props>`) |
| 11 | +
|
| 12 | +See `~.font_manager.FontManager.addfont` for adding a font family from a file. |
| 13 | +
|
| 14 | +In the example below, we are overriding the default sans-serif generic family |
| 15 | +to include a specific (Tahoma) font. (Note that the best way to achieve this |
| 16 | +would simply be to prepend 'Tahoma' in 'font.family') |
| 17 | +
|
| 18 | +The default family is set with the font.family rcparam, |
| 19 | +e.g. :: |
| 20 | +
|
| 21 | + rcParams['font.family'] = 'sans-serif' |
| 22 | +
|
| 23 | +and for the font.family you set a list of font styles to try to find |
| 24 | +in order:: |
| 25 | +
|
| 26 | + rcParams['font.sans-serif'] = ['Tahoma', 'DejaVu Sans', |
| 27 | + 'Lucida Grande', 'Verdana'] |
| 28 | +
|
| 29 | +.. redirect-from:: /gallery/font_family_rc_sgskip |
| 30 | +
|
| 31 | +
|
| 32 | +
|
| 33 | +The font font.family defaults are OS dependent and can be viewed with |
| 34 | +""" |
| 35 | +import matplotlib.pyplot as plt |
| 36 | + |
| 37 | +print(plt.rcParams["font.sans-serif"][0]) |
| 38 | +print(plt.rcParams["font.monospace"][0]) |
| 39 | + |
| 40 | + |
| 41 | +################################################################# |
| 42 | +# Choose default sans-serif font |
| 43 | + |
| 44 | +def print_text(text): |
| 45 | + fig, ax = plt.subplots(figsize=(6, 1), facecolor="#eefade") |
| 46 | + ax.text(0.5, 0.5, text, ha='center', va='center', size=40) |
| 47 | + ax.axis("off") |
| 48 | + plt.show() |
| 49 | + |
| 50 | + |
| 51 | +plt.rcParams["font.family"] = "sans-serif" |
| 52 | +print_text("Hello World! 01") |
| 53 | + |
| 54 | + |
| 55 | +################################################################# |
| 56 | +# Choose sans-serif font and specify to it to "Nimbus Sans" |
| 57 | + |
| 58 | +plt.rcParams["font.family"] = "sans-serif" |
| 59 | +plt.rcParams["font.sans-serif"] = ["Nimbus Sans"] |
| 60 | +print_text("Hello World! 02") |
| 61 | + |
| 62 | + |
| 63 | +################################################################## |
| 64 | +# Choose default monospace font |
| 65 | + |
| 66 | +plt.rcParams["font.family"] = "monospace" |
| 67 | +print_text("Hello World! 03") |
| 68 | + |
| 69 | + |
| 70 | +################################################################### |
| 71 | +# Choose monospace font and specify to it to "FreeMono" |
| 72 | + |
| 73 | +plt.rcParams["font.family"] = "monospace" |
| 74 | +plt.rcParams["font.monospace"] = ["FreeMono"] |
| 75 | +print_text("Hello World! 04") |
0 commit comments