bar-chart-grouping.tex 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. % Source: http://tex.stackexchange.com/a/69234/5645
  2. \documentclass[varwidth=true, border=2pt]{standalone}
  3. \usepackage{pgfplots}
  4. \usepackage{tikz}
  5. \usetikzlibrary{arrows, positioning, calc}
  6. \begin{document}
  7. \newcommand\shift{1.5}
  8. \begin{tikzpicture}[scale=1]
  9. \begin{axis}[
  10. ybar,
  11. ylabel={vaule},
  12. ylabel style={at={(0.02,0.5)}, color=red},
  13. xlabel={items},
  14. legend style={at={(0.5,-0.15)},
  15. anchor=north,legend columns=-1},
  16. width=0.8*\textwidth,
  17. height=9cm,
  18. bar width=7pt,
  19. symbolic x coords={
  20. $x_1$,$x_2$,$x_3$,$x_4$,$x_5$,$x_6$, $x_7$, $x_8$,
  21. $x_9$, $x_{10}$, $x_{11}$, $x_{12}$, $x_{13}$,
  22. $x_{14}$, $x_{15}$
  23. },
  24. xtick=data,
  25. scaled y ticks=base 10:-3,
  26. axis y line*=left,
  27. %nodes near coords,
  28. %nodes near coords align={vertical},
  29. ]
  30. \addplot[red, fill=red!15, shift={(-\shift,0)}] coordinates {
  31. ($x_1$,96000) ($x_2$,126000) ($x_3$,115000)
  32. ($x_4$,125000) ($x_5$,123000) ($x_6$, 123000)
  33. ($x_7$, 112000) ($x_8$, 111000) ($x_9$, 110000)
  34. ($x_{10}$, 110000) ($x_{11}$, 120000)
  35. ($x_{12}$, 98000) ($x_{13}$, 130000)
  36. ($x_{14}$, 87000) ($x_{15}$, 97000)};
  37. \end{axis}
  38. \begin{axis}[
  39. ybar,
  40. ylabel={weight},
  41. ylabel style={at={(1.25,0.5)}, color=blue},
  42. legend style={at={(0.7,-0.15)},
  43. anchor=north,legend columns=-1},
  44. width=0.8*\textwidth,
  45. height=9cm,
  46. bar width=7pt,
  47. symbolic x coords={
  48. $x_1$,$x_2$,$x_3$,$x_4$,$x_5$,$x_6$, $x_7$, $x_8$,
  49. $x_9$, $x_{10}$, $x_{11}$, $x_{12}$, $x_{13}$,
  50. $x_{14}$, $x_{15}$
  51. },
  52. xtick=data,
  53. %nodes near coords,
  54. %nodes near coords align={vertical},
  55. axis y line*=right,
  56. axis x line=none,
  57. ]
  58. \addplot +[shift={(\shift,0)}] coordinates {($x_1$,27)
  59. ($x_2$,21) ($x_3$,27) ($x_4$,15) ($x_5$,19)
  60. ($x_6$, 46) ($x_7$, 47) ($x_8$, 32) ($x_9$, 14)
  61. ($x_{10}$, 20) ($x_{11}$, 50) ($x_{12}$, 19)
  62. ($x_{13}$, 22) ($x_{14}$, 50) ($x_{15}$, 46)};
  63. \end{axis}
  64. \end{tikzpicture}
  65. \end{document}