Python 在两个x轴上以不同比例在同一图形中绘制两个函数
我想在同一个图形中绘制两个不同的函数,但是我希望它们在x轴上使用不同的比例。 一个刻度应该只显示x的值,其他刻度最后必须显示秒 现在我有这个Python 在两个x轴上以不同比例在同一图形中绘制两个函数,python,matplotlib,plot,axis,Python,Matplotlib,Plot,Axis,我想在同一个图形中绘制两个不同的函数,但是我希望它们在x轴上使用不同的比例。 一个刻度应该只显示x的值,其他刻度最后必须显示秒 现在我有这个 k=5 fig = plt.figure() ax1 = fig.add_subplot(111) ax1.set_xlabel(r"values of x") #adds description to scale on bottom ax2 = ax1.twiny() #adds the seconds scale on top x =
k=5
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xlabel(r"values of x") #adds description to scale on bottom
ax2 = ax1.twiny() #adds the seconds scale on top
x = np.arange(0.1, 1.5, 0.1) #values of x for function are in range
y = k*(np.power(x,(k-1))) * np.exp(-(np.power(x,(k-1)))) #that is the function I want to draw
ax1.plot(x,y) #draw function
tx = x
ty = x*7
ax2.plot(x,x*7)
ax2.set_xlabel(r"time in seconds")
ax2.set_xlim(1484) #set limit of time
ax2.invert_xaxis() #invert it so that it works like we want to
ax1.set_xlim(0.1,1.4) #set limit for the x axis so that it doesn't skale on its own.
plt.show()
很抱歉,我无法正确插入代码。
ax2函数现在只是一个虚拟函数。我只想能够看到它,并在最后改变ax2的规模,以我的时间框架
任何帮助都将不胜感激 我不确定您的代码是否工作:-p ax2的虚拟函数不够好,我将其替换为
ax2.plot(x*1000,x*50)
,以便能够看到它
我在重新缩放后进行绘图:
k=5
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xlabel(r"values of x") #adds description to scale on bottom
ax2 = ax1.twiny() #adds the seconds scale on top
x = np.arange(0.1, 1.5, 0.1) #values of x for function are in range
y = k*(np.power(x,(k-1))) * np.exp(-(np.power(x,(k-1)))) #that is the function I want to draw
ax1.plot(x,y) #draw function
tx = x
ty = x*7
ax2.set_xlabel(r"time in seconds")
ax2.set_xlim(1484) #set limit of time
ax2.invert_xaxis() #invert it so that it works like we want to
ax2.plot(x*1000,x*50)
ax1.set_xlim(0.1,1.4) #set limit for the x axis so that it doesn't skale on its own.
plt.show()
其中:
第二个绘图隐藏在左Y轴后面。如果使用较粗的线和/或标记,您将能够看到它:
ax2.plot(x,x*7, '-o', lw=5)
您也可以更改ax2的x限制,但您已尽力使其保持原样,因此我想这正是您所希望的