python/matplotlib - 指定寄生 Axis 长度

标签 python matplotlib axis

我正在绘制来自多个来源的数据,需要 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]

关于python/matplotlib - 指定寄生 Axis 长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12465975/

相关文章:

python - 如何将(自定义)函数传递给 simplify 的 "locals"参数?

python - 使用scrapy在没有javascript代码的情况下抓取文本

python - 如何在Python中用点覆盖水平条形图?

python - 颜色条打断轴

python - 带有 twinx() : how to add to legend? 的辅助轴

python - 是否可以通过 Python 使用 Cocoa API?

python - 从 pandas.read_csv() 中删除重复的列

python - 在 Matplotlib、Python 中完全自定义图例

java - Axis :没有引擎配置文件 - 中止

java - 将Java Bean配置为Web服务