如何仅根据一个图将平轴缩放而不延长其他地块?
我想在一个图中绘制几个函数,但是如果绘制一个函数比其他函数高/较小,我想防止扩展轴。在下面的代码中,参数alpha
实际上是随机的(在这里,我将其修复到alpha = 2
),并且可以获得很高的值,这会使我的情节弄乱。基本上,我想做的是,我想绘制一个功能,然后根据其xlim
和ylim
冻结轴,然后添加其余的绘图而无需扩展如果alpha
恰好很大,则轴了。我该怎么做? 这里的解决方案不幸的是,不幸的是对我,即使用<<代码> plt.autoscale(false)我需要手动修复限制,这不是我想要的。
这是一个最小工作示例:
x = np.linspace(0,4*np.pi)
data1 = np.sin(0.5*x)
alpha = 2
data2 = alpha*np.sin(x)
data3 = np.sin(x)
data4 = np.sin(x)
data5 = np.cos(x)
fig = plt.figure(constrained_layout=True, figsize=(10, 4))
subfigs = fig.subfigures(1, 2, wspace=0.07)
axsLeft = subfigs[0].subplots(1, 1)
axsLeft.plot(x,data1)
# plt.autoscale(False)
axsLeft.plot(x,data2) #final prediction
axsLeft.plot(x,data3,'--k',linewidth=2.5)
# axsLeft.set_ylim([-1.05,+1.05])
axsLeft.set_xlabel("x")
axsRight = subfigs[1].subplots(2, 1, sharex=True)
axsRight[0].plot(data4)
axsRight[1].plot(data5)
axsRight[1].set_xlabel('x')
fig.show()
I want to plot several functions in one figure, but I want to prevent the axis to be extended if one function is plotted that has much higher/smaller values than others. In the code below, the parameter alpha
is actually random (here I fixed it to alpha = 2
), and could get very high values which messes up my plot. Basically what I would like to do is, I'd like to plot one function, then freeze the axis according to its xlim
and ylim
, then add the remaining plots without extending the axis anymore if alpha
happens to be large. How can I do this? The solution here did unfortunately not work for me, i.e., using plt.autoscale(False)
I would need to fix the limits manually, which is not what I want.
Here is a minimum working example:
x = np.linspace(0,4*np.pi)
data1 = np.sin(0.5*x)
alpha = 2
data2 = alpha*np.sin(x)
data3 = np.sin(x)
data4 = np.sin(x)
data5 = np.cos(x)
fig = plt.figure(constrained_layout=True, figsize=(10, 4))
subfigs = fig.subfigures(1, 2, wspace=0.07)
axsLeft = subfigs[0].subplots(1, 1)
axsLeft.plot(x,data1)
# plt.autoscale(False)
axsLeft.plot(x,data2) #final prediction
axsLeft.plot(x,data3,'--k',linewidth=2.5)
# axsLeft.set_ylim([-1.05,+1.05])
axsLeft.set_xlabel("x")
axsRight = subfigs[1].subplots(2, 1, sharex=True)
axsRight[0].plot(data4)
axsRight[1].plot(data5)
axsRight[1].set_xlabel('x')
fig.show()
This orange plot extends the axis such that the other plots are not interpretable anymore. I'd like the orange plot to to overshoot in the y-direction, like this:
but without setting ylim
manually.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
绘制参考后,在您的情况下,您可以在SEPERATE变量中使用
get_ylim()
a
a 和b
检索定义的y轴限制。并在用set_ylim
绘制其余曲线后相应地重新汇总轴:这确保轴始终根据参考缩放,即使y轴的下限非常低或零,也可以工作。
After plotting the reference, in your case data1, you can retrieve the defined y-axis limits with
get_ylim()
in seperate variablesa
andb
and rescale your axis accordingly after plotting the remaining curves withset_ylim
:This makes sure the axis is always scaled according to the reference and it works even if the lower limit of the y-axis is very low or zero.
如果要将Y轴调整为Data1的最大值和最小值,请使用以下代码。 (0.05是填充。)
如果您希望alpha值也根据Data1而变化,则可以通过从np.max()和np.min()中减去alpha值来获得该值。以下是您上传的代码的修改版本。
If you want to adjust the y-axis to the maximum and minimum values of data1, use the code below. (0.05 is padding.)
If you want the alpha value to also vary according to data1, you can get the value by subtracting the alpha value from np.max() and np.min(). Below is a modified version of the code you uploaded.