Browse Source

fix previous

ming 3 years ago
parent
commit
dbecca1f97
1 changed files with 29 additions and 11 deletions
  1. 29 11
      newbot.py

+ 29 - 11
newbot.py

@@ -12,6 +12,13 @@ import matplotlib.dates as mdates
 import matplotlib.pyplot as plt
 from starlette.responses import StreamingResponse
 from fastapi.responses import FileResponse
+
+import numpy as np
+from pylab import plt
+from scipy.interpolate import make_interp_spline
+from matplotlib import dates
+import dataset
+import matplotlib.dates as mdates
 #
 
 
@@ -154,18 +161,29 @@ async def get_trend_image(kw):
     for row in db.query(sql_get_newest_data_date):
         x_axis += [row['iot_date']]
         y_axis += [row['iot_value']]
+
+    x = np.array(x_axis)
+    y = np.array(y_axis)
+
         
-    #fomat example
-    #x = ['7/15', '7/18', '7/19', '7/24', '7/25', '7/26']
-    #y = [1, 5, 2, 7, 9, 1]
-
-    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y/%m/%d'))
-    plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=30)) #座標軸刻度1天
-    print(kw)
-    print(x_axis[:3])
-    print(y_axis[:3])
-    target_path = 'trend_image/'+kw+".png"
-    plt.plot(x_axis[:3], y_axis[:3],'r-^') # 設定樣式
+    # create an array of numbers for the dates
+    x_dates = np.array([dates.date2num(i) for i in x])
+
+
+    # create more uniform intervals in x axis and use spline to interpolate data
+    x_smooth = np.linspace(x_dates.min(), x_dates.max(), 200)
+    #y_smooth = spline(x_dates, y, x_smooth)
+    y_smooth = make_interp_spline(x_dates, y)(x_smooth)
+    # creating a new date array from the new date number array
+    x_new = np.array([dates.num2date(i) for i in x_smooth])
+
+    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y/%m'))
+    plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=100)) #座標軸刻度1天
+
+    #plt.figure()
+    plt.plot(x_new, y_smooth)
+    target_path = 'trend_image/'+kw+'.png'
+    plt.savefig(target_path))
     plt.savefig(target_path, bbox_inches='tight') #存檔,第二個參數表示把圖表外多餘的空間刪除
     
     return FileResponse(target_path)