import matplotlib.pyplot as plt
# 示例数据
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [1, 2, 3, 4, 5]
# 创建图形和子图
plt.figure()
# 绘制两条线并添加标签
plt.plot(x, y1, label='Square numbers', color='blue')
plt.plot(x, y2, label='Linear numbers', color='red')
# 添加图例
plt.legend(title='Legend Title')
# 添加标题和标签
plt.title('Python Legend Example')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
# 显示图形
plt.show()
plt.plot()
函数用于绘制线条图,并通过 label
参数为每条线指定标签。plt.legend()
函数用于在图中添加图例,title
参数可以设置图例的标题。plt.title()
, plt.xlabel()
, 和 plt.ylabel()
分别用于设置图表的标题和坐标轴标签。plt.show()
用于显示生成的图表。这个示例展示了如何使用 legend
来标记图表中的不同数据系列。
上一篇:python 死循环
下一篇:python 读取word
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站