3

I am trying to generate a multiple legend histogram(example). The problem is that the length(size) of the DataFrame is different. The following code would have worked if the size (30 and 10 in this example) were the same. Is there a way to still generate the histogram that I can compare multiple data series?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

orig = pd.DataFrame(np.random.random(30))
short = pd.DataFrame(np.random.random(10))
combine = pd.DataFrame({'orig' : orig, 'short' : short})

plt.figure()
h = combine.plot(kind='hist', logy=True)
f = h.get_figure()
f.savefig('figures/combined.png')
andwjstks
  • 643
  • 1
  • 6
  • 7
  • The line `combine = pd.DataFrame({'orig' : orig, 'short' : short})` is going to fail regardless if the arrays are the same length. I believe you want to combine your dataframes with something like `pd.concat([orig, short], axis=1)` Though you'll want to name your columns. The plot in the link was generated with `kind='bar'` not `kind='hist'` which would give a different result. Can you describe in a bit more detail what you are hoping the result to look like? Thanks! – johnchase Feb 04 '16 at 21:46
  • Also do you mean `np.random.random()` instead of `np.random()`? – johnchase Feb 04 '16 at 21:50
  • 1
    http://stackoverflow.com/questions/25539195/multiple-histograms-in-pandas/25539531#25539531 – Paul H Feb 04 '16 at 21:51
  • @johnchase Yes, I understand that the example was generated with kind='bar', and referencing to it to show how the two bar charts are presented in a single figure. Why I don't use the 'bar' chart is for the need of counting. – andwjstks Feb 04 '16 at 21:51
  • @PaulH Thank you for the link, which turns out to work for data series with different length. One issue that I forgot to mention was that I need to have the y-axis in a logarithmic scale. A rough search on numpy.histogram() seems to not have such option. – andwjstks Feb 04 '16 at 22:53
  • 1
    `numpy.histogram` doesn't draw anything, so log-scales are irrelevant there. You set log scales on the axes object i.e., `ax.set_yscale('log')` before drawing the bars. Note that by default the bottoms of the bars are set to zero, but log scales have no zero place, so you need to use the `bottom` kwarg in `ax.bar` – Paul H Feb 04 '16 at 22:58

0 Answers0