2

我已经实现了一个 Matplotlib 图,可以正确绘制,但是当我尝试使用 mpld3 模块在浏览器上实现它时,我收到一个错误,说日期时间不是 Json 可序列化的,我在 Google 上搜索过,但是找不到什么我一直在寻找。我的代码如下:

  connection = MySQLdb.connect ()

  # prepare a cursor object using cursor() method
  cursor = connection.cursor ()

  # execute the SQL query using execute() method.
  cursor.execute ()

  #fetch all of the rows from the query
  data = cursor.fetchall()
  ddt =  sorted(set(data))

  qwe = []
  for date in data:
      qwe.append(date[0]);  

  def generate_dates(start_date, end_date,dte):
  x= []
  a= []
  d= []
  td = datetime.timedelta(hours=24)
  current_date = start_date
  i = 0
  j = 0
  x.append(0)
  while current_date <= end_date:
      if str(current_date) in dte:
          x[j] += dte.count(str(current_date))
      else:
          x[j] += 0
      if i % 7 == 0:
          a.append(current_date)
          j = j+1
          x.append(0)
      current_date += td
      i = i+1
  return x,a
start_date = datetime.date(2017, 07, 26)
end_date = datetime.date.today()
#generate_dates(start_date,end_date,qwe)
x,a = str(generate_dates(start_date,end_date,qwe))


y = range(len(x))
plt.plot(x,color='green', linewidth = 2,marker='o',  markerfacecolor='blue', markersize=8)
plt.ylabel('Number of Bookings')
plt.xlabel('Date')
plt.xticks(y,a,rotation = 90,fontsize = 8)
mpld3.show()

这是我得到的错误:“datetime.date(2017, 7, 26) is not JSON serializable”

4

0 回答 0