6、set_xlim、set_ylim、xticks、yticks、set_xlabels、set_ylabels 和 双坐标轴twin()

本文介绍了Matplotlib中坐标轴的设置方法。可使用set_xlim()和set_ylim()明确设置x、y轴显示范围;xticks()和yticks()用于设置轴刻度位置,set_xlabels()和set_ylabels()用于设置刻度标签;还能通过twinx和twiny函数实现双坐标轴,以绘制不同单位的曲线。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、set_xlim()、set_ylim()

Matplotlib会自动得出沿x、y(和z轴,如果是3D图)轴显示的变量的最小值和最大值。然而,可以通过使用set_xlim()和set_ylim()函数明确地设置限制。实例如下:

下面代码是自动设置轴变量的:

import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
a1 = fig.add_axes([0.3,0.3,0.5,0.5])
x = np.arange(1,10)
a1.plot(x, np.exp(x))
a1.set_title('exp')
plt.show()

显示结果如下:
在这里插入图片描述
给x轴设置在0到10,给y轴设置0到10000,代码如下:

import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
a1 = fig.add_axes([0,0,1,1])
x = np.arange(1,10)
# 在axes区域画图并设置颜色为红色
a1.plot(x, np.exp(x),'r')
a1.set_title('exp')
# 给y轴设置变量范围
a1.set_ylim(0,10000)
# 给x轴设置变量范围
a1.set_xlim(0,10)
plt.show()

显示结果如下:
在这里插入图片描述

2、 xticks() 、yticks()、set_xlabels()、set_ylabels()

ticks是表示轴上数据点的标记。xticks()和yticks()函数使用一个列表对象作为参数。列表中的元素表示相应动作上显示ticks的位置。简单来说,ticks就是轴刻度对应的数。这里我们需要学习两个函数:

第一个:xticks() and yticks()

xticks()和yticks()函数使用一个列表对象作为参数。列表中的元素表示相应动作上显示ticks的位置。

ax.set_xticks([2,4,6,8,10])

第二个:set_xlabels() 和 set_ylabels()

ax.set_xlabels([‘two’, ‘four’,’six’, ‘eight’, ‘ten’])

实例如下:

import matplotlib.pyplot as plt
import numpy as np
import math
x = np.arange(0, math.pi*2, 0.05)
fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # main axes
y = np.sin(x)
ax.plot(x, y)
ax.set_xlabel(‘angle’)
ax.set_title('sine')
ax.set_xticks([0,2,4,6])
#ax.set_xticklabels(['zero','two','four','six'])
ax.set_yticks([-1,0,1])
plt.show()

显示结果如下:ax.set_xticks([0,2,4,6])
在这里插入图片描述
在ax.set_xticks([0,2,4,6])添加一行ax.set_xticklabels([‘zero’,‘two’,‘four’,‘six’])后显示图像如下:
在这里插入图片描述
可以看出,利用set_xticks([0,2,4,6])可以将我们的x轴显示4个刻度值并显示每个刻度上的值,用set_xticklabels([‘zero’,‘two’,‘four’,‘six’])会在x轴下方显示,并会替换掉原来的数字

set_xticks()和set_xticklabels()缺一不可,否则刻度个显示个数不确定,会乱。

3、双坐标轴

有时候我们需要用到双x轴和双y轴。在绘制不同单位的曲线时更是如此。Matplotlib通过twinx和twiny函数来支持这个功能。

在下面的例子中,该图有两个y轴,一个显示exp(x),另一个显示log(x)。

import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
a1 = fig.add_axes([0.1,0.1,0.8,0.8])
x = np.arange(1,11)
# 先设置好a1区域
a1.plot(x,np.exp(x))
a1.set_ylabel('exp')
# 利用a1.twin(),则还是这个区域作图,却使用了不同的y轴
a2 = a1.twinx()
a2.plot(x, np.log(x),'ro-')
a2.set_ylabel('log')
fig.legend(labels = ('exp','log'),loc='upper left')
plt.show()

显示结果如下:
在这里插入图片描述

#time = df["时间(hh:mm:ss)"] #将XX:XX:XX转换为min time = df["时间(hh:mm:ss)"] time_diff_mins = [0] t = datetime.strptime(df["时间(hh:mm:ss)"][0] , "%H:%M:%S")#起始 for i in range(1,len(time)): t1 = datetime.strptime(df["时间(hh:mm:ss)"][i] , "%H:%M:%S") time_diff = t1 - t#时间增量 time_diff_mins.append(round(time_diff.total_seconds()/60 , 2))#保留2位小数 #分别对分钟、油压、砂比、总排量赋值 p1 = np.array(time_diff_mins) p2 = np.array(df["油压(MPa)"]) p3 = np.array(df["砂比(%)"]) p4 = np.array(df["总排量(m^3)"]) fig , ax = plt.subplots(figsize=(8,4) , constrained_layout=True) ax.set_xlabel("Time(min)") ax.set_ylabel("Pressure(MPa)",color="blue") ax.set_xlim([0,120]) ax.set_ylim([0,120]) ax.tick_params(axis="y" , colors="blue") #创建共享x轴的twin1,twin2 twin1 = ax.twinx() twin2 = ax.twinx() ax.spines["right"].set_color("none") twin1.set_ylabel("Proppant conc(%)" , color="orange") twin1.set_ylim([0,80]) #修改坐标轴twin1刻度的颜色 twin1.tick_params(axis="y" , colors="orange") #确定twin2轴右边轴的位置为140 twin2.spines["right"].set_position(("data",140)) twin2.set_ylabel("Pume rate(m3/min)",color="g") twin2.set_ylim([0,40]) #修改坐标轴twin2刻度的颜色 twin2.tick_params(axis="y" , colors="green") #显示图例,对参数命名时加逗号,否则报错 z1, = ax.plot(p1 , p2 , linestyle="-" , color="blue" , label="Pressure(MPa)") z2, = twin1.plot(p1 , p3 , linestyle="-" , color="orange" , label="Proppant conc(%)") z3, = twin2.plot(p1 , p4 , linestyle="-" , color="green" , label="Pume rate(m3/min)") ax.legend(handles=[z1,z2,z3] , loc="upper left")
07-20
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

steelDK

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

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

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

打赏作者

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

抵扣说明:

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

余额充值