注释图形时,在python终端中将文本打印为粗体和下划线:RuntimeWarning

问题描述 投票:1回答:1

我有下面的代码:

fig, ax = plt.subplots(1, 2, figsize = (8, 8))
ax[0].set_title('Deaths from Cancer in Michigan 2020',pad=13,fontweight='bold',color='navy')
ax[1].set_title('Deaths from COVID-19 in Michigan 2020',pad=13,fontweight='bold',color='navy')
ax[0].set_ylabel('Number of cases',labelpad=6,color='brown')
ax[1].set_ylabel('Number of cases',labelpad=6,color='brown')
ax[0].set_xlabel('Month',labelpad=6,color='brown')
ax[1].set_xlabel('Month',labelpad=6,color='brown')
ax[0].plot(COVID19['month'], COVID19['Cases'], color='green', marker='o', linestyle='dashed',linewidth=2, markersize=12)
ax[1].plot(Deaths['month'], Deaths['Cases'], color='red', marker='o', linestyle='dashed',linewidth=2, markersize=12)
ax[0].tick_params(axis='x', labelsize=10,rotation=45)
ax[1].tick_params(axis='x', labelsize=10,rotation=45)
ax[0].tick_params(axis='y', labelsize=10)
ax[1].tick_params(axis='y', labelsize=10)
Deaths = Deaths['Cases'].reset_index()
Deaths = Deaths.drop(['index'],axis=1)
fig.subplots_adjust(bottom=0.2)
Correlation = Deaths['Cases'].corr(COVID19['Cases'],method='pearson')

这给了我下面的输出:

enter image description here

然后我尝试注释该图:

class color:
       PURPLE = '\033[95m'
       CYAN = '\033[96m'
       DARKCYAN = '\033[36m'
       BLUE = '\033[94m'
       GREEN = '\033[92m'
       YELLOW = '\033[93m'
       RED = '\033[91m'
       BOLD = '\033[1m'
       UNDERLINE = '\033[4m'
       END = '\033[0;0m'
fig.text(0.3,0.05, (color.BOLD + "Pearson's Correlation Coefficient is: " + color.END + str(Correlation)))

但是,出现错误:RuntimeWarning:当前字体中缺少字形27。 font.set_text(s,0.0,flags = flags)

当我尝试以下操作时:

class color:
       PURPLE = '\033[95m'
       CYAN = '\033[96m'
       DARKCYAN = '\033[36m'
       BLUE = '\033[94m'
       GREEN = '\033[92m'
       YELLOW = '\033[93m'
       RED = '\033[91m'
       BOLD = '\033[1m'
       UNDERLINE = '\033[4m'
       END = '\033[0;0m'
print(color.BOLD + color.UNDERLINE + "Pearson's Correlation Coefficient is: " + color.END)

这给了我:

enter image description here

因此,我能够将文本加粗并加下划线

但是,在上述命令的上下文中,我无法执行此操作(用此字符串注释该图)。

有人可以帮我吗?

python pandas class matplotlib annotations
1个回答
0
投票

在此处安装MacTex软件包:https://www.tug.org/mactex/(我使用的是Mac)。

一旦我在计算机上安装了该软件包,就不需要更改python解释器上的任何设置-我就照原样保留它了

然后使用以下命令:

rc(‘text’,usetex=True)
fig.text(0.3,0.05, r"$\bf\underline{Pearsons \ Correlation \ Coefficient \ is:}$" + str(Correlation))

这将在文本下划线并加粗显示“ Pearsons相关系数为:”。

在单词之间的两侧添加一个'\'空格将在单词之间添加一个空格。

© www.soinside.com 2019 - 2024. All rights reserved.