2

From what I understand of this documentation matplotlib provides 3 ways to change the style parameters for plotting (things like axes.grid). Those ways are: using style sheets to set many parameters at a time; setting specific parameters through matplotlib.rcParams or matplotlib.rc; or using a matplotlibrc file to set defaults.

I would like to understand if all of the parameters are accessible in each of the methods I listed above and where I can find a comprehensive list of all of the parameters.

I have tried to understand this from the linked documentation, but I often fail. A specific example is setting the axis font. I typically use a combination like this:

axis_font = {'fontname':'Arial', 'size':'32'}
ax.set_ylabel('some axis title',**axis_font)

But it is not clear what matplotlib parameter (if any) I have set. Does there exist a parameter for the axis font that could be included in a style file for example?

Other attempts in my code include confusing blocks like:

legend_font = {'fontname':'Arial', 'size':'22'}
#fonts global settings
matplotlib.rc('font',family=legend_font['fontname'])

By the names it seems like it would be changing the legend font, but actually it is clearly setting the parameter for the overall font. And the size is not being used. Are there matplotlib parameters for specifically the legend font and legend size?

The things I've tried are:

  • Checking the example matplotlibrc at the bottom of the linked page (no sign of axis or legend fonts specifically)
  • Printing matplotlib.rcParams (no sign of axis or legend fonts)
  • Checking the axis api (could not match up with example style files e.g. the classic predefined style file has facecolor set, which is mentioned in that page, but it also has edgecolor set which is not mentioned on the page)
William Miller
  • 9,839
  • 3
  • 25
  • 46
villaa
  • 1,043
  • 3
  • 14
  • 32
  • `rcParams` is simply a dictionary of defaults that were deemed useful to allow to be set as defaults. There is no guarantee that any particular method has an entry int he rcParams, and they should not be considered synonymous with the kwargs for various classes or methods. – Jody Klymak Mar 03 '20 at 22:39
  • If you want to change the legend font, suggest you use `ax.legend(prop=fontprop)`, rather than togging the rcParam. Similarly, for axis fonts... – Jody Klymak Mar 03 '20 at 22:42
  • @JodyKlymak Ok. If I understand what you are saying this implies a very simple answer to my direct questions. i.e. No, the axis and legend fonts cannot be set by `rcParams` If this is the answer, please post it. – villaa Mar 03 '20 at 22:44
  • [This page](https://matplotlib.org/3.1.3/tutorials/introductory/customizing.html) and particularly the [sample rc file](https://matplotlib.org/3.1.3/tutorials/introductory/customizing.html#a-sample-matplotlibrc-file) should be helpful. – William Miller Mar 03 '20 at 22:45
  • @WilliamMiller I specifically referenced both of those pages in my answer. No, they did not answer this question for me. – villaa Mar 03 '20 at 22:46
  • All the rcParams are in the file that you linked. – Jody Klymak Mar 03 '20 at 22:48
  • 1
    Then perhaps you aren't looking closely enough, the answers to every part of this question are contained in that page. It is specifically helpful to look at the sample rc file because it contains a comprehensive list of **every** parameter which can be modified using `rcParams`, a `matplotlibrc` file, or a style sheet. And yes, you can modify the same set of parameters with any of the three methods. – William Miller Mar 03 '20 at 22:49
  • You may also find [this answer](https://stackoverflow.com/questions/12444716/how-do-i-set-the-figure-title-and-axes-labels-font-size-in-matplotlib/59169442#59169442) helpful since it addresses how to change the legend font size specifically. – William Miller Mar 03 '20 at 22:52
  • @WilliamMiller Thank you, but I **still** don't see any mention of how to change the font. I see how to change the sizes. Please, make this simpler for me--just tell me the parameter that specifies the font (like Arial) for the axis label. I am reading more carefully now, but you can easily save me time if you have the parameter. – villaa Mar 04 '20 at 01:59

1 Answers1

1

The rcParams property which changes the font is font.family it accepts 'serif', 'sans-serif', 'cursive', 'fantasy', and 'monospace' as outlined in the linked sample matplotlibrc file. If text.usetex is False it also accepts any concrete font name or list of font names - which will be tried in the order they are specified until one works.

This method applies the specified font name to the entire figure (and to all figures when done globally). If you want to modify the font family for an individual Text instance (i.e. an axis label) you can use matplotlib.text.Text.set_family() (which is an alias for matplotlib.text.Text.set_fontfamily())

import matplotlib.pyplot as plt

ylabel = plt.ylabel("Y Label")
ylabel.set_family("DejaVu Serif")
plt.xlabel("X Label")

plt.show()

enter image description here

And to set the font family for just a legend instance you can use plt.setp, e.g.

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

ylabel = plt.ylabel("Y Label")
plt.xlabel("X Label")
ylabel.set_family("DejaVu Serif")

legend = plt.legend(handles = [mpatches.Patch(color='grey', label="Label")])
plt.setp(legend.texts, family="EB Garamond")

plt.show()

enter image description here


Note that this method will not work when text.usetex is True.

William Miller
  • 9,839
  • 3
  • 25
  • 46
  • Thanks, very useful. It seems to me that this implies that the axis and legend font _cannot_ be set individually with a style sheet (for example). You can instead set an overall font. Then you can change things for a specific object like the `ylabel` just before plot time. Similarly can change legend with call to `matplotlib.pyplot.setp` just before plot time. I'm interested to know if there are other aspects of the plot that cannot be set globally (once) with a stylesheet or similar--I enjoy a uniform style for the plots, so I would like to set it once and never change. – villaa Mar 05 '20 at 18:16
  • @villaa You are correct in the conclusion that the font family for individual text instances **cannot** be set with a style sheet, only the global font family. If you want to "*set it once and never change it*" and need different fonts for axis labels, legend, etc then I would suggest writing a function which wraps the individual generation function (e.g. `plt.xlabel()`) and responds to a custom parameter for setting the font family. – William Miller Mar 05 '20 at 23:17
  • Ok, add that to the answer please. I will accept this as the best answer. – villaa Mar 07 '20 at 18:13