Matplotlib 发出绘图命令时出现错误(异常),我可以采取什么必要的措施?

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

绘制图形时发生错误:用序列设置数组元素。


import matplotlib.pyplot as plt 

try:
    # Plot accuracy graph
    plt.figure(figsize=(10, 6))
    plt.plot(["Teacher", "Assistant teacher", "Distilled student", "Student trained from scratch"],
             [teacher_results, assistant_teacher_results, student_results, student_scratch_results],
             marker='o')

   plt.title('Accuracy Comparison')
    plt.xlabel('Model')
    plt.ylabel('Accuracy')
    plt.ylim([0, 1])
    plt.grid(True)
    plt.show()

except Exception as e:
    print("An error occurred while plotting the graph:", e)
python matplotlib plot
1个回答
0
投票

要解决此问题,请确保 Teacher_results、assistant_teacher_results、student_results 和 Student_scratch_results 是数组或数字列表。如果它们是列表的列表,您可能需要将它们展平或相应地重组您的数据。

这是更正后的代码:

import matplotlib.pyplot as plt

try:
    # Plot accuracy graph
    plt.figure(figsize=(10, 6))
    plt.plot(["Teacher", "Assistant teacher", "Distilled student", "Student trained from scratch"],
             [teacher_results, assistant_teacher_results, student_results, student_scratch_results],
             marker='o')
    plt.title('Accuracy Comparison')
    plt.xlabel('Model')
    plt.ylabel('Accuracy')
    plt.ylim([0, 1])
    plt.grid(True)
    plt.show()
except Exception as e:
    print("An error occurred while plotting the graph:", e)
© www.soinside.com 2019 - 2024. All rights reserved.