Advertisement
Guest User

Untitled

a guest
May 28th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. #!/usr/bin/python
  2. # matplotlib pyplot module
  3. import MySQLdb
  4. import matplotlib
  5. matplotlib.use('Agg')
  6. import matplotlib.pyplot as plt
  7.  
  8. # connect to MySQL database
  9. conn = MySQLdb.connect(host="localhost", user="xx", passwd="xxxx", db="r1array")
  10.  
  11. # prepare a cursor
  12. cur = conn.cursor()
  13.  
  14. # this is the query we will be making
  15. query = """
  16. SELECT TimeStamp,Pac
  17. FROM SolarData
  18. WHERE TimeStamp >= "2014-01-03"
  19. AND TimeStamp < "2014-01-04";
  20. """
  21.  
  22. # execute the query
  23. cur.execute(query)
  24.  
  25. # retrieve the whole result set
  26. data = cur.fetchall()
  27.  
  28. # close cursor and connection
  29. cur.close()
  30. conn.close()
  31.  
  32. # unpack data in TimeStamp (x axis) and Pac (y axis)
  33. TimeStamp, Pac = zip(*data)
  34.  
  35. # graph code
  36. plt.bar(TimeStamp, Pac, align='center')
  37.  
  38. # set title, X/Y labels
  39. plt.title("PVIC R1 panel")
  40. plt.xlabel("Time of Day")
  41. plt.ylabel("Pac")
  42. fig = plt.gcf()
  43.  
  44. # plt.xticks(TimeStamp, (hour))
  45. fig.set_size_inches(20.5,10.5)
  46. plt.grid(True)
  47. plt.draw()
  48. fig.savefig('test2.png', dpi=100)
  49.  
  50. Traceback (most recent call last):
  51. File "python_sql_image.py", line 41, in <module>
  52. plt.savefig('test.png')
  53. File "/usr/lib/python2.6/site-packages/matplotlib/pyplot.py", line 356, in savefig
  54. return fig.savefig(*args, **kwargs)
  55. File "/usr/lib/python2.6/site-packages/matplotlib/figure.py", line 1032, in savefig
  56. self.canvas.print_figure(*args, **kwargs)
  57. File "/usr/lib/python2.6/site-packages/matplotlib/backend_bases.py", line 1476, in print_figure
  58. **kwargs)
  59. File "/usr/lib/python2.6/site-packages/matplotlib/backends/backend_agg.py", line 358, in print_png
  60. FigureCanvasAgg.draw(self)
  61. File "/usr/lib/python2.6/site-packages/matplotlib/backends/backend_agg.py", line 314, in draw
  62. self.figure.draw(self.renderer)
  63. File "/usr/lib/python2.6/site-packages/matplotlib/artist.py", line 46, in draw_wrapper
  64. draw(artist, renderer, *kl)
  65. File "/usr/lib/python2.6/site-packages/matplotlib/figure.py", line 773, in draw
  66. for a in self.axes: a.draw(renderer)
  67. File "/usr/lib/python2.6/site-packages/matplotlib/artist.py", line 46, in draw_wrapper
  68. draw(artist, renderer, *kl)
  69. File "/usr/lib/python2.6/site-packages/matplotlib/axes.py", line 1735, in draw
  70. a.draw(renderer)
  71. File "/usr/lib/python2.6/site-packages/matplotlib/artist.py", line 46, in draw_wrapper
  72. draw(artist, renderer, *kl)
  73. File "/usr/lib/python2.6/site-packages/matplotlib/axis.py", line 736, in draw
  74. for tick, loc, label in self.iter_ticks():
  75. File "/usr/lib/python2.6/site-packages/matplotlib/axis.py", line 680, in iter_ticks
  76. majorLabels = [self.major.formatter(val, i) for i, val in enumerate(majorLocs)]
  77. File "/usr/lib/python2.6/site-packages/matplotlib/dates.py", line 417, in __call__
  78. return self._formatter(x, pos)
  79. File "/usr/lib/python2.6/site-packages/matplotlib/dates.py", line 293, in __call__
  80. dt = num2date(x, self.tz)
  81. File "/usr/lib/python2.6/site-packages/matplotlib/dates.py", line 249, in num2date
  82. if not cbook.iterable(x): return _from_ordinalf(x, tz)
  83. File "/usr/lib/python2.6/site-packages/matplotlib/dates.py", line 170, in _from_ordinalf
  84. dt = datetime.datetime.fromordinal(ix)
  85. ValueError: ordinal must be >= 1
  86.  
  87. ValueError: ordinal must be >= 1
  88.  
  89. TimeStamp | Pac |
  90. +---------------------+------+
  91. | 2014-01-03 07:55:02 | 0 |
  92. | 2014-01-03 08:00:02 | 0 |
  93. | 2014-01-03 08:05:02 | 2 |
  94. | 2014-01-03 08:10:02 | 11 |
  95. | 2014-01-03 08:15:03 | 72 |
  96. | 2014-01-03 08:20:04 | 162 |
  97. | 2014-01-03 08:25:04 | 307 |
  98. | 2014-01-03 08:30:04 | 464 |
  99. | 2014-01-03 08:40:01 | 665 |
  100. | 2014-01-03 08:45:01 | 764 |
  101. | 2014-01-03 08:50:02 | 844 |
  102. | 2014-01-03 08:55:01 | 931 |
  103. | 2014-01-03 09:00:02 | 1017 |
  104. | 2014-01-03 09:05:02 | 1098 |
  105. | 2014-01-03 09:10:01 | 1176 |
  106. | 2014-01-03 09:15:02 | 1272 |
  107. | 2014-01-03 09:20:01 | 1351 |
  108. | 2014-01-03 09:25:02 | 1432 |
  109. | 2014-01-03 09:30:01 | 1528 |
  110. | 2014-01-03 09:35:02 | 1653 |
  111. | 2014-01-03 09:40:02 | 1738 |
  112. | 2014-01-03 09:45:01 | 1821 |
  113. | 2014-01-03 09:50:02 | 1895 |
  114. | 2014-01-03 09:55:01 | 2015 |
  115. | 2014-01-03 10:00:02 | 2153 |
  116. | 2014-01-03 10:05:01 | 2300 |
  117. | 2014-01-03 10:10:02 | 2482 |
  118. | 2014-01-03 10:15:02 | 2635 |
  119. | 2014-01-03 10:20:01 | 2750 |
  120. | 2014-01-03 10:25:02 | 2923 |
  121. | 2014-01-03 10:30:01 | 3059 |
  122. | 2014-01-03 10:35:02 | 3201 |
  123. | 2014-01-03 10:40:01 | 3275 |
  124. | 2014-01-03 10:45:02 | 3360 |
  125.  
  126. import matplotlib.pyplot as plt
  127. import pandas
  128. import MySQLdb
  129.  
  130. # connect to MySQL database
  131. conn = MySQLdb.connect(host="localhost", user="xx", passwd="xxxx", db="r1array")
  132.  
  133.  
  134. # this is the query we will be making
  135. query = """
  136. SELECT TimeStamp, Pac
  137. FROM SolarData
  138. WHERE TimeStamp >= "2014-01-03"
  139. AND TimeStamp < "2014-01-04";
  140. """
  141.  
  142. df = pandas.read_sql(query, conn, index_col=['TimeStamp'])
  143. fig, ax = plt.subplots()
  144. df.plot(ax=ax)
  145. conn.close()
  146.  
  147. import numpy as np
  148. import matplotlib.pyplot as plt
  149. import matplotlib.dates as mdates
  150.  
  151.  
  152. Pac = [
  153. 0,0,2,11,72,162,307,464,665,764,844,931,1017,1098,1176,1272,1351,1432,
  154. 1528,1653,1738,1821,1895,2015,2153,2300,2482,2635,2750,2923,3059,3201,
  155. 3275,3360
  156. ]
  157. TimeStamp = mdates.num2date(mdates.datestr2num([
  158. '2014-01-03 07:55:02', '2014-01-03 08:00:02', '2014-01-03 08:05:02', '2014-01-03 08:10:02',
  159. '2014-01-03 08:15:03', '2014-01-03 08:20:04', '2014-01-03 08:25:04', '2014-01-03 08:30:04',
  160. '2014-01-03 08:40:01', '2014-01-03 08:45:01', '2014-01-03 08:50:02', '2014-01-03 08:55:01',
  161. '2014-01-03 09:00:02', '2014-01-03 09:05:02', '2014-01-03 09:10:01', '2014-01-03 09:15:02',
  162. '2014-01-03 09:20:01', '2014-01-03 09:25:02', '2014-01-03 09:30:01', '2014-01-03 09:35:02',
  163. '2014-01-03 09:40:02', '2014-01-03 09:45:01', '2014-01-03 09:50:02', '2014-01-03 09:55:01',
  164. '2014-01-03 10:00:02', '2014-01-03 10:05:01', '2014-01-03 10:10:02', '2014-01-03 10:15:02',
  165. '2014-01-03 10:20:01', '2014-01-03 10:25:02', '2014-01-03 10:30:01', '2014-01-03 10:35:02',
  166. '2014-01-03 10:40:01', '2014-01-03 10:45:02'
  167. ]))
  168.  
  169. fig = plt.figure()
  170. ax = fig.add_subplot()
  171. ax.bar(TimeStamp, Pac, align='center', width=0.002)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement