Good day,
I am trying to plot two arrays (timesin
and locations
) as a scatter of points. However, because timesin
is a datetime object (of which I want time only), I find that I can only plot it properly using pyplot.plot()
, not pyplot.scatter()
. The issue arrises when I want to color the points on this plot with a third variable, idx
. I know pyplot.scatter()
is capable of doing this quite easily, but I don't know how to do it with pyplot.plot()
.
My excerpt of code:
import os
import tempfile
from datetime import datetime
import numpy as np
os.environ['MPLCONFIGDIR'] = tempfile.mkdtemp()
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
pp = PdfPages('output.pdf')
names = ['WestHall', 'Elevator', 'EastHall', 'MathLounge']
locations = np.arange(4)+1
plt.scatter(timesin, locations, c=idx, marker="o")
plt.xlabel("Time of day")
plt.ylabel("Location")
plt.yticks(np.arange(4)+1, names)
plt.gcf().autofmt_xdate()
pp.savefig()
plt.close()
pp.close()
When I try this, I get an error, because it tries to interpret idx
as rgba:
ValueError: to_rgba: Invalid rgba arg "[...]"
number in rbg sequence outside 0-1 range
How do I get it to interpret idx
as conditional coloring without using pyplot.scatter()
?
Thanks
Update:
As suggested by Hun, I actually can use pyplot.scatter()
in this context by converting the datetime objects to numbers using matplotlibs dates library. Thus, figuring out how to use pyplot.plot()
for conditional coloring was unnecessary.