I need to update a plot with interact
and for that I use something like
fig, ax = plt.subplots()
df = pd.DataFrame({'x':[some data x],'y':[some data y]}
p, = ax.plot(df_temp.x,df_temp.y,'.-')
ax.set_xticks(x[::5])
plt.setp(ax.xaxis.get_majorticklabels(), rotation=45, fontsize=6)
@interact()
def update(i):
df = pd.DataFrame(new data)
p.set_xdata(df_temp.x)
p.set_ydata(df_temp.y)
ax.relim()
ax.autoscale_view()
ax.set_xticks(x[::5])
fig.canvas.draw_idle()
which works fine when I have datetime
type for x
. But I changed it for string with x = [datetime.strftime(y,"%Y-%m-%d %H:%M:%S")[:-3] for y in datetimedata]
because the plot had big gaps due to business days and times, which was not ideal. With the string x data, however, the gaps disappear but now, on some occasions, the plot is out of order, as can be seen here:
data points 0,1 and 2 are out of order
I know the data is still in the right order because I can print the dataframe and see the data is sorted. Also, when I create a new figure and axis and plot the data without the interact
it shows in the right order too:
x data in the right order plotted without ax.set_xdata
and interact
EDIT
The x
column is already sorted, but the plot is out of order in the first picture. The first 3 rows are plotted on the right side of the chart.
x y
0 2013-10-07 12:30 -0.008961
1 2013-10-07 13:30 0.003846
2 2013-10-07 14:30 -0.043890
3 2013-10-07 15:30 0.013083
4 2013-10-07 16:30 0.043967
.. ... ...
85 2013-10-18 16:30 -0.036062
86 2013-10-18 17:30 -0.036062
87 2013-10-21 09:30 -0.015806
88 2013-10-21 10:30 -0.059842
89 2013-10-21 11:30 -0.132455