03_设置轴标签和范围、轴的标签(Labels on Axes)、定义轴的范围、使用linspace定义X值 (“linspace“ to Define X Values)

本文介绍了如何在Matplotlib中设置轴标签和范围,包括添加轴标签、调整轴限以及使用numpy的linspace函数创建等间距点。通过实例展示了如何为图表添加专业外观,以及如何根据需求定制轴的显示效果。
摘要由CSDN通过智能技术生成

3.设置轴标签和范围
3.1.轴的标签(Labels on Axes)
3.2.定义轴的范围
3.3.使用linspace定义X值 (“linspace” to Define X Values)

3.设置轴标签和范围

3.1.轴的标签(Labels on Axes)

我们可以向轴添加标签来改善图形的外观。 这可以通过pyplot的ylabel和xlabel函数来完成。

import matplotlib.pyplot as plt
days = list(range(1, 9))
print(days)
'''
输出结果: [1, 2, 3, 4, 5, 6, 7, 8]
'''
celsius_values = [25.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1]
plt.plot(days, celsius_values)
plt.xlabel('Day')
plt.ylabel('Degrees Celsius')
plt.show()

在这里插入图片描述

可以在绘图函数plot中指定任意数量的x,y,fmt组。 在以下示例中,我们使用两个不同的y值列表:

import matplotlib.pyplot as plt
days = list(range(1, 9))
print(days)
'''
输出结果: [1, 2, 3, 4, 5, 6, 7, 8]
'''
celsius_min = [19.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1]
celsius_max = [24.8, 28.9, 31.3, 33.0, 34.9, 35.6, 38.4, 39.2]
plt.xlabel('Day')
plt.ylabel('Degrees Celsius')
plt.plot(days, celsius_min, "oy",         # o表示离散的
          days, celsius_max, "or")
plt.show()

在这里插入图片描述

再如:

import matplotlib.pyplot as plt
days = list(range(1, 9))
print(days)
'''
输出结果: [1, 2, 3, 4, 5, 6, 7, 8]
'''
celsius_min = [19.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1]
celsius_max = [24.8, 28.9, 31.3, 33.0, 34.9, 35.6, 38.4, 39.2]
plt.xlabel('Day')
plt.ylabel('Degrees Celsius')
plt.plot(days, celsius_min,
         days, celsius_min, "oy",
         days, celsius_max,
         days, celsius_max, "or")
plt.show()

在这里插入图片描述

3.2.定义轴的范围

我们还可以使用函数axis查看和定义轴的范围。 如果不带参数调用它,则返回当前轴的limits:

import matplotlib.pyplot as plt
days = list(range(1, 9))
print(days)
'''
输出结果: [1, 2, 3, 4, 5, 6, 7, 8]
'''
celsius_min = [19.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1]
celsius_max = [24.8, 28.9, 31.3, 33.0, 34.9, 35.6, 38.4, 39.2]
plt.xlabel('Day')
plt.ylabel('Degrees Celsius')
plt.plot(days, celsius_min,
         days, celsius_min, "oy",
         days, celsius_max,
         days, celsius_max, "or")
print("The current limits for the axes are:")
print(plt.axis())
print("We set axes to the following values:")
xmin, xmax, ymin, ymax = 0, 10, 14, 45
print(xmin, xmax, ymin, ymax)
plt.axis([xmin, xmax, ymin, ymax])
plt.show()
"""
总的输出结果:
[1, 2, 3, 4, 5, 6, 7, 8]
The current limits for the axes are:
(0.6499999999999999, 8.35, 18.62, 40.18)
We set axes to the following values:
0 10 14 45
"""

在这里插入图片描述

3.3.使用linspace定义X值 (“linspace” to Define X Values)

我们在以下示例中使用Numpy函数linspace。 linspace可用于在指定间隔内创建均匀分布的数字。

import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(0, 2 * np.pi, 50, endpoint=True)
F = np.sin(X)
plt.plot(X,F)
startx, endx = -0.1, 2*np.pi + 0.1
starty, endy = -1.1, 1.1
plt.axis([startx, endx, starty, endy])
plt.show()

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(-2 * np.pi, 2 * np.pi, 50, endpoint=True)
F1 = 3 * np.sin(X)
F2 = np.sin(2*X)
F3 = 0.3 * np.sin(X)
startx, endx = -2 * np.pi - 0.1 , 2* np.pi + 0.1
starty, endy = -3.1, 3.1
plt.axis([startx, endx, starty, endy])
plt.plot(X, F1)
plt.plot(X, F2)
plt.plot(X, F3)
plt.show()

在这里插入图片描述

下一个例子中将在上图的基础上添加两个具有离散点的图:

import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(-2 * np.pi, 2 * np.pi, 50, endpoint=True)
F1 = 3 * np.sin(X)
F2 = np.sin(2*X)
F3 = 0.3 * np.sin(X)
startx, endx = -2 * np.pi - 0.1 , 2* np.pi + 0.1
starty, endy = -3.1, 3.1
plt.axis([startx, endx, starty, endy])
plt.plot(X, F1)
plt.plot(X, F2)
plt.plot(X, F3)
plt.plot(X, F1, 'ro')
plt.plot(X, F2, 'bx')
plt.show()

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涂作权的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值