acbart

Untitled

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