从Datafram打印Matplotlib标题

2024-03-29 06:28:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要打印一个标题为几个绘图在一个循环字符串值从一个数据帧。我已经成功地创建了一个循环来绘制图表,但是我对所说的标题有困难,这是必要的。你知道吗

这是我使用的代码:

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'])

所以它为我的每个图表打印正确的标题,应该是这样的:

DEPARTMENT XXX MONTH X YEAR XXXX

谢谢你的帮助。你知道吗


Tags: in标题title图表pltresultyearlength
1条回答
网友
1楼 · 发布于 2024-03-29 06:28:59

明白了!你知道吗

#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()

相关问题 更多 >