<Python, matplotlib, seaborn> xlim, ylimの値をゲット

seabornや、matplotlibで作ったグラフgraphのX/Yの端の座標をゲットgetする方法。
.get_xlim()の関数methodを使う。

では、一例。
seabornFacetオブジェクトの場合、matplotlib.axes.Axesに辿り着くまでちと深い。

In [1]: import pandas as pd

In [2]: import numpy as np

In [3]: import seaborn as sns

In [4]: import matplotlib.pyplot as plt

In [5]: df0 = pd.DataFrame(data=np.random.randn(10,1), columns=['val'])

In [6]: df0['key'] = '1st'

In [7]: df1 = pd.DataFrame(data=np.random.randn(10,1), columns=['val'])

In [8]: df1['key'] = '2nd'

In [9]: df = pd.concat([df0, df1])

In [10]: g = sns.FacetGrid(df, hue='key')
    ...: g.map(sns.violinplot, 'val', label='key')
    ...: g.add_legend()
    ...: 
Out[10]: <seaborn.axisgrid.FacetGrid at 0x92940b8>

f:id:nekoyukimmm:20151107171201p:plain

In [11]: type(g.axes)
Out[11]: numpy.ndarray

In [12]: g.axes
Out[12]: array([[<matplotlib.axes._subplots.AxesSubplot object at 0x0000000009294A90>]], dtype=object)

In [13]: g.axes[0]
Out[13]: array([<matplotlib.axes._subplots.AxesSubplot object at 0x0000000009294A90>], dtype=object)

In [14]: g.axes[0][0]
Out[14]: <matplotlib.axes._subplots.AxesSubplot at 0x9294a90>

In [15]: g.axes[0][0].get_xlim()
Out[15]: (-3.0, 3.0)

In [16]: g.axes[0][0].get_ylim()
Out[16]: (0.5, -0.5)

ちなみに、type()で、評価したい関数に()を付けると、戻り値returnを評価。
つけないと、関数そのものを評価。

In [17]: type(g.axes[0][0].get_ylim())
Out[17]: tuple

In [18]: type(g.axes[0][0].get_ylim)
Out[18]: method

へー。。。

 
 
マニュアルリンク。matplotlib.axes.Axes.get_xlim
axes — Matplotlib 1.3.0 documentation