上一章我们已经学习了画Figure的三种方法,本章我们就来学习如何操控MatplotLib中的坐标轴(Lim, Tick)和边框(Spines)。
首先我们导入需要的库,并创建用来画图的数据:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5, 5, 50) # 50个-5到5之间的等分点
y1 = 2 * x
y2 = x ** 2
基本设置
标题 Title
标题可以使用plt.title实现:
plt.plot(x, y1)
plt.plot(x, y2)
plt.title("Title", color = "red")
plt.show()
![](https://i0.wp.com/turingplanet.org/wp-content/uploads/2021/09/Screen-Shot-2021-09-23-at-7.32.34-PM.png?resize=312%2C220&ssl=1)
坐标轴名称 Label
设置x轴和y轴坐标轴名称相对应的方法是xlabel和ylabel:
plt.plot(x, y1)
plt.plot(x, y2)
plt.xlabel("x label")
plt.ylabel("y label")
plt.show()
![](https://i0.wp.com/turingplanet.org/wp-content/uploads/2021/09/Screen-Shot-2021-09-23-at-7.34.03-PM.png?resize=332%2C218&ssl=1)
坐标轴
接下来学习控制坐标轴中两个非常重要的元素:取值范围和记号。
坐标轴范围 Lim
xlim可以控制x轴的取值范围,ylim可以控制y轴的取值范围:
plt.plot(x, y1)
plt.plot(x, y2)
plt.xlim((-2, 2)) # x的取值范围为-2到2
plt.ylim((-5, 10)) # y的取值范围为-5到10
plt.show()
![](https://i0.wp.com/turingplanet.org/wp-content/uploads/2021/09/Screen-Shot-2021-09-23-at-7.39.26-PM.png?resize=362%2C242&ssl=1)
如果使用axe来限制取值范围的话,可以使用相对应的set_xlim和set_ylim来实现:
fig, axe = plt.subplots()
axe.plot(x, y1)
axe.plot(x, y2)
axe.set_xlim((-2, 2))
axe.set_ylim((-5, 10))
plt.show()
坐标轴记号和记号标签 Tick
记号(Tick)是坐标轴上用来显示特定点的标记,此标记可以是数字、文字、或其他形式的记号。(默认记号为数字)
我们可以通过xticks函数限制x坐标轴记号的显示范围:
plt.plot(x, y1)
plt.plot(x, y2)
plt.xticks(np.linspace(-2, 2, 3)) # 只显示-2到2之间的3个记号
plt.show()
![](https://i0.wp.com/turingplanet.org/wp-content/uploads/2021/09/Screen-Shot-2021-09-23-at-7.50.41-PM.png?resize=382%2C256&ssl=1)
如果想要将特定的数字转化成文字标记,可以使用两个相对应的list来实现:
plt.plot(x, y1)
plt.plot(x, y2)
plt.xlim((-2, 2))
plt.ylim((-2, 2))
plt.xticks([-2, 0, 2], ["x1", "x2", "x3"]) # x轴-2, 0, 2对应x1, x2, x3文字标记
plt.yticks([-2, -1, 0, 1, 2], ["y1", "y2", "y3", "y4", "y5"]) # y轴-2, -1, 0, 1, 2对应y1, y2, y3, y4, y5文字标记
plt.show()
![](https://i0.wp.com/turingplanet.org/wp-content/uploads/2021/09/Screen-Shot-2021-09-23-at-7.52.59-PM.png?resize=402%2C272&ssl=1)
在axe中可以使用set_xticks设置记号,然后通过tick_params设置其他的参数:
fig, axe = plt.subplots()
axe.plot(x, y1)
axe.plot(x, y2)
axe.set_xlim((-2, 2))
axe.set_ylim((-5, 10))
axe.set_xticks(np.linspace(-2, 2, 3))
axe.tick_params(direction = 'in', length = 5, width = 2, colors = 'b')
plt.show()
![](https://i0.wp.com/turingplanet.org/wp-content/uploads/2021/09/Screen-Shot-2021-09-23-at-7.54.47-PM.png?resize=402%2C280&ssl=1)
边框 Spines
Spines是连接轴刻度标记的线,它对应的是坐标系中上下左右(top, bottom, left, right)四个边框,说白了它能标明数据区域的边界。
我们可以直接通过axe中的spines来获取相对应的上下左右(top,bottom,left,right)四个边框,以下我们将左边框和下边框放到中间:
fig, axe = plt.subplots()
axe.plot(x, y1)
axe.plot(x, y2)
axe.set_xticks(np.linspace(-5, 5, 9))
axe.spines['left'].set_position(('axes', 0.5)) # 左边框移到中间
axe.spines['bottom'].set_position(('axes', 0.5)) # 下边框移到中间
plt.show()
![](https://i0.wp.com/turingplanet.org/wp-content/uploads/2021/09/Screen-Shot-2021-09-23-at-8.03.09-PM.png?resize=418%2C264&ssl=1)
如果想要隐藏特定边框,可以直接将其颜色设为none:
fig, axe = plt.subplots()
axe.plot(x, y1)
axe.plot(x, y2)
axe.set_xticks(np.linspace(-5, 5, 9))
axe.spines['left'].set_position(('axes', 0.5))
axe.spines['bottom'].set_position(('axes', 0.5))
axe.spines['top'].set_color('none') # 将上表框隐藏
axe.spines['right'].set_color('none') # 将下边框隐藏
plt.show()
![](https://i0.wp.com/turingplanet.org/wp-content/uploads/2021/09/Screen-Shot-2021-09-23-at-8.09.09-PM.png?resize=426%2C284&ssl=1)