Issue
I want to connect the orange dots to the blue dots.
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?
Solution
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?
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:
For reference, please see Matplotlib python connect two scatter plots with lines for each pair of (x,y) values?
Hope it is helpful!
Answered By - Life is Good
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.