Reputation: 911
I would like to know how to properly handle unix time stamps and plot them as a date string with a proper x-axis scale (relative to the x-axis time frame eg. minutes, hours, days...).
I have a candlestick plot with a numerical timestamp (using the unix epoch). It looks like this with samples 120 seconds apart:
with this code:
import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter, epoch2num
from matplotlib.finance import candlestick
results=[[1388898634, 24.339, 24.24389, 24.376, 24.22290666666667, 24.247343013888855, 1724.3410760000002], [1388898514, 24.2444, 24.26181818181818, 24.28, 24.2444, 24.25408960227278, 1875.784091], [1388898394, 24.262727272727272, 24.310416666666665, 24.310416666666665, 24.262727272727272, 24.295351803977244, 389.329334], [1388898274, 24.310499999999998, 24.3996, 24.3996, 24.310499999999998, 24.33321454274891, 536.4064960000003], [1388898154, 24.3998, 24.45051724137931, 24.45051724137931, 24.3998, 24.423427643678153, 313.12763599999994]]
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
candlestick(ax, results, width=20)
ax.autoscale_view()
plt.setp( plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
plt.show()
Now I would like to plot this with date format, so I tried moving it to the proper epoch:
i=0;
for data in results:
results[i][0] = epoch2num(data[0])
i+=1
Which for compactness results in this:
results=[[735238.21567129635, 24.339, 24.24389, 24.376, 24.22290666666667, 24.247343013888855, 1724.3410760000002], [735238.21428240743, 24.2444, 24.26181818181818, 24.28, 24.2444, 24.25408960227278, 1875.784091], [735238.2128935185, 24.262727272727272, 24.310416666666665, 24.310416666666665, 24.262727272727272, 24.295351803977244, 389.329334], [735238.21150462958, 24.310499999999998, 24.3996, 24.3996, 24.310499999999998, 24.33321454274891, 536.4064960000003], [735238.21011574077, 24.3998, 24.45051724137931, 24.45051724137931, 24.3998, 24.423427643678153, 313.12763599999994]]
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
candlestick(ax, results, width=20)
ax.autoscale_view()
plt.setp( plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
plt.show()
But the candlestick plot is destroyed:
I also tried mapping the x-axis with a formatter like so:
results=[[735238.21567129635, 24.339, 24.24389, 24.376, 24.22290666666667, 24.247343013888855, 1724.3410760000002], [735238.21428240743, 24.2444, 24.26181818181818, 24.28, 24.2444, 24.25408960227278, 1875.784091], [735238.2128935185, 24.262727272727272, 24.310416666666665, 24.310416666666665, 24.262727272727272, 24.295351803977244, 389.329334], [735238.21150462958, 24.310499999999998, 24.3996, 24.3996, 24.310499999999998, 24.33321454274891, 536.4064960000003], [735238.21011574077, 24.3998, 24.45051724137931, 24.45051724137931, 24.3998, 24.423427643678153, 313.12763599999994]]
xfmt = DateFormatter('%Y-%m-%d %H:%M:%S')
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.xaxis.set_major_formatter(xfmt)
candlestick(ax, results, width=20)
ax.xaxis_date()
ax.autoscale_view()
plt.setp( plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
plt.show()
But on top of the mangled candlestick it also scales the datetime axis for way too long of a time span.
Why does my candlestick plot get destroyed when I remap the timestamp? How do I properly scale the x-axis when using dates (in this case I need to show minute increments, but I might want to span a few days or months depending on the sample rates)?
Thanks!
Upvotes: 3
Views: 1630
Reputation: 369074
width=1
of candlestick
function represent 1 days, not 1 pixel.
According to the docstring of candlestick
:
candlestick(ax, quotes, width=0.2, colorup='k', colordown='r', alpha=1.0)
...
width : fraction of a day for the rectangle width
Replace it with small value like 30 / 86400.0
(to represent 30 seconds):
candlestick(ax, results, width=30/86400.0)
Upvotes: 3