我需要使用数据帧中的字符串值在循环中为几个图打印标题。我已经设法创建了一个循环来绘制图形,但是我对标题(必不可少)有麻烦。
这是我使用的代码:
for i in range (len(resultXDataFrame.index)):
selected_row = i
selected_result = result.iloc[selected_row, 17:]
selected_etalon = etalon.iloc[selected_row, 17:]
plt.figure()
plt.title(result.loc[selected_row, 'DEPARTMENT'], 'MONTH ', result.loc[selected_row, 'M'], 'YEAR', result.loc[selected_row, 'Y'])
x_axis = np.arange(0, predict_length, 1)
plt.plot(x_axis, selected_result, color = 'black')
plt.plot(x_axis, selected_etalon, color = 'red')
plt.xlim(0, predict_length)
plt.xlabel('Prediction length in days')
plt.ylabel('Predicted Illness')
plt.show()
但是我收到一条错误消息:
TypeError: title() takes from 1 to 4 positional arguments but 5 were given
问题肯定是在我尝试定义标题的那一行上。当我选择固定标题时,它就可以正常工作。我只需要合并以下部分:
plt.title(result.loc[selected_row, 'DEPARTMENT'], 'MONTH ', result.loc[selected_row, 'M'], 'YEAR', result.loc[selected_row, 'Y'])
因此它为我的每个图形打印正确的标题,看起来应该像这样:
部门XXX月X年XXXX
谢谢您的帮助。
答案 0 :(得分:0)
知道了!
#plot results for selected department
for i in range (len(resultXDataFrame.index)):
selected_row = i
selected_result = result.iloc[selected_row, 17:]
selected_etalon = etalon.iloc[selected_row, 17:]
plt.figure()
plt.title(str(result.loc[selected_row, 'DEPARTMENT']) + ' MONTH ' + str(result.loc[selected_row, 'M']) + ' YEAR ' + str(result.loc[selected_row, 'Y']))
x_axis = np.arange(0, predict_length, 1)
plt.plot(x_axis, selected_result, color = 'black')
plt.plot(x_axis, selected_etalon, color = 'red')
plt.xlim(0, predict_length)
plt.xlabel('Prediction length in days')
plt.ylabel('Predicted Illness')
plt.show()