0

I want to connect the orange dots to the blue dots. enter image description here

Data Used: (pulled it using CSV lib)

handler,first_test,handler_arrived
hvvm-02,7/15/2020,1/26/2020
hvvm-03,4/8/2020,1/26/2020
hvvm-04,5/27/2020,1/26/2020
hvvm-07,7/8/2020,1/26/2020
hvvm-09,2/26/2020,1/7/2020
hvvm-10,2/26/2020,1/7/2020
hvvm-11,7/1/2020,1/6/2020
hvvm-12,7/15/2020,1/6/2020

I convert a list of strings to datetimes

first_test = [dt.datetime.strptime(date, '%m/%d/%Y').date() for date in first_test]
handler_arrive = [dt.datetime.strptime(date, '%m/%d/%Y').date() for date in handler_arrive]

Then I plot them:

scat1 = ax.scatter(first_test, handlers)
scat2 = ax.scatter(handler_arrive, handlers)

I tried to connect them by calling: plt.plot_date(first_test, handler_arrive)

and this

plt.plot(first_test, handler_arrive, '-o')

I get this error

  File "C:\Users\user\PycharmProjects\handler_enablement\venv\lib\site-packages\matplotlib\dates.py", line 342, in _from_ordinalf
dt = dt.astimezone(tz)
TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'UnitData'

So I try plt.plot_date(first_test, handler_arrive, tz=None) (to try to shush the error) didn't work

At no point have I set a timezone nor did I need to, because I never got this error when messing with the datetimes before, so idk why this error occurs now. It must be how I am trying to connect the dots with that function call. It doesn't seem right...

I have tried these and they did not work

pandas scatter plotting datetime

Matplotlib connect scatterplot points with line - Python

What am I missing?

  • Could you post a few rows of data so we could try to recreate the error? – Life is Good Mar 03 '21 at 23:59
  • Gah sorry! Edited with sample data. – Glenn Oberlander Mar 04 '21 at 00:58
  • Thanks for the data. When I tried `plt.plot_date(first_test, handler_arrive)`, I did not incur an error, but I had `first_test` on the x-axis and `handler_arrive` on the y-axis. My understanding is that you are not trying to plot the two dates against each other, but trying to draw a line in between the two dots for each handler. I will see if I can find a solution. – Life is Good Mar 04 '21 at 02:03

1 Answers1

0

Thanks for posting your data. Below is the dataframe that I created based on the tab delimited version:

    handler first_test  handler_arrived
0   hvvm-02 7/15/2020   1/26/2020
1   hvvm-03 4/8/2020    1/26/2020
2   hvvm-04 5/27/2020   1/26/2020
3   hvvm-07 7/8/2020    1/26/2020
4   hvvm-09 2/26/2020   1/7/2020
5   hvvm-10 2/26/2020   1/7/2020
6   hvvm-11 7/1/2020    1/6/2020
7   hvvm-12 7/15/2020   1/6/2020

Then, I created the data series to use for plotting using mostly your code:

handlers = df['handler']
first_test = [dt.datetime.strptime(date, '%m/%d/%Y').date() for date in df['first_test']]
handler_arrive = [dt.datetime.strptime(date, '%m/%d/%Y').date() for date in df['handler_arrived']]

When I did the scatter plot with plt.plot_date(first_test, handler_arrive), I did not incur any error. Could the (delta: xx days) part be a complication in the plotting?

enter image description here

I also did a plot connecting the two groups of dates using the following code:

plt.rcdefaults()
fig, ax = plt.subplots()
scat1 = ax.scatter(first_test, handlers)
scat2 = ax.scatter(handler_arrive, handlers)
for i in range(len(first_test)):     
    plt.plot([first_test[i],handler_arrive[i]], [handlers[i],handlers[i]], color='black')

The result is below:

enter image description here

For reference, please see Matplotlib python connect two scatter plots with lines for each pair of (x,y) values?

Hope it is helpful!

Life is Good
  • 106
  • 9
  • Your for loop was the answer! `plt.plot([first_test[i],handler_arrive[i]], [handlers[i],handlers[i]], color='black')` I had a feeling I wasn't accessing the lines correctly. When I look at the `plot` function, it doesn't give me much info on parameters, so it seems your code iterates to each date in the plot along the y axis of each handler. Is that correct? And thanks again for the answer! Was eating at me. – Glenn Oberlander Mar 04 '21 at 15:56
  • Yeah. Seems like `plt.plot` plots more than lines - it also plots points in a scatterplot. I think what you described is correct. `plt.plot` takes in a pair of coordinates for the x-axis and a pair for the y-axis, and the pair of coordinates for the y-axis are the same coordinate, which is what we want. Honestly, I also did not know this, so thank you for asking this question, I learned something new today! – Life is Good Mar 04 '21 at 16:45
  • 1
    Haha, well then we both learned! :) To add, in this case, the set of y coordinates are the same, but maybe in other cases they could be different? I can't think of a situation, but I'll have to test. – Glenn Oberlander Mar 05 '21 at 17:20