0

I am trying to iterated over the array of matplotlib.axes.AxesSubplot retuned by pandas.DataFrame.hist to make each subhistogram logy. The following example code does not work

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()
3
  • 1
    "The following example code does not work" -> What does that mean? Do you get any errors? Or unexpected output (what output did you expect)? Commented May 22, 2014 at 7:55
  • "Does not work" always means "does not perform as I expected" but we do not know your expectation. So please state what you expected and describe the observations you've got instead. Commented May 22, 2014 at 8:00
  • Sorry the exception I get is "ValueError: Iterator global flags must be a list or tuple of strings" for this version. Commented May 22, 2014 at 8:52

2 Answers 2

2

use flat iter:

for axis in Histograms.flat:
   axis.set_yscale("log", nonposy='clip')
Sign up to request clarification or add additional context in comments.

Comments

0

Didn't you forgot to plot something ? look at matplotlib sample :

"""
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--')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.