2015-05-28 149 views
0

繪製ylabel我需要繪製多指標數據框從列0級ylabels。數據幀如下:如何大熊貓多指標數據幀

PCs        pc1   pc2   pc3 \ 
explained_variance_ratio 9.977643e-01 2.196399e-03 3.275875e-05 
wavelength               
540.00      0.015110  -0.004772  -0.018467 
540.05      0.015110  -0.004772  -0.018467 
540.10      0.015110  -0.004772  -0.018467 
540.15      0.015110  -0.004772  -0.018467 
540.20      0.015110  -0.004772  -0.018467 
540.25      0.015110  -0.004772  -0.018467 
540.30      0.015110  -0.004772  -0.018467 
540.35      0.015110  -0.004772  -0.018467 
540.40      0.015081  -0.004226  -0.017577 
540.45      0.015081  -0.004226  -0.017577 
....... 

ylabel應該是pc1,pc2,pc3 ....帶波長的xlabel。

我曾嘗試以下不工作:

pca_df.plot(subplots=True, sharex=True, title='Principle Components', \ 
    legend=False, y=list(pca_df.columns.levels[0])) 

錯誤是:

Traceback (most recent call last): 
    File "<stdin>", line 2, in <module> 
    File "/usr/lib/python2.7/dist-packages/pandas/tools/plotting.py", line 1716, in plot_frame 
    ser = frame[y] 
    File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 1652, in __getitem__ 
    return self._getitem_array(key) 
    File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 1697, in _getitem_array 
    return self.take(indexer, axis=1, convert=True) 
    File "/usr/lib/python2.7/dist-packages/pandas/core/generic.py", line 1166, in take 
    indices, len(self._get_axis(axis))) 
    File "/usr/lib/python2.7/dist-packages/pandas/core/indexing.py", line 1438, in _maybe_convert_indices 
    if mask.any(): 
AttributeError: 'bool' object has no attribute 'any' 

也試過:

pca_df.plot(subplots=True, sharex=True, title='Principle Components', \ 
    legend=False) 
plt.ylabel(pca_df.columns.levels[0]) 

雖然繪製覆蓋所有ylabel情節很簡單:

Index([u'pc1', u'pc10', u'pc2', u'pc3', u'pc4', u'pc5', u'pc6', u'pc7', u'pc8', u'pc9'], dtype='object') 

回答

3

我通常做的大部分格式的斧頭或致電plot後軸本身。 試試這個:

axes = pca_df.plot(subplots=True, sharex=True, title='Principle Components', legend=False) 
for ax, label in zip(axes.ravel(), pca_df.columns.levels[0]): 
    ax.set_ylabel(label) 

我覺得它更容易使用的軸API,而不是試圖把它全部融入劇情方法調用。請注意,當次要情節是真實的,劇情方法返回軸,你必須獲得通過索引或迭代,因爲我在這裏所做的數組。該.ravel是處理與你當你的身材有多個行和列的二維數組的一個很好的方式,但它仍然有效,如果數組只有一維的。