Python Matplotlib中标题的格式
我有以下python代码:Python Matplotlib中标题的格式,python,matplotlib,title,Python,Matplotlib,Title,我有以下python代码: def plot_only_rel(): filenames = find_csv_filenames(path) for name in filenames: sep_names = name.split('_') Name = 'Name='+sep_names[0] Test = 'Test='+sep_names[2] Date = 'Date='+str(sep_names[5]
def plot_only_rel():
filenames = find_csv_filenames(path)
for name in filenames:
sep_names = name.split('_')
Name = 'Name='+sep_names[0]
Test = 'Test='+sep_names[2]
Date = 'Date='+str(sep_names[5])+' '+str(sep_names[4])+' '+str(sep_names[3])
plt.figure()
plt.plot(atb_mat_2)
plt.title((Name, Test, Date))
然而,当我在我的图形上打印标题时,它会以
(u'Name=X',u'Test=Ground',Date=8.3 2012')
我有以下问题:
为什么我会得到“u”?我如何去掉括号和引号?
当我使用suptitle时也会发生这种情况
谢谢你的帮助 那么:
plt.title(', '.join(Name,Test,Date))
由于标题是作为数组提供的,因此它显示了数组的表示形式(实际上是元组)。
u
告诉您它是一个unicode
字符串
您还可以使用format更好地指定格式:
plt.title('{0}, {1}, {2}'.format(Name, Test, Date))
plt.title
接收一个字符串作为参数,然后传入一个元组(名称、测试、日期)
。因为它需要一个字符串,所以它尝试使用元组的\uuuu str\uuuu
方法将其转换为字符串,该方法为您提供了获得的输出。您可能希望执行以下操作:
plat.title('{0} {1}, {2}'.format(Name, Test, Date))
在Python>3.6中,您甚至可以使用f-string来简化格式设置:
plt.title(f'{Name}, {Test}, {Date}')