Reputation: 2326
I am using matplotlib to draw candlestick charts.
[Q] The default setting shows the high-low bar running through the open-close box. I would prefer to have the open-low box be "above" the line so that I only see high-max(open,close) and min(open,close)-low as lines. Is that possible? How?
I answered my own question and am posting my finding with the original question if it helps anyone else.
The only way to do this is by rewriting the candletsick() function to draw two lines instead of one. I have some sample code that I am using that does this. Always happy to hear better ways to do the same thing.
def fooCandlestick(ax, quotes, width=0.5, colorup='k', colordown='r',
alpha=1.0):
OFFSET = width/2.0
lines = []
boxes = []
for q in quotes:
t, op, cl, hi, lo = q[:5]
box_h = max(op, cl)
box_l = min(op, cl)
height = box_h - box_l
if cl>=op:
color = colorup
else:
color = colordown
vline_lo = Line2D(
xdata=(t, t), ydata=(lo, box_l),
color = 'k',
linewidth=0.5,
antialiased=True,
)
vline_hi = Line2D(
xdata=(t, t), ydata=(box_h, hi),
color = 'k',
linewidth=0.5,
antialiased=True,
)
rect = Rectangle(
xy = (t-OFFSET, box_l),
width = width,
height = height,
facecolor = color,
edgecolor = color,
)
rect.set_alpha(alpha)
lines.append(vline_lo)
lines.append(vline_hi)
boxes.append(rect)
ax.add_line(vline_lo)
ax.add_line(vline_hi)
ax.add_patch(rect)
ax.autoscale_view()
return lines, boxes
[Q] Does matplotlib support setting the width of the candle and also the spacing between each candlestick?
Thanks.
Upvotes: 3
Views: 2491
Reputation: 46530
I'm not sure about spacing, but:
alpha
kwarg to candlestick
. 1
should be opaque.width
kwarg, not sure what the units are, just play with them.For example:
candlestick(ax, quotes, width=0.5, alpha=1.0)
Ah, this answer adds empty data in between as a hack to increase the inter-candlestick spacing: https://stackoverflow.com/a/9713447/1730674
Upvotes: 1