3

I am looking to increment the plot title font size, by bumping it from e.g. medium to large.

My plots are getting their base font size as well as modifiers from a matplotlibrc .conf file. This is commonly expressed like so:

font.size : 8
axes.titlesize : medium

Now, for one of my plots, I would like to increment the title font size, specifically. Were this a number (as it is for e.g. the linewidth), I could do something like:

from matplotlib import rcParams
newsize = rcParams['lines.linewidth']*2

But this won't work as easily as that. I could, of course, use a dictionary or a series of elif statements - but I am thinking matplotlib, knowing how to interpret these strings, perhaps also has some function to increment them. Do you know of such a thing?

EDIT: Someone has suggested this would be a duplicate of some other question. This question specifically inquires about incrementing the "string font size" (as I have written in the title of the question). This means that setting the font size manually to some integer is not an option (unless there is a way to convert matplotlibrc's small or medium to integers - which would also be unique to this question).

TheChymera
  • 17,004
  • 14
  • 56
  • 86
  • 2
    What exactly are you trying to do? You say what you can do. You hint at how you would solve the problem, but you do not clearly state the problem itself. Do you want to increment the title size by a factor? Do you want to add a constant to it? Do you want to select the next step on the ladder of relative font sizes? If you think you can solve the problem with `elif` maybe show a [mcve] of the code such that the actual problem becomes clear and one can then find a more efficient solution. – ImportanceOfBeingErnest Dec 12 '17 at 23:29
  • Possible duplicate of [How do I set the figure title and axes labels font size in Matplotlib?](https://stackoverflow.com/questions/12444716/how-do-i-set-the-figure-title-and-axes-labels-font-size-in-matplotlib) – Dewald Abrie Dec 13 '17 at 01:57
  • I don't think it's a duplicate. But as said, the question is unclear. The result is the same: You won't get an answer. Any chance that the question is edited in a useful way (not defending against some dup, but constructively giving a clear problem)? – ImportanceOfBeingErnest Dec 13 '17 at 08:50
  • I cannot imagine in what simpler terms you would like to read this. If the plots are being created with e.g. `axes.titlesize : medium`, I would like to increment the size to the next bigger option, in that case, `axes.titlesize : large`. – TheChymera Dec 13 '17 at 15:42

1 Answers1

4

So the question seems to be: Given an "axes.titlesize" rcParam, how to set the title size to the next higher relative size specification.

The solution will surely depend on what values there might be in "axes.titlesize". If for example we already know that those values can only be one of 'xx-small','x-small','small','medium','large','x-large', the solution would be rather easy. One would just take the next possible item in a list of all items.

import matplotlib.pyplot as plt
sizes = ['xx-small','x-small','small','medium','large','x-large','xx-large']
plt.rcParams["axes.titlesize"] = sizes[sizes.index(plt.rcParams["axes.titlesize"])+1]

The generic general case, where "axes.titlesize" can be a number or any valid size string would be handled via the following:

import matplotlib.pyplot as plt
sizes = ['xx-small','x-small','small','medium','large','x-large','xx-large']
cs = plt.rcParams["axes.titlesize"]
try:
    plt.rcParams["axes.titlesize"] = float(cs)*1.2
except:
    if cs in ["larger", "smaller"]:
        cs = cs[:5]
    if cs in sizes[:-1]:    
        plt.rcParams["axes.titlesize"] = sizes[sizes.index(cs)+1]
    elif cs == 'xx-large':
        plt.rcParams["axes.titlesize"] =  plt.rcParams["font.size"]*1.2**4
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Thank you for your answer! Do you know for a fact that the numerical equivalent of a 1-step increment in the string size specification corresponds to 20% in the float size specification? If so, shouldn't the next bigger size after `xx-large` be `1.2**4`? – TheChymera Dec 14 '17 at 17:01
  • 1
    Yes, the step size is a factor `1.2`. You are right that to get the next step from `xx-large` should be `1.2**4`. – ImportanceOfBeingErnest Dec 14 '17 at 19:38