温馨提示×

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

小亿
145
2023-12-20 22:10:29
栏目: 编程语言

要在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模块来设置标题和标签等属性。

0