温馨提示×

在Matplotlib中如何设置图表的标题和轴标签

小亿
86
2024-05-20 12:29:31
栏目: 编程语言

要在Matplotlib中设置图表的标题和轴标签,可以使用以下方法:

  1. 设置标题:
plt.title('Title of the plot')
  1. 设置x轴和y轴标签:
plt.xlabel('X-axis label')
plt.ylabel('Y-axis label')

例如,下面是一个完整的示例代码,展示如何设置图表的标题和轴标签:

import matplotlib.pyplot as plt

# 创建数据
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

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

# 设置标题和轴标签
plt.title('Prime numbers')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')

# 显示图表
plt.show()

0