迭代一组对象python

时间:2014-05-22 07:50:11

标签: python arrays loops matplotlib pandas

我正在尝试迭代由pandas.DataFrame.hist重新调整的matplotlib.axes.AxesSubplot数组,以使每个子组合图都具有逻辑性。以下示例代码不起作用

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()

2 个答案:

答案 0 :(得分:2)

使用flat iter:

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

答案 1 :(得分: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--')