Re: [Matplotlib-users] I really can't get into hist() barstacked

2009-07-22 Thread ms
Sandro Tosi ha scritto:
 On Wed, Jul 22, 2009 at 10:25, msdeviceran...@gmail.com wrote:
 Sandro Tosi ha scritto:
 Hi all,
 I'd like to do a histogram with barstacked style. Well, I'm not able
 to make it in any way :(

 - what is the format of the the data to pass?
 - what's the value of bins? (related to the above question, I suppose)

 for example, let's say I want to plot this series

 s1 = 2,3,6,3,1
 s2 = 1,2,2,4,1
 s3 = 4,1,0,3,7

 what's the format of data to pass to hist() ? by row? by column?

 Thanks a lot in advance,
 I think hist() *computes* histograms ; you can find information here:

 http://matplotlib.sourceforge.net/api/pyplot_api.html
 
 and infact I want a barstacked histogram

So you want to
1) compute your histogram from your series of values and then
2) display the several histograms as a stacked bar graph

or just
1) display already-calculated histogram values as a stacked bar graph

These are two different things. Since it seems you want to pass directly
s1,s2,s3, it seems that they are your already calculated histogram
values. In this case hist() is of no utility for you.

 It seems from your example that you already have the histogram computed
 
 no, I just have a series of lists to plot, I didn't compute anything.

Ok, so you want a bar graph and not to compute a histogram.

 and that you want to plot the bar graph; in this case you should look to
  bar()

 In the page linked above you find also the example on how to do a
 barstacked graph.
 
 sure, for 2 data sets might be fine: but what for 10 ? how to use the
 bottom argument in that case?

Ehm, it seems pretty straighforward to me.
I attach the same example of the webpage but extended with three bar
graphs stacked. I admit that the fact the example used tuples for the
values made it unnecessarily awkward; using numpy arrays everything goes
to place:

#!/usr/bin/env python
# a stacked bar plot with errorbars
import numpy as np
import matplotlib.pyplot as plt


N = 5
menMeans   = np.array([20, 35, 30, 35, 27])
womenMeans = np.array([25, 32, 34, 20, 25])
xMeans = np.array([17, 11, 43, 5, 18])
menStd = (2, 3, 4, 1, 2)
womenStd   = (3, 5, 2, 3, 3)
ind = np.arange(N)# the x locations for the groups
width = 0.35   # the width of the bars: can also be len(x) sequence

p1 = plt.bar(ind, menMeans,   width, color='r', yerr=womenStd)
p2 = plt.bar(ind, womenMeans, width, color='y',
 bottom=menMeans, yerr=menStd)
p3 = plt.bar(ind, xMeans, width, color='b', bottom=womenMeans+menMeans)


plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(ind+width/2., ('G1', 'G2', 'G3', 'G4', 'G5') )
plt.yticks(np.arange(0,81,10))
plt.legend( (p1[0], p2[0], p3[0]), ('Men', 'Women','Aliens') )

plt.show()


--
___
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users


Re: [Matplotlib-users] I really can't get into hist() barstacked

2009-07-22 Thread ms
Sandro Tosi ha scritto:
 I got only data values, nothing already computed (for whatever you
 mean by that), and I want to plot stacked bars with those values
 (either by row or by column).

Ok, I see.

 It seems from your example that you already have the histogram computed
 no, I just have a series of lists to plot, I didn't compute anything.
 Ok, so you want a bar graph and not to compute a histogram.
 
 what is the difference? maybe I didn't get it right ;)

Oh, sorry, I thought it was common knowledge. That's why I didn't
understand at beginning :)

A histogram is a *statistical technique* which takes a vector of data,
classifies them into bins (usually magnitude intervals), counts the
number of data points into each bin and returns another vector with the
number of data points for each bin.

It is a technique to get an approximation of the distribution underlying
your data; it is not the only one (for example I am a big fan of kernel
density estimation, which is, roughly speaking, the continuous extension
of the histogram)

A bar graph is a representation of data as bars of different heights.
Usually people display histograms with bar graphs, each bar being a bin,
and for this reason they often confuse the two things :)

But they're two different and quite unrelated things: there's nothing
intrinsically wrong in plotting histograms as line or scatter plots, for
example, and you can do bar graphs of things which are not histograms.

So it seems you want a bar graph :),and that's why hist() is of little
help for you.

 In the page linked above you find also the example on how to do a
 barstacked graph.
 sure, for 2 data sets might be fine: but what for 10 ? how to use the
 bottom argument in that case?
 Ehm, it seems pretty straighforward to me.
 ...
 p3 = plt.bar(ind, xMeans, width, color='b', bottom=womenMeans+menMeans)
 
 do you really expect it to be straightforward for 10, 100 or for any
 other dynamically generated data? let's imagine today I get 15 sets of
 data, tomorrow 20, the next  day 7. I cannot do that by hand, and I
 want some general way to get stacked by dynamically adaptable to more
 columns with more blocks in them.

I didn't try, but isn't it easily done with a for cycle? Instead of
having p1,p2,p3 you have a vector pN=[...] where you append the bars,
and the bottom is the sum of the previous values of your bars.

Not counting the fact that if you have 100 data sets, maybe a stacked
bar graph begins to be confusing ;) but hey, that's your choice! :D

m.


--
___
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users