【matplotlib基础】--坐标轴
Matplotlib的坐标轴是用于在绘图中表示数据的位置的工具。坐标轴是图像中的水平和垂直线,它们通常表示为 x 轴和 y 轴。
坐标轴的作用是帮助观察者了解图像中数据的位置和大小,通常标有数字或标签,以指示特定的值在图像中的位置。
1. 坐标轴范围
Matplotlib绘制图形时,会自动根据X,Y轴的数值,自动确定其范围,确保能够涵盖所有的数值。
比如:
_, ax = plt.subplots(2, 1)
#X轴范围0~8,Y轴范围1~100
x = np.array(range(0, 8))
y = np.random.randint(1, 100, 8)
ax.plot(x, y)
#X轴范围10~18,Y轴范围100~200
x = np.array(range(10, 18))
y = np.random.randint(100, 200, 8)
ax.plot(x, y)https://cdn.nlark.com/yuque/0/2023/png/2235414/1686640515871-0d6d7238-8ec7-418c-9126-e160b7c209af.png#averageHue=%23fafafa&clientId=uc6f70997-263e-4&from=paste&height=412&id=uc8bec98f&originHeight=412&originWidth=548&originalType=binary&ratio=1&rotation=0&showTitle=false&size=27569&status=done&style=stroke&taskId=uc60aaccf-7c08-4a8c-942d-4425b8db76a&title=&width=548
可以看出,图形中X轴,Y轴的范围是根据 x, y列表中数值的最大最小值来生成的。
有时候,为了看图的局部位置,可以主动设置X轴,Y轴的范围,而不是依靠自动生成。
比如:
_, ax = plt.subplots(2, 1)
x = np.array(range(0, 8))
y = np.random.randint(1, 100, 8)
ax.set_xlim(3, 6) #X轴范围3~6
ax.plot(x, y)
x = np.array(range(10, 18))
y = np.random.randint(100, 200, 8)
ax.set_ylim(120, 150) #Y轴范围120~150
ax.plot(x, y)https://cdn.nlark.com/yuque/0/2023/png/2235414/1686641685053-1b31bb3a-6a18-4889-a547-8db6bbaddbbe.png#averageHue=%23f9f9f9&clientId=uc6f70997-263e-4&from=paste&height=412&id=u4c59fb2c&originHeight=412&originWidth=560&originalType=binary&ratio=1&rotation=0&showTitle=false&size=26770&status=done&style=stroke&taskId=u8a3202a4-033b-4349-aaf8-041d726b165&title=&width=560
上面的示例设置的第一个图的X轴范围,第二个图的Y轴范围。
2. 双坐标轴
如果要把Y轴不同范围的两个曲线放在一起比较趋势的话,就要用到双坐标轴。
比如:
fig = plt.figure()
ax = fig.add_axes()
x = np.array(range(0, 8))
y1 = np.random.randint(1, 100, 8)
ax.plot(x, y1, c='r')
y2 = np.random.randint(100, 200, 8)
ax.plot(x, y2, c='g')https://cdn.nlark.com/yuque/0/2023/png/2235414/1686642581371-a06b5f01-e52a-4be0-a9f5-fc53321714da.png#averageHue=%23fcfafa&clientId=uc6f70997-263e-4&from=paste&height=426&id=u5307efa7&originHeight=426&originWidth=564&originalType=binary&ratio=1&rotation=0&showTitle=false&size=21473&status=done&style=stroke&taskId=u8bd26ab3-ac45-4ac8-8c6e-f3c5af71491&title=&width=564
上图中红色的线范围在1~100之间,绿色的线范围在100~200之间。
虽然放在一个图中比较,看着却像是在两个子图。
这时,我们可以用两个不同范围的Y轴,从而能够让两条曲线更好的比较。
比如:
fig = plt.figure()
ax = fig.add_axes()
ax_twinx = ax.twinx()
x = np.array(range(0, 8))
y1 = np.random.randint(1, 100, 8)
ax.plot(x, y1, c='r')
y2 = np.random.randint(100, 200, 8)
ax_twinx.plot(x, y2, c='g')https://cdn.nlark.com/yuque/0/2023/png/2235414/1686642740214-64d38a8f-312a-4563-93e7-6937886981fd.png#averageHue=%23fbf9f9&clientId=uc6f70997-263e-4&from=paste&height=426&id=u623e6dcf&originHeight=426&originWidth=597&originalType=binary&ratio=1&rotation=0&showTitle=false&size=29862&status=done&style=stroke&taskId=u47ea2aba-5d77-4ec9-929b-180d8168a23&title=&width=597
左边是红线对应的Y轴,右边是绿线对应的Y轴。
3. 反坐标轴
最后,关于坐标轴的设置,还有一个比较常用的设置是反转坐标轴。
坐标轴的默认顺序是从小到大的,但是,对于一些特殊的图表类型(如散点图、条形图、直方图等),可以通过反转坐标轴来更好地展示数据点的分布情况。
3.1. 反转X轴
fig = plt.figure()
x = np.array(range(0, 8))
y = np.random.randint(1, 100, 8)
ax1 = fig.add_subplot(211)
ax1.plot(x, y)
#反转X轴
ax2 = fig.add_subplot(212)
ax2.invert_xaxis()
ax2.plot(x, y)https://cdn.nlark.com/yuque/0/2023/png/2235414/1686643592515-dd745d4c-37fb-4626-b500-46cd527065b7.png#averageHue=%23fafafa&clientId=uc6f70997-263e-4&from=paste&height=412&id=uf57d3c73&originHeight=412&originWidth=540&originalType=binary&ratio=1&rotation=0&showTitle=false&size=26628&status=done&style=stroke&taskId=u74aaf820-4270-40e1-83ea-e639b1fe57e&title=&width=540
上例两个子图的X轴顺序是相反的。
3.2. 反转Y轴
fig = plt.figure()
x = np.array(range(0, 8))
y = np.random.randint(1, 100, 8)
ax1 = fig.add_subplot(211)
ax1.plot(x, y)
#反转Y轴
ax2 = fig.add_subplot(212)
ax2.invert_yaxis()
ax2.plot(x, y)https://cdn.nlark.com/yuque/0/2023/png/2235414/1686643661190-5e2520bb-b09b-4903-bbeb-9effae247962.png#averageHue=%23fafafa&clientId=uc6f70997-263e-4&from=paste&height=415&id=u61b5ed1d&originHeight=415&originWidth=548&originalType=binary&ratio=1&rotation=0&showTitle=false&size=22648&status=done&style=stroke&taskId=u38e5ab9d-bedc-4029-ba2d-754856f5638&title=&width=548
上例两个子图的Y轴顺序是相反的。
4. 总结回顾
这里介绍的主要是坐标轴在展示分析结果的不同场景中的常用设置方法,
其他关于坐标轴字体,颜色等等可以查阅官方文档。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页:
[1]