我想有一个情节,其中字体是"计算机现代"(即latex 风格),但与x-ticks和y-ticks在粗体.

由于matplotlib最近的升级,我之前的过程不再有效.

这是我的老程序:

plt.rc('font', family='serif',size=24)
matplotlib.rc('text', usetex=True)
matplotlib.rc('legend', fontsize=24) 
matplotlib.rcParams['text.latex.preamble'] = [r'\boldmath']

这是输出消息:

test_font.py:26: MatplotlibDeprecationWarning: Support for setting an rcParam that expects a str value to a non-str value is deprecated since 3.5 and support will be removed two minor releases later.
  matplotlib.rcParams['text.latex.preamble'] = [r'\boldmath']

我已经决定一个可能的解决方案是使用"现代电脑"作为字体.这是我的例子:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np


font = {'family' : 'serif',
        'weight' : 'bold',
        'size'   : 12
        }

matplotlib.rc('font', **font)


# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

fig, ax = plt.subplots(1,figsize=(9,6))

ax.plot(t, s)

ax.set(xlabel='time (s)  $a_1$', ylabel='voltage (mV)',
       title='About as simple as it gets, folks')
ax.grid()

fig.savefig("test.png")
plt.show()

结果是:

enter image description here

然而,我无法设置字体样式.

我try 将字体系列设置为"cmr10".这是代码:

font = {'family' : 'serif',
         'weight' : 'bold',
         'size'   : 12,
         'serif':  'cmr10'
         }

matplotlib.rc('font', **font)

似乎"cmr10"让消失成为了一个大胆的 Select .

谢谢

推荐答案

您仍然可以使用旧的程序,但只需稍作更改.得到的MatplotlibDeprecationWarning表示参数期望值为str,但得到的是其他值.在本例中,发生的情况是,您将其作为list传递.拆下支架可以实现以下目的:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

plt.rc('font', family='serif',size=24)
matplotlib.rc('text', usetex=True)
matplotlib.rc('legend', fontsize=24)
matplotlib.rcParams['text.latex.preamble'] = r'\boldmath'


# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

fig, ax = plt.subplots(1,figsize=(9,6))

ax.plot(t, s)

ax.set(xlabel='time (s)  $a_1$', ylabel='voltage (mV)',
       title='About as simple as it gets, folks')
ax.grid()

fig.savefig("test.png")
plt.show()

上面的代码生成的绘图没有任何错误:

enter image description here

Python相关问答推荐

通过优化空间在Python中的饼图中添加标签

@Property方法上的inspect.getmembers出现意外行为,引发异常

为什么以这种方式调用pd.ExcelWriter会创建无效的文件格式或扩展名?

如何将一个动态分配的C数组转换为Numpy数组,并在C扩展模块中返回给Python

如何让这个星型模式在Python中只使用一个for循环?

在含噪声的3D点网格中识别4连通点模式

创建可序列化数据模型的最佳方法

Python导入某些库时非法指令(核心转储)(beautifulsoup4."" yfinance)

如何合并两个列表,并获得每个索引值最高的列表名称?

如何在FastAPI中为我上传的json文件提供索引ID?

与命令行相比,相同的Python代码在Companyter Notebook中运行速度慢20倍

在numpy数组中寻找楼梯状 struct

在极点中读取、扫描和接收有什么不同?

如何使用Azure Function将xlsb转换为xlsx?

Pandas 数据帧中的枚举,不能在枚举列上执行GROUP BY吗?

有没有办法在不先将文件写入内存的情况下做到这一点?

当输入是字典时,`pandas. concat`如何工作?

极点:在固定点扩展窗口

用LAKEF划分实木地板AWS Wrangler

正则表达式反向查找