扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

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

扬州沐宇科技
2024-05-20 12:29:31
matplotlib

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

扫码添加客服微信