-1

我正在使用 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!"
4

2 回答 2

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

于 2019-06-10T13:01:24.353 回答
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!"  
于 2019-01-06T17:26:10.210 回答