import matplotlib.pyplot as plt
# 创建一些示例数据
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
# 创建一个简单的折线图
plt.plot(x, y, label='y = x^2')
# 添加标题和标签
plt.title('Simple Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
# 显示图例
plt.legend()
# 显示图形
plt.show()
import matplotlib.pyplot as plt
: 导入 matplotlib
的 pyplot
模块,并命名为 plt
,这是绘制图形的主要接口。x
和 y
: 定义了两个列表,分别表示横坐标和纵坐标的数据点。plt.plot(x, y, label='y = x^2')
: 使用 plot
函数绘制折线图,并通过 label
参数为这条线添加标签。plt.title()
, plt.xlabel()
, plt.ylabel()
: 分别设置图表的标题和坐标轴的标签。plt.legend()
: 显示图例,图例会根据 plot
中的 label
参数自动生成。plt.show()
: 最后调用 show
函数显示绘制好的图形。这个代码片段展示了如何使用 matplotlib
库中的 pyplot
模块来创建一个简单的折线图。
上一篇:python字符串转json
下一篇:python中str什么作用
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站