温馨提示×

jupyter绘制折线图的代码怎么写

小亿
273
2023-12-20 22:10:29
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

要在Jupyter中绘制折线图,可以使用Python的数据可视化库,例如matplotlib或seaborn。

下面是使用matplotlib库绘制折线图的示例代码:

import matplotlib.pyplot as plt

# 创建数据
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

# 绘制折线图
plt.plot(x, y)

# 添加标题和标签
plt.title("Line Chart")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")

# 显示图形
plt.show()

如果需要在Jupyter中生成内联图形,可以在代码的开头添加%matplotlib inline

使用seaborn库绘制折线图也十分简单,下面是一个示例代码:

import seaborn as sns

# 创建数据
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

# 绘制折线图
sns.lineplot(x, y)

# 添加标题和标签
plt.title("Line Chart")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")

# 显示图形
plt.show()

注意,在使用seaborn绘制折线图时,需要使用matplotlib库的pyplot模块来设置标题和标签等属性。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:python绘制折线图代码怎么写

0