2014-05-22 262 views
0

我想迭代matplotlib.axes.AxesSubplot的數組,重新調用pandas.DataFrame.hist來使每個子記錄logy。下面的示例代碼不起作用迭代對象數組python

from pandas import DataFrame 
import numpy as np 
import matplotlib.pyplot as plt 

x = np.random.uniform(0, 100, size=1000) 
y = x *x + 50*x*np.random.randn(1000) 
z = x * y + 50*y*np.random.randn(1000) 

frame = DataFrame({'z' : z,'x' : x , 'y' : y}) 

Histograms = frame.hist(bins=50) 
for axis in np.nditer(Histograms,"refs_ok"): 
    axis.set_yscale("log", nonposy='clip') 

plt.show() 
+1

「以下示例代碼不起作用」 - >這是什麼意思?你有任何錯誤?或意想不到的輸出(你期望輸出什麼)? –

+0

「不起作用」總是意味着「沒有按照我的預期行事」,但我們不知道你的期望。所以請說出你的期望,並描述你所得到的觀察結果。 – Alfe

+0

對不起,我得到的這個版本的「ValueError:迭代器全局標誌必須是一個列表或字符串元組」。 – Keith

回答

2

使用flat ITER:

for axis in Histograms.flat: 
    axis.set_yscale("log", nonposy='clip') 
0

你不是忘了什麼陰謀? 看看matplotlib示例:

""" 
Demo of the histogram (hist) function with a few features. 

In addition to the basic histogram, this demo shows a few optional features: 

    * Setting the number of data bins 
    * The ``normed`` flag, which normalizes bin heights so that the integral of 
     the histogram is 1. The resulting histogram is a probability density. 
    * Setting the face color of the bars 
    * Setting the opacity (alpha value). 

""" 
import numpy as np 
import matplotlib.mlab as mlab 
import matplotlib.pyplot as plt 


# example data 
mu = 100 # mean of distribution 
sigma = 15 # standard deviation of distribution 
x = mu + sigma * np.random.randn(10000) 

num_bins = 50 
n, bins, patches = plt.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5) 
plt.plot(bins, y, 'r--')