I am using the matplotlib
scatterplot function to create the appearance of handles on vertical lines to delineate certain parts of a graph. However, in order to make them look correct, I need to be able to align the scatter plot marker to the left (for the left line / delineator) and / or right (for the right line / delineator).
Here's an example:
#create the figure
fig = plt.figure(facecolor = '#f3f3f3', figsize = (11.5, 6))
ax = plt. ax = plt.subplot2grid((1, 1), (0,0))
#make some random data
index = pandas.DatetimeIndex(start = '01/01/2000', freq = 'b', periods = 100)
rand_levels = pandas.DataFrame( numpy.random.randn(100, 4)/252., index = index, columns = ['a', 'b', 'c', 'd'])
rand_levels = 100*numpy.exp(rand_levels.cumsum(axis = 0))
ax.stackplot(rand_levels.index, rand_levels.transpose())
#create the place holder for the vertical lines
d1, d2 = index[25], index[50]
#draw the lines
ymin, ymax = ax.get_ylim()
ax.vlines([index[25], index[50]], ymin = ymin, ymax = ymax, color = '#353535', lw = 2)
#draw the markers
ax.scatter(d1, ymax, clip_on = False, color = '#353535', marker = '>', s = 200, zorder = 3)
ax.scatter(d2, ymax, clip_on = False, color = '#353535', marker = '<', s = 200, zorder = 3)
#reset the limits
ax.set_ylim(ymin, ymax)
ax.set_xlim(rand_levels.index[0], rand_levels.index[-1])
plt.show()
The code above gives me almost the graph I'm looking for, like this:
However, I'd like the leftmost marker (">") to be "aligned left" (i.e. shifted slightly to the right) so that the line is continued to the back of the marker Likewise, I'd like the rightmost marker ("<") to be "aligned right" (i.e. slightly shifted to the left). Like this:
Any guidance or suggestions on how to accomplish this in a flexible manner?
NOTE: In practice, my DataFrame
index is pandas.Datetime
not integers as I've provided for this simple example.