温馨提示×

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

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

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

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

推荐阅读:Matplotlib中怎么设置图表标题、坐标轴标签和刻度

0