【问题标题】:Iterating over an array of objects python迭代对象数组python
【发布时间】:2014-07-11 03:15:24
【问题描述】:

我正在尝试迭代由 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()

【问题讨论】:

  • "以下示例代码不起作用" -> 这是什么意思?你有任何错误吗?还是出乎意料的输出(您期望的输出是什么)?
  • “不工作”总是意味着“没有按我的预期执行”,但我们不知道您的期望。因此,请说明您的预期并描述您的观察结果。
  • 对不起,我得到的异常是“ValueError: Iterator global flags must be a list or tuple of strings”。

标签: python arrays loops matplotlib pandas


【解决方案1】:

使用flatiter:

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

【讨论】:

    【解决方案2】:

    你不是忘了画点什么吗? 查看 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--')
    

    【讨论】:

      猜你喜欢
      • 2021-06-22
      • 2019-08-11
      • 1970-01-01
      • 2017-03-29
      • 2019-02-27
      • 2017-07-30
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多