python/matplotlib-指定寄生轴长度
我正在绘制来自多个来源的数据,需要偏移量,最好是链接中看到的偏移量。我非常希望我的x轴具有可变长度,允许我在同一个图形上绘制多个图。到目前为止,我所做的是:python/matplotlib-指定寄生轴长度,python,matplotlib,axis,Python,Matplotlib,Axis,我正在绘制来自多个来源的数据,需要偏移量,最好是链接中看到的偏移量。我非常希望我的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='l
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)
因此,我让轴下降,但在我的一生中,无法弄清楚如何让轴实际水平压缩(例如,像上面链接图中的蓝色轴)
我的问题是如何做到这一点(如果有的话)
@臭氧层123 以下是我所拥有的:
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,所以恐怕我在这里还是个新手 您正在使用major
ax
对象创建par1.axis['bottom']
,因此您实际可以做的事情非常有限。相反,您应该创建两个或多个
轴
实例。并将它们放在figure实例上
添加新轴实例
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))
请注意,使用,您可以快速找到可用于控制的方法
新创建的轴实例
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:
控制新创建实例的位置:
[38]中的:cax1.set\u位置?
类型:instancemethod
字符串形式:
文件:/usr/lib/pymodules/python2.7/matplotlib/axes.py
定义:cax1.set_位置(self,pos,which='both')
文档字符串:
将轴位置设置为:
位置=[左、下、宽、高]
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]