3. Matplotlib设置轴标签和范围

《玩转Matplotlib数据绘图库》视频课程
《玩转Matplotlib数据绘图库》视频课程链接:https://edu.csdn.net/course/detail/28720

设置轴标签和范围

轴的标签 (Labels on Axes)

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

%matplotlib inline
import matplotlib.pyplot as plt
days = list(range(1,9))
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))
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()

在这里插入图片描述

定义轴的范围

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

days = list(range(1,9))
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 the 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()
The current limits for the axes are:
(0.6499999999999999, 8.35, 18.62, 40.18)
We set the axes to the following values:
0 10 14 45

在这里插入图片描述

import matplotlib.pyplot as plt
days = list(range(1,9))
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.axis([0, 10, 18, 41])
plt.show()

在这里插入图片描述

使用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 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 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()

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: matplotlib是一个用于绘制图形的Python库,其中的pyplot模块提供了一些高级别的接口用于数据可视化。pyplot模块支持包括线形图、柱状图、散点图、饼图等多种图形,其中坐标设置是比较重要的部分,包括坐标范围标签、刻度等。 1. 设置坐标范围 在pyplot模块中,我们可以通过设置xlim和ylim来设置x和y范围。例如: ```python import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y = np.sin(x) plt.plot(x, y) plt.xlim([-1, 11]) plt.ylim([-1.5, 1.5]) plt.show() ``` 上述代码会绘制一个sin曲线,同时通过xlim和ylim设置x范围为[-1, 11],y范围为[-1.5, 1.5]。 2. 设置坐标标签 我们可以使用xlabel和ylabel来为x和y设置标签。例如: ```python import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y = np.sin(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.show() ``` 上述代码会绘制一个sin曲线,同时设置x标签为'x',y标签为'y'。 3. 设置刻度 我们可以使用xticks和yticks来设置x和y的刻度。例如: ```python import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y = np.sin(x) plt.plot(x, y) plt.xticks([0, 5, 10]) plt.yticks([-1, 0, 1]) plt.show() ``` 上述代码会绘制一个sin曲线,同时设置x刻度为[0, 5, 10],y刻度为[-1, 0, 1]。 除了以上三个设置,还有很多其他的坐标设置可以使用,例如网格线、刻度标签旋转、坐标标题等。对于初学者来说,建议先掌握以上三个设置,逐步深入理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

bai666ai

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

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

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

打赏作者

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

抵扣说明:

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

余额充值