plotim.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. # # P L O T . I M # #
  2. # https://github.com/tbartos14/plotim
  3. # version 0.5.5, 5/22/2018
  4. # readme needs update for residual plots
  5. import tkinter as tk
  6. import math
  7. def vertical_text(text):
  8. newtext = ""
  9. for character in text:
  10. newtext += character + "\n"
  11. return newtext
  12. class linear_plot(object):
  13. def __init__(self, bordernorth=50, bordersouth=50, bordereast=30, borderwest=50,
  14. title="Linear Plot", \
  15. draw_lines=True, draw_points=False, ytitle="y", xtitle="x", line_colors=["#0000bb"],
  16. image=None):
  17. self.bordernorth = bordernorth
  18. self.bordersouth = bordersouth
  19. self.bordereast = bordereast
  20. self.borderwest = borderwest
  21. self.originalyaxistitle = ytitle
  22. self.yaxistitle = vertical_text(ytitle)
  23. self.xaxistitle = xtitle
  24. self.yaxis = [0, 10]
  25. self.xaxis = [0, 10]
  26. self.title = title
  27. self.draw_lines = draw_lines
  28. self.draw_points = draw_points
  29. self.linecolors = line_colors
  30. self.image = image
  31. def create_canvas(self, master):
  32. self.master = master
  33. self.canvas = tk.Canvas(self.master)
  34. self.canvas.bind("<Configure>", self.on_resize)
  35. self.windowx = self.canvas.winfo_reqwidth()
  36. self.windowy = self.canvas.winfo_reqheight()
  37. self.on_resize()
  38. return self.canvas
  39. def set_scale(self, xaxis, yaxis):
  40. self.yaxis = yaxis
  41. self.xaxis = xaxis
  42. def on_resize(self,event = None):
  43. if event:
  44. self.windowx = event.width - 4
  45. self.windowy = event.height - 4
  46. self.canvas.config(width=self.windowx, height=self.windowy)
  47. self.canvas.delete("all")
  48. self.graphx = self.windowx - self.bordereast - self.borderwest
  49. self.graphy = self.windowy - self.bordernorth - self.bordersouth
  50. self.canvas.create_text(self.borderwest + self.graphx / 2, self.bordernorth / 2, text=self.title)
  51. self.canvas.create_text(self.borderwest / 3, self.bordernorth + self.graphy / 2, text=self.yaxistitle)
  52. self.canvas.create_text(self.borderwest + self.graphx / 2, self.windowy - self.bordersouth / 3,
  53. text=self.xaxistitle)
  54. self.canvas.create_rectangle(self.bordernorth, self.borderwest, (self.windowx - self.bordereast),
  55. (self.windowy - self.bordersouth), fill="white", outline="white")
  56. # finding limits for axis
  57. self.yrange = abs(self.yaxis[1] - self.yaxis[0]) - 1
  58. self.xrange = abs(self.xaxis[1] - self.xaxis[0]) - 1
  59. # choosing what kind of scale to use
  60. self.yrangefactor = -round(math.log10(self.yrange) - 1)
  61. self.xrangefactor = -round(math.log10(self.xrange) - 1)
  62. # determining how many lines need to be placed
  63. # minimums/maximums for ease of reading
  64. self.yrangemin = ((int((self.yaxis[0]) * (10 ** (self.yrangefactor)))) / (10 ** (self.yrangefactor)))
  65. self.xrangemin = ((int((self.xaxis[0]) * (10 ** (self.xrangefactor)))) / (10 ** (self.xrangefactor)))
  66. self.yrangemax = ((int((self.yaxis[0]) * (10 ** (self.yrangefactor)))) / (10 ** (self.yrangefactor)) + (
  67. (int(self.yrange * (10 ** self.yrangefactor)) + 1) * (10 ** (-1 * self.yrangefactor))))
  68. self.xrangemax = ((int((self.xaxis[0]) * (10 ** (self.xrangefactor)))) / (10 ** (self.xrangefactor)) + (
  69. (int(self.xrange * (10 ** self.xrangefactor)) + 1) * (10 ** (-1 * self.xrangefactor))))
  70. # determining increments
  71. # finding if scales are appropriate
  72. self.additionalscaley = 0
  73. self.additionalscalex = 0
  74. # seeing if data needs more space (y)
  75. while True:
  76. if self.yaxis[1] > self.yrangemax:
  77. self.additionalscaley = self.additionalscaley + 1
  78. self.yrangemax = (
  79. (int((self.yaxis[0]) * (10 ** (self.yrangefactor)))) / (10 ** (self.yrangefactor)) + (
  80. (int(self.yrange * (10 ** self.yrangefactor)) + 1 + self.additionalscaley) * (
  81. 10 ** (-1 * self.yrangefactor))))
  82. else:
  83. break
  84. # (x)
  85. while True:
  86. if self.xaxis[1] > self.xrangemax:
  87. self.additionalscalex = self.additionalscalex + 1
  88. self.xrangemax = (
  89. (int((self.xaxis[0]) * (10 ** (self.xrangefactor)))) / (10 ** (self.xrangefactor)) + (
  90. (int(self.xrange * (10 ** self.xrangefactor)) + 1 + self.additionalscalex) * (
  91. 10 ** (-1 * self.xrangefactor))))
  92. else:
  93. break
  94. self.yincrement = int(self.yrange * (10 ** self.yrangefactor)) + self.additionalscaley + 1
  95. self.xincrement = int(self.xrange * (10 ** self.xrangefactor)) + self.additionalscalex + 1
  96. # now we determine y
  97. for increment in range(0, self.yincrement + 1):
  98. self.canvas.create_line(self.borderwest + 1,
  99. (self.windowy - self.bordersouth) - (increment / self.yincrement) * (
  100. self.windowy - self.bordernorth - self.bordersouth), \
  101. self.borderwest + self.graphx,
  102. (self.windowy - self.bordersouth) - (increment / self.yincrement) * (
  103. self.windowy - self.bordernorth - self.bordersouth), fill="#bbbbbb",
  104. dash=(2, 2), width=2 if increment % 5 == 0 else 1)
  105. self.canvas.create_text(self.borderwest - 12,
  106. (self.windowy - self.bordersouth) - (increment / self.yincrement) * (
  107. self.windowy - self.bordernorth - self.bordersouth), \
  108. text="{0:4.4}".format((int((self.yaxis[0]) * (10 ** (self.yrangefactor)))) / (
  109. 10 ** (self.yrangefactor)) + (
  110. (increment) * (10 ** (-1 * self.yrangefactor)))))
  111. # determining x
  112. for increment in range(0, self.xincrement + 1):
  113. self.canvas.create_line(
  114. self.bordersouth + (increment / self.xincrement) * (self.windowx - self.bordereast - self.borderwest),
  115. (self.windowy - self.bordersouth) - 1, \
  116. self.bordersouth + (increment / self.xincrement) * (self.windowx - self.bordereast - self.borderwest),
  117. (self.windowy - self.bordersouth - self.graphy), fill="#bbbbbb", dash=(2, 2), width=2 if increment % 5 == 0 else 1)
  118. self.canvas.create_text(
  119. self.borderwest + (increment / self.xincrement) * (self.windowx - self.bordereast - self.borderwest),
  120. (self.windowy - self.bordersouth) + 12, \
  121. text="{0:4.4}".format((int((self.xaxis[0]) * (10 ** (self.xrangefactor)))) / (10 ** (self.xrangefactor)) + (
  122. (increment) * (10 ** (-1 * self.xrangefactor)))))
  123. self.canvas.create_line(self.bordernorth, self.borderwest, self.bordernorth, (self.windowy - self.bordersouth))
  124. self.canvas.create_line(self.borderwest, (self.windowy - self.bordersouth), (self.windowx - self.bordereast),
  125. (self.windowy - self.bordersouth))
  126. return self.canvas
  127. def point_to_coords(self, x, y, offsetX=0, offsetY=0):
  128. return ((x - self.xrangemin) * (self.windowx - self.bordereast - self.borderwest) / (self.xrangemax - self.xrangemin) + self.borderwest + offsetX,
  129. (self.windowy - self.bordersouth) - (y - self.yrangemin) * (self.windowy - self.bordernorth - self.bordersouth) / (self.yrangemax - self.yrangemin) + offsetY)
  130. ovals = []
  131. images = []
  132. def plot_data(self, xs, ys):
  133. #self.canvas.delete("graph")
  134. # adding lines
  135. if self.draw_lines == True:
  136. self.canvas.delete("line")
  137. for plot in range(len(xs)):
  138. xpoints = xs[plot]
  139. ypoints = ys[plot]
  140. linecolor = self.linecolors[plot]
  141. coords = []
  142. for point in range(0, len(xpoints)):
  143. if not math.isnan(ypoints[point]):
  144. coord = self.point_to_coords(xpoints[point], ypoints[point])
  145. coords.append(coord[0])
  146. coords.append(coord[1])
  147. if len(coords) > 2:
  148. self.canvas.create_line(
  149. coords,
  150. fill=linecolor, tags="line", width=3)
  151. # adding points!
  152. if self.draw_points == True:
  153. while len(self.ovals) < len(xs):
  154. self.ovals.append([None] * len(xs[0]))
  155. for plot in range(len(xs)):
  156. xpoints = xs[plot]
  157. ypoints = ys[plot]
  158. for point in range(0, len(xpoints)):
  159. if math.isnan(ypoints[point]):
  160. self.canvas.delete(self.ovals[plot][point])
  161. self.ovals[plot][point] = None
  162. elif self.ovals[plot][point] != None:
  163. self.canvas.coords(
  164. self.ovals[plot][point],
  165. *self.point_to_coords(xpoints[point], ypoints[point], 3, 3),
  166. *self.point_to_coords(xpoints[point], ypoints[point], -3, -3),
  167. )
  168. else:
  169. self.ovals[plot][point] = self.canvas.create_oval(
  170. *self.point_to_coords(xpoints[point], ypoints[point], 3, 3),
  171. *self.point_to_coords(xpoints[point], ypoints[point], -3, -3),
  172. fill="white", tags="oval")
  173. # use an image?
  174. if self.image != None:
  175. while len(self.images) < len(xs):
  176. self.images.append([None] * len(xs[0]))
  177. for plot in range(len(xs)):
  178. xpoints = xs[plot]
  179. ypoints = ys[plot]
  180. pointimage = tk.PhotoImage(file=self.image)
  181. for point in range(0, len(xpoints)):
  182. if math.isnan(ypoints[point]):
  183. self.canvas.delete(self.images[plot][point])
  184. self.images[plot][point] = None
  185. elif self.images[plot][point] != None:
  186. self.canvas.coords(
  187. self.images[plot][point],
  188. *self.point_to_coords(xpoints[point], ypoints[point])
  189. )
  190. else:
  191. self.images[plot][point] = self.canvas.create_image(
  192. *self.point_to_coords(xpoints[point], ypoints[point]),
  193. image=pointimage, tags="image")
  194. return self.canvas
  195. if __name__ == "__main__":
  196. # Datetime,RecNbr,WS_mph_Avg,PAR_Den_Avg,WS_mph_S_WVT,WindDir_SD1_WVT,AirTF_Avg,Rain_in_Tot,RH,WindDir_D1_WVT
  197. #!/usr/bin/env python3
  198. import urllib.request
  199. import time
  200. xpoints = []
  201. ypoints = []
  202. time = 0
  203. newlines = range(0, 100)
  204. for line in newlines:
  205. xpoints.append(time)
  206. time = time + (1 / 60)
  207. ypoints.append(line)
  208. plot1 = linear_plot(line_of_best_fit=True, \
  209. ytitle="Temperature F\u00B0", \
  210. xtitle="Time (hours)", \
  211. title="Temperature in Durham NH Today", \
  212. line_color="#2222ff", \
  213. windowx=1000, \
  214. windowy=700, )
  215. plot1.set_data(xpoints, ypoints)
  216. plot1.plot_data()