Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # First, we organize the data you've collected into a series of lists
- young_data = [cigyoungnone, cigyoungfew, cigyoungmid, cigyoungmany]
- teen_data = [cigteennone, cigteenfew, cigteenmid, cigteenmany ]
- adult_data = [cigadultnone, cigadultfew, cigadultmid, cigadultmany]
- # We make a list for each kind of category that you have
- age_categories = ["1-14","15-17","18+"]
- frequency_categories = ["None", "Few", "Mid", "Many"]
- # I've shown you "xrange", which is used to build up a list of numbers from a
- # length:
- # > xrange(5)
- # [0,1,2,3,4]
- # The NumpPy library (which you have never seen in this class) has a similar
- # thing called "arange", which also builds up a list but with extra abilities:
- # > arange(5) + .5
- # [0.5, 1.5, 2.5, 3.5, 4.5]
- # > arange(5) + .25
- # [0.25, 1.25, 2.25, 3.25, 4.25]
- from numpy import arange
- indexes = arange(len(frequency_categories))
- width = 0.25
- # Why does this matter? Because of the way that MatPlotLib places bars.
- # Essentially, it's stupid and needs to be told where you want to place each
- # bar. Remember in Algebra how your graph is a grid, and your x-axis is a
- # line of numbers stretching from 0 to infinity, with each tick being 1?
- # https://www.mathsisfun.com/data/images/x-y-axes.gif
- # Well, that's what is happening here. With only one series of bars, you tell
- # Matplotlib to put them at the 0, 1, 2, ... on the x-axis, and each bar
- # defaults to a width of 1 unit.
- # When you want to plot multiple, you must horizontally shrink the width of
- # these bars to be less than 1 (0.25 is a good choice when you have three
- # bars, so that there's a bit of empty space on the right) and space them out
- # so that the first series is at [0, 1, 2, ...], the second series is at
- # [0.25, 1.25, 2.25, ...], and so on. In other words, you are offsetting
- # each list of positions by a few decimal places.
- # So below, you will notice that each time we call plt.bar, we pass in the
- # "indexes" and add an offset using "width" (which is the reason for using
- # the special "arange" function instead of "xrange". Then we pass in the
- # actual data to plot, the width of the bars, and the color of that bar.
- # How did I know how to do all this? I stole it mostly from the documentation!
- # http://matplotlib.org/examples/api/barchart_demo.html
- young_plot = plt.bar(indexes, young_data, width, color='r')
- teen_plot = plt.bar(indexes+width, teen_data, width, color='y')
- adult_plot = plt.bar(indexes+2*width, adult_data, width, color='b')
- # We offset the labels on each x tick so that they are under the center of the
- # of the bars (thse are the frequencies, e.g., "None", "Many"). Remember,
- # these x ticks are the units of the number line!
- plt.xticks(indexes+0.5, frequency_categories)
- # The labels you know and love
- plt.xlabel('Frequency')
- plt.ylabel('# of respondents')
- plt.title('Cigarettes')
- # For clarity, you should add a legend. This looks weirder than it is.
- # The first argument are the plots generated by using plt.bar (which actually
- # returns multiple values like your new cigyoung_over_days function,
- # except we only care about the first value from that.
- # The second argument is the labels for each of those plots.
- # Then we also specify a title for the legend.
- plt.legend( (young_plot[0], teen_plot[0], adult_plot[0]),
- age_categories, title='Age at first use' )
- # Show and we're done!
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment