1

I have noticed this issue occurring often in several different plots. I set font, linewidth, and marker sizes in the standard ways (examples):

  • plt.title('My Title', fontsize = 12)
  • plt.plot(x, y, linewidth = 2)
  • plt.scatter(x, y, s = 300)
  • plt.xticks(fontsize = 12)

My issue is that font, line, and scatter sizes end up varying wildly between different runs of the program. There is seemingly no rhyme or reason. What causes this? It makes formatting plots very difficult as I never know what to expect from my code.

I am using Spyder with Python 3.8 (Anaconda) on the same computer. I am not switching screen resolutions or using an external monitor.

mfgeng
  • 89
  • 5
  • One thing that can make this _seem_ to happen is if the figure changes size because your backend is displayibg with bbox_inches=“tight” turned on and you have some artists that make the figure larger than it is when you start. – Jody Klymak Aug 12 '21 at 04:16

1 Answers1

0

Well, I see there are two ways for you to simplify your code:

1. Use common idiom of python

design = {
    'fontsize' : 12
}

plt.title('My Title', **design)
plt.xticks(**design)

In case you dont know what and how ** work follow this link: What does ** (double star/asterisk) and * (star/asterisk) do for parameters?

This method works when functions share the same params.

2. Use built-in rcParams of matplotlib lib for setting up default design

params = {
    'axes.labelsize': 6,
    'axes.titlesize':6,
    'xtick.labelsize':6,
    'ytick.labelsize':6,
    'axes.titlepad': 1,
    'axes.labelpad': 1,
    'font.size': 12
}

plt.rcParams.update(params)
plt.title('My Title')
plt.plot(x, y)
plt.scatter(x, y)
Chau Loi
  • 1,106
  • 1
  • 14
  • 36