Python Matplotlib 绘制折线图:修改坐标轴名称和标题
本文将介绍如何使用 Python Matplotlib 库绘制折线图,并更改 x 轴为'隐私预算',y 轴为'准确度(%)',以及设置图表的标题。
示例代码:
import matplotlib.pyplot as plt
avg_train_accuracy = [10, 55, 88.33, 96.97, 88.33, 96.67, 88.33]
test_accuracy = [12.71, 55.13, 92.32, 91.90, 92.61, 91.90, 92.31]
x = [0.75, 1, 1.2, 1.5, 1.75, 2, 3]
plt.plot(x, avg_train_accuracy, marker='o', label='Avg Train Accuracy')
plt.plot(x, test_accuracy, marker='o', label='Test Accuracy')
plt.xlabel('隐私预算')
plt.ylabel('准确度(%)')
plt.title('准确度 vs 隐私预算')
plt.legend()
plt.grid(True)
plt.show()
代码说明:
plt.xlabel('隐私预算'):将 x 轴标签设置为'隐私预算'。plt.ylabel('准确度(%)'):将 y 轴标签设置为'准确度(%)'。plt.title('准确度 vs 隐私预算'):设置图表的标题为'准确度 vs 隐私预算'。
注意:
此代码示例仅供参考。实际使用时,请根据您的需求进行修改,例如更改数据、添加其他图示元素等。
通过使用 matplotlib.pyplot 库,您可以轻松地绘制折线图,并根据需要修改坐标轴名称和标题。这有助于您更清晰地展示数据分析结果。
原文地址: https://www.cveoy.top/t/topic/ldn 著作权归作者所有。请勿转载和采集!