Skip to main content
 首页 » 编程设计

python/matplotlib之指定寄生 Axis 长度

2025年05月04日10unruledboy

我正在绘制来自多个来源的数据,需要 multiple x axes ,最好像链接中看到的那样偏移。我非常希望我的 x Axis 具有可变长度,允许我在同一个图形上放置多个图。到目前为止我所做的是:

import matplotlib.pyplot as plt 
from mpl_toolkits.axes_grid1 import host_subplot 
import mpl_toolkits.axisartist as AA 
host = host_subplot(111, axes_class=AA.Axes,yscale='log') 
plt.subplots_adjust(bottom=0.25) 
 
par1 = host.twiny() 
 
offset = 60 
 
new_fixed_axis = par1.get_grid_helper().new_fixed_axis 
par1.axis['bottom'] = new_fixed_axis(loc='bottom', 
                                     axes=par1, 
                                     offset=(0, -60)) 
 
host.set_xlim(200, 350) 
host.set_ylim(1050, 100) 
par1.set_xlim(0, 1) 
 
host.set_xlabel('Temperature (K)') 
host.set_ylabel('Pressure (hPa)') 
par1.set_xlabel('Relative Humidity (%)') 
 
p1, = host.plot(T,P) 
p2, = host.plot(pT,P) 
p2, = par1.plot(RH,P) 

所以我让 Axis 下降,但我无法弄清楚如何让 Axis 实际水平压缩(例如,就像上图中的蓝色 Axis )。

我的问题是如何做到这一点(如果有的话)?


@Oz123

这是我的:

host = host_subplot(111, axes_class=AA.Axes,yscale='log') 
plt.subplots_adjust(bottom=0.25) 
 
par1 = host.twiny() 
 
new_fixed_axis = par1.get_grid_helper().new_fixed_axis 
 
cax1 = plt.axes(axisbg='none',frameon=False) 
cax1 = plt.add_axes(plt.get_position(), frameon=False) 
par1.axis['bottom'] = new_fixed_axis(loc='bottom', 
                                     axes=cax1, 
                                     offset=(0, -60)) 

当我到达:

cax1 = plt.add_axes(plt.get_position(), frameon=False) 

我以前的 x/y Axis 消失了,我只剩下 cax1 的灰色屏幕。

抱歉,我刚刚开始学习 matplotlib,所以恐怕我在这里还是个新手。

请您参考如下方法:

您正在使用主要的 ax 对象创建 par1.axis['bottom'],因此您实际可以做的事情非常有限。
相反,您应该创建 2 个或更多 axes 实例。并将它们放在图形实例上。

添加新的 Axis 实例

cax1 = plt.axes(axisbg='none', frameon=False)

像这样,您可以对湿度标尺的大小进行精细控制。

下面一行:

par1.axis['bottom'] = new_fixed_axis(loc='bottom', 
                                     axes=par1, 
                                     offset=(0, -60)) 

例如应该是:

par1.axis['bottom'] = new_fixed_axis(loc='bottom', 
                                     axes=cax1, # custom axis number 1 
                                     offset=(0, -60)) 

请注意,使用 IPython ,可以快速找到哪些方法可以控制 您新创建的 Axis 实例。

In [38]: cax1.set_ #tab pressed 
cax1.set_adjustable            cax1.set_axis_bgcolor          cax1.set_frame_on              cax1.set_subplotspec           cax1.set_xticks 
cax1.set_agg_filter            cax1.set_axis_off              cax1.set_gid                   cax1.set_title                 cax1.set_ybound 
cax1.set_alpha                 cax1.set_axis_on                
# many more options trimmed, but I think you might want to take a look in: 

控制新创建实例的位置:

In [38]: cax1.set_position? 
Type:       instancemethod 
String Form:<bound method AxesSubplot.set_position of <matplotlib.axes.AxesSubplot object at 0x2d7fb90>> 
File:       /usr/lib/pymodules/python2.7/matplotlib/axes.py 
Definition: cax1.set_position(self, pos, which='both') 
Docstring: 
Set the axes position with:: 
 
  pos = [left, bottom, width, height]