不同图形上 Y 轴的相同比例
same scale of Y axis on differents figures
我尝试在几个图形上绘制具有相似表示但行为和来源略有不同的不同数据。所以 Y 轴的最小值和最大值在每个图之间是不同的,但是比例也是。
例如这是我的批处理绘图的一些摘录:
是否存在一种使用 matplotlib 的简单方法来将相同的 Y 步骤约束在那些不同的图形上,以便进行简单的视觉解释,同时保持自动确定的 Y 最小值和 Y 最大值?
换句话说,我希望每个 Y 刻度
之间的间距相同 metric
您可以在两个轴上使用 MultipleLocator
from the ticker
模块来定义刻度间距:
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
fig=plt.figure()
ax1=fig.add_subplot(211)
ax2=fig.add_subplot(212)
ax1.set_ylim(0,100)
ax2.set_ylim(40,70)
# set ticks every 10
tickspacing = 10
ax1.yaxis.set_major_locator(ticker.MultipleLocator(base=tickspacing))
ax2.yaxis.set_major_locator(ticker.MultipleLocator(base=tickspacing))
plt.show()
编辑:
您想要的行为似乎与我对您问题的解释不同。这是一个函数,它将更改 y 轴的限制以确保两个子图的 ymax-ymin
相同,使用两个 ylim
范围中较大的一个来更改较小的一个。
import matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()
ax1=fig.add_subplot(211)
ax2=fig.add_subplot(212)
ax1.set_ylim(40,50)
ax2.set_ylim(40,70)
def adjust_axes_limits(ax1,ax2):
yrange1 = np.ptp(ax1.get_ylim())
yrange2 = np.ptp(ax2.get_ylim())
def change_limits(ax,yr):
new_ymin = ax.get_ylim()[0] - yr/2.
new_ymax = ax.get_ylim()[1] + yr/2.
ax.set_ylim(new_ymin,new_ymax)
if yrange1 > yrange2:
change_limits(ax2,yrange1-yrange2)
elif yrange2 > yrange1:
change_limits(ax1,yrange2-yrange1)
else:
pass
adjust_axes_limits(ax1,ax2)
plt.show()
请注意,此处的第一个子图已从 (40, 50)
扩展到 (30, 60)
,以匹配第二个子图的 y 范围
汤姆的回答很好!
但我决定使用更简单的解决方案
我为我所有的地块定义了一个任意的 yrange
,例如
yrang = 0.003
对于每个地块,我都这样做:
ymin, ymax = ax.get_ylim()
ymid = np.mean([ymin,ymax])
ax.set_ylim([ymid - yrang/2 , ymid + yrang/2])
并且可能:
ax.yaxis.set_major_locator(ticker.MultipleLocator(base=0.005))
我尝试在几个图形上绘制具有相似表示但行为和来源略有不同的不同数据。所以 Y 轴的最小值和最大值在每个图之间是不同的,但是比例也是。
例如这是我的批处理绘图的一些摘录:
是否存在一种使用 matplotlib 的简单方法来将相同的 Y 步骤约束在那些不同的图形上,以便进行简单的视觉解释,同时保持自动确定的 Y 最小值和 Y 最大值?
换句话说,我希望每个 Y 刻度
之间的间距相同 metric您可以在两个轴上使用 MultipleLocator
from the ticker
模块来定义刻度间距:
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
fig=plt.figure()
ax1=fig.add_subplot(211)
ax2=fig.add_subplot(212)
ax1.set_ylim(0,100)
ax2.set_ylim(40,70)
# set ticks every 10
tickspacing = 10
ax1.yaxis.set_major_locator(ticker.MultipleLocator(base=tickspacing))
ax2.yaxis.set_major_locator(ticker.MultipleLocator(base=tickspacing))
plt.show()
编辑:
您想要的行为似乎与我对您问题的解释不同。这是一个函数,它将更改 y 轴的限制以确保两个子图的 ymax-ymin
相同,使用两个 ylim
范围中较大的一个来更改较小的一个。
import matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()
ax1=fig.add_subplot(211)
ax2=fig.add_subplot(212)
ax1.set_ylim(40,50)
ax2.set_ylim(40,70)
def adjust_axes_limits(ax1,ax2):
yrange1 = np.ptp(ax1.get_ylim())
yrange2 = np.ptp(ax2.get_ylim())
def change_limits(ax,yr):
new_ymin = ax.get_ylim()[0] - yr/2.
new_ymax = ax.get_ylim()[1] + yr/2.
ax.set_ylim(new_ymin,new_ymax)
if yrange1 > yrange2:
change_limits(ax2,yrange1-yrange2)
elif yrange2 > yrange1:
change_limits(ax1,yrange2-yrange1)
else:
pass
adjust_axes_limits(ax1,ax2)
plt.show()
请注意,此处的第一个子图已从 (40, 50)
扩展到 (30, 60)
,以匹配第二个子图的 y 范围
汤姆的回答很好! 但我决定使用更简单的解决方案
我为我所有的地块定义了一个任意的 yrange
,例如
yrang = 0.003
对于每个地块,我都这样做:
ymin, ymax = ax.get_ylim()
ymid = np.mean([ymin,ymax])
ax.set_ylim([ymid - yrang/2 , ymid + yrang/2])
并且可能:
ax.yaxis.set_major_locator(ticker.MultipleLocator(base=0.005))