使用PyXLL创建的Excel函数抛出#num错误

时间:2019-01-03 18:39:02

标签: python excel matplotlib pyxll

我正在将Python与Pyxll一起使用以在Excel中创建一个函数,该函数应返回时间线图。函数带有两个参数-“名称”和“日期”

当我尝试通过在“名称”和“日期”列中选择范围在excel中使用此功能时,它将引发“ #num错误。但是在调试Python代码时看不到任何错误。可能是问题?

这是我的代码:

 import matplotlib.pyplot as plt
 import numpy as np
 import matplotlib.dates as mdates
 from datetime import datetime

 from pyxll import xl_func

 @xl_func("str[] names, str[] dates")

def TimeLine_Plot(names, dates): 

  names = np.array(names)
  names = names.reshape(names.size)
  dates = np.array(dates)
  dates = dates.reshape(dates.size)

  dates = [datetime.strptime(ii, "%Y-%m-%dT%H:%M:%SZ") for ii in dates] 
  levels = np.array([-5, 5, -3, 3, -1, 1])
  fig, ax = plt.subplots(figsize=(8, 5))

  # Create the base line
  start = min(dates)
  stop = max(dates)
  ax.plot((start, stop), (0, 0), 'k', alpha=.5)

  for ii, (iname, idate) in enumerate(zip(names, dates)):
     level = levels[ii % 6]
     vert = 'top' if level < 0 else 'bottom'

     ax.scatter(idate, 0, s=100, facecolor='w', edgecolor='k', zorder=9999)
     # Plot a line up to the text
     ax.plot((idate, idate), (0, level), c='r', alpha=.7)
     # Give the text a faint background and align it properly
     ax.text(idate, level, iname,
         horizontalalignment='right', verticalalignment=vert, fontsize=14,
         backgroundcolor=(1., 1., 1., .3))

  ax.set(title="Matplotlib release dates")
  # Set the xticks formatting
  # format xaxis with 3 month intervals
  ax.get_xaxis().set_major_locator(mdates.MonthLocator(interval=3))
  ax.get_xaxis().set_major_formatter(mdates.DateFormatter("%b %Y"))
  fig.autofmt_xdate()

  # Remove components for a cleaner look
  plt.setp((ax.get_yticklabels() + ax.get_yticklines() +
       list(ax.spines.values())), visible=False)
  plt.show()

 return "Done!"

2 个答案:

答案 0 :(得分:0)

这是我的答案。请记住在excel单元格中使用日期格式=%Y-%m-%dT%H:%M:%SZ

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.dates as mdates
from datetime import datetime 
from pyxll import xl_func

@xl_func("str[] names, str[] dates") 
def TimeLine_Plot(names, dates):

     names = np.array(names)
     names = names.reshape(names.size)
     dates = np.array(dates)
     dates = dates.reshape(dates.size)

     dates = [datetime.strptime(ii, "%Y-%m-%dT%H:%M:%SZ") for ii in dates] 
     levels = np.array([-5, 5, -3, 3, -1, 1])
     fig, ax = plt.subplots(figsize=(8, 5))

     # Create the base line
     start = min(dates)
     stop = max(dates)
     ax.plot((start, stop), (0, 0), 'k', alpha=.5)

     for ii, (iname, idate) in enumerate(zip(names, dates)):
         level = levels[ii % 6]
         vert = 'top' if level < 0 else 'bottom'

         ax.scatter(idate, 0, s=100, facecolor='w', edgecolor='k', zorder=9999)
         # Plot a line up to the text
         ax.plot((idate, idate), (0, level), c='r', alpha=.7)
         # Give the text a faint background and align it properly
         ax.text(idate, level, iname,
         horizontalalignment='right', verticalalignment=vert, fontsize=14,
         backgroundcolor=(1., 1., 1., .3))

    ax.set(title="Matplotlib release dates")
    # Set the xticks formatting
    # format xaxis with 3 month intervals
    ax.get_xaxis().set_major_locator(mdates.MonthLocator(interval=3))
    ax.get_xaxis().set_major_formatter(mdates.DateFormatter("%b %Y"))
    fig.autofmt_xdate()

    # Remove components for a cleaner look
    plt.setp((ax.get_yticklabels() + ax.get_yticklines() +
          list(ax.spines.values())), visible=False)
    plt.show()

    return "Done!"  

答案 1 :(得分:0)

要传递日期,而不要使用“ str”类型,请使用“ date”类型,例如:

@xl_func("str[] names, date[] dates")    
def TimeLine_Plot(names, dates):
    ....

有关详细信息,请参见https://www.pyxll.com/docs/userguide/udfs.html#argument-types-and-return-types