【问题标题】:matplotlib label formatting font changedmatplotlib 标签格式化字体已更改
【发布时间】:2018-09-08 07:45:00
【问题描述】:

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

# 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 轴没什么大不了的,我可以忍受它并调整绘图/轴。我有点困惑的是更改的字体。似乎在 matplotlib 更新期间的某个时间,matplotlibrc 文件已更改。我可以忍受这一点,并在我的脚本中明确设置matplotlibrc 属性,或者在每个标签的基础上设置标签文本属性,如this answer 所示。但我不知道如何返回到以前的格式,即要设置哪些文本属性。有什么想法吗?

一些有用的信息

  1. 当前 Python 版本 = 3.5.4
  2. 我用来生成“旧图”的 Python 版本 = 3.5.something
  3. 当前matplotlib 版本 = 2.2.2
  4. matplotlib 我用来生成“旧情节”的版本 = 我希望我知道...

【问题讨论】:

    标签: python python-3.x matplotlib text string-formatting


    【解决方案1】:

    默认数学字体在 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):

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      • 2020-02-03
      • 1970-01-01
      • 2020-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多