matplotlib 标签格式化字体已更改

matplotlib label formatting font changed

我使用 matplotlib 生成数据分析图,然后在会议、出版物等中向人们展示。我通常存储生成每个 interesting/useful 图的所有数据和脚本,只是以防我需要在创建图表后的某个时候更新图表。这是我的 box-standard 一段代码的示例,它生成了下面的图:

# In[Imports]:
import pandas, matplotlib, matplotlib.pyplot

# In[Plot formatting]:
ticksFontSize = 18
labelsFontSize = 30
legendFontSize = 16
cm=matplotlib.pyplot.cm.get_cmap('viridis')
matplotlib.rc('xtick', labelsize=ticksFontSize)
matplotlib.rc('ytick', labelsize=ticksFontSize)

# In[Read the data]:
# CDF of TLE update frequenies
dfGood=pandas.read_csv('blue.csv',sep=',',header=None)
epochsGood=dfGood[0].values
cdfsGood=dfGood[1].values

# In[Plot the data]:
fig,ax=matplotlib.pyplot.subplots(1,1,sharex=True,figsize=(14,8))
ax.scatter(epochsGood,cdfsGood,c='indigo',marker='o',lw=0,s=30)
ax.set_xlabel(r"$TLE\ update\ frequency\ (orbital\ periods)$",
    size=labelsFontSize)
ax.set_ylabel(r"$Cumulative\ Distribution\ Function\ (-)$",
    fontsize=labelsFontSize)
ax.grid(linewidth=1)    
ax.tick_params(axis='both',reset=False,which='both',
    length=5,width=1.5)
ax.tick_params(axis='x', which='major', pad=15)
ax.set_xlim(0,5)
ax.set_ylim(0,1.1)
matplotlib.pyplot.subplots_adjust(left=0.1,right=0.95,
    bottom=0.15,top=0.9)
fig.show()

最近(在过去几个月内,但这可能是一个迟来的更新...),我更新了 matplotlib 并且上面的脚本开始生成不同格式的图:

剪辑的 Y-axis 没什么大不了的,我可以接受并调整 plot/axis。我有点困惑的是更改后的字体。似乎 matplotlib 更新期间 matplotlibrc 文件已更改。我可以接受并在我的脚本中显式设置 matplotlibrc 属性,或在 per-label 基础上设置标签文本属性,如 this answer 所示。但我不知道如何返回到 以前的格式 ,即设置什么文本属性。有什么想法吗?

一些有用的信息

  1. 当前 Python 版本 = 3.5.4
  2. Python 版本我用来生成 "old plot" = 3.5.something
  3. 当前 matplotlib 版本 = 2.2.2
  4. matplotlib 版本我用来生成 "old plot" = 我希望我知道...

版本 2.x from "Computer Modern" to "DejaVu Sans" 中的默认数学字体已更改。您可以按照文档中所述在脚本中将其更改回以前的版本:

from matplotlib import pyplot as plt

plt.rcParams['mathtext.fontset'] = 'cm'
plt.rcParams['mathtext.rm'] = 'serif'

示例输出(没有您的数据,因为您的问题不包含 MCVE):