I am using Python with Pyxll to create a function in Excel which should return a timeline chart. Function takes two parameters -"names" and "dates"
when I tried to use this function in excel by selecting the range in "names" and "dates" columns, it throws "#num error. But I don't see any error on debugging the Python code. What could be the issue?
Here is my code:
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!"