plotim.py 12 KB

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