In matplotlib.pyplot I frequently need to change the font size for 4 different text elements within a given axes:
- axis labels
- major tick labels
- legend text
- axes title
This is how I currently do it:
import matplotlib.pyplot as plt
plt.plot([1,2,3], label = 'Whatever')
plt.xlabel('xaxis', fontsize = 16)
plt.ylabel('yaxis', fontsize = 20)
plt.legend(fontsize = 18)
plt.xticks(fontsize = 20)
plt.yticks(fontsize = 20)
plt.title('PLOT', fontsize = 20)
As you can see that is quite a few lines of code, especially considering usually I need for these all to be the same font size (though this is not reflected in this example). Also these lines can be scattered throughout my script and difficult to find if I need to reset the font. I tend to think of the formatting elements like grid, font, color and legend as separate from the actual data processing/plotting. I therefore have two questions:
- Is there a way to use pyplot to get all these elements then set their fontsize all in one go?
- Is there a way to set the fontsize of these objects (especially the x and y labels and the title, though preferably all) after plotting ?