2

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

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)

所以我让轴下降,但在我的一生中,无法弄清楚如何让轴实际水平压缩(例如上面链接图中的蓝色轴)。

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


@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 轴消失了,我只剩下一个只有 cax1 的灰色屏幕。

抱歉,我只是拿起 matplotlib,所以恐怕我在这里还是个新手。

4

1 回答 1

1

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

添加新轴实例

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,您可以快速找到可用于控制新创建的轴实例的方法。

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]
于 2012-09-18T09:01:56.050 回答