1

I am looking to create a stacked histogram but I cannot seem to get it to work correctly. To explain I have three pieces of data I wish to plot:

  1. Number of Hits (y-axis)
  2. Position of the hit in m (the histogram bin the hit falls in) (the x axis)
  3. The type of hit that falls into the bin from (2).

So far I have been able to create this:

enter image description here

This is generated using the following code:

pl.figure()
stats[ column ].plot( kind='hist', alpha=0.7, bins = 13 )
pl.xticks( np.linspace( 0, 0.013, num = 13, endpoint = False ), rotation=70 )
pl.axvline( 0.003, color='r', linestyle='dashed', linewidth=2)
pl.ylabel( "Number of Hits" )
pl.title( "Number of Hits " )
pl.savefig( output_file_name, bbox_inches='tight' )
print( "Saved Figure to ", output_file_name )   

What I want to do is to have the values so that each bin is stacked. I have three types of hits that can occur. I would like to call them Hit1, Hit2, Hit3. They are all stored in a Pandas dataframe which has the following structure:

df =   #   |  value    | hit type | location 
     ----------------------------------------
     (int) | float(64) |  string  | float(64)

The column under the title value is not relevant other than it is part of the original pandas dataframe.

The problem I am running into is that I am unable to stack these varying data types. I can already plot the number of times a hit falls into a specific location (bin in the histogram) but I am unable to do this in a manner that leads to having three colors in the plot one for each "type" of hit that is found.

badrobit
  • 743
  • 1
  • 11
  • 25
  • http://stackoverflow.com/questions/27365601/how-to-label-the-bars-of-a-stacked-bar-plot-from-a-pandas-dataframe?rq=1 – Paul H Mar 11 '15 at 19:23
  • @PaulH its close but I cannot even get them to stack and I don't want to plot the entire DF just the value and location and group them by hit type – badrobit Mar 11 '15 at 19:25
  • start with a simpler, reproducible data structure and see how it differs from the DataFrame in the linked question. Seems like this is really not about plotting (we know that can be done) but more about how to filter your data and get it in the proper format. There are dozens of questions on SO about those two topics already. – Paul H Mar 11 '15 at 19:28

0 Answers0