Changing the default font in matplotlib to "Times New Roman".

In summary, you need to add the code below to your Python script to make the "Times New Roman" font the default:
  • #1
amjad-sh
246
13
I want to submit an article to an aps journal, so the labels of the figures have the "Times New Roman" font.
I had used the package matplotlib to get the figures and I want to make "Times New Roman" the default font. So, what code I need to write for this to be achieved?
Note that I used the package subplots to get the figures, by implementing this code:
from matplotlib.pyplot import subplots, show

I also tried the code below to check if the font is available:

import matplotlib.font_manager
flist = matplotlib.font_manager.get_fontconfig_fonts()
names = [matplotlib.font_manager.FontProperties(fname=fname).get_name() for fname in flist]
print (names)
if "Times New Roman" in names:
print ("Yes")
else:
print ("font not available")

and the output was "font not available". Does this mean I need to add it to matplotlib? and if yes, how?
 
Technology news on Phys.org
  • #2
amjad-sh said:
and the output was "font not available". Does this mean I need to add it to matplotlib? and if yes, how?
Might it be called just "Times" in the package?
 
  • Like
Likes amjad-sh and jedishrfu
  • #4
anorlunda said:
Might it be called just "Times" in the package?
I think the last code I wrote is meaningless, because I tried @jedishrfu's link and could change the default font into Times New Roman. But a problem still remaing. The part of the label which is written in mathmode always appears bold:
r"\mathcal{L}^s$"
I want the boldness of the text written in mathmode and the rest of the text be the same ( I want the font to be light). What is needed to be done for this to be achieved?
 
  • #5
amjad-sh said:
I want the boldness of the text written in mathmode and the rest of the text be the same

Matplotlib does have a fontweight (or just weight) property that can be applied to most text.
 
  • #6
websterling said:
Matplotlib does have a fontweight (or just weight) property that can be applied to most text.
I used this code to make the weight of the labels appears light:
matplotlib.rcParams['axes.labelweight']=100,
but the the mathematical symbols written in the mathmode still look bold.
 
  • #7
Isn't this bold issue a nit? It may be that a different font is required for the math symbols to be properly displayed.

You could generate a plot and send it to the publishing journal editor to see if it is acceptable or if they have any guidelines on how best to make a plot. Their rules might only apply to the article and not the plot images. They may also have some recommended font packages to use as well.

Do you have other more pressing things to work on to complete your project? or is this the last thing? I'm asking because sometimes we focus on a minor issue to the detriment of other things we need to solve. I've done this too and missed deadlines which teach you to stay focused on the end goal.
 
  • Like
Likes amjad-sh

1. How do I change the default font in matplotlib to "Times New Roman"?

To change the default font in matplotlib to "Times New Roman", you can use the rcParams function to specify the font family and font type. First, import the matplotlib library and then use the plt.rc function to set the font family to "Times New Roman" and the font type to serif. This will change the default font for all plots in your script.

2. Can I change the default font in matplotlib for a specific plot only?

Yes, you can change the default font for a specific plot by using the fontname argument in the plt.plot() function. This will override the default font set in the rcParams and apply the specified font only to that particular plot.

3. How can I check which font is currently set as the default in matplotlib?

You can use the matplotlib.rcParams["font.family"] function to check which font is currently set as the default in matplotlib. This will return a string value of the font family, which you can compare with the desired font family.

4. Can I change the default font size in addition to the font family?

Yes, you can change the default font size by using the matplotlib.rcParams["font.size"] function and specifying the desired font size. This will change the font size for all plots in your script.

5. Is it possible to change the default font in matplotlib to a non-system font?

Yes, it is possible to change the default font in matplotlib to a non-system font. First, you need to download the font file and save it in your working directory. Then, use the fontpath argument in the plt.rc function to specify the file path of the font. This will change the default font to the specified font file.

Similar threads

  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
15
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
404
  • Programming and Computer Science
Replies
14
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
9K
  • Computing and Technology
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top