Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

python legend

作者:惨美歌   发布日期:2025-03-02   浏览:78

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

解释说明:

  • matplotlib.pyplot 是一个常用的绘图库,用于生成图表。
  • plt.plot() 函数用于绘制线条图,并通过 label 参数为每条线指定标签。
  • plt.legend() 函数用于在图中添加图例,title 参数可以设置图例的标题。
  • plt.title(), plt.xlabel(), 和 plt.ylabel() 分别用于设置图表的标题和坐标轴标签。
  • 最后,plt.show() 用于显示生成的图表。

这个示例展示了如何使用 legend 来标记图表中的不同数据系列。

上一篇:python 死循环

下一篇:python 读取word

大家都在看

python时间格式

python ord和chr

python中的yield

python自定义异常

python list.pop

python的for i in range

npm config set python

python代码简单

python读取文件夹

python中turtle

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站