2014-05-22 215 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--')