Skip to content

Modified 'candlestick' function in response to request #2546 #3016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions lib/matplotlib/finance.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def plot_day_summary(ax, quotes, ticksize=3,


def candlestick(ax, quotes, width=0.2, colorup='k', colordown='r',
alpha=1.0):
alpha=1.0, coloredge='k', theme='light'):

"""

Expand All @@ -326,6 +326,8 @@ def candlestick(ax, quotes, width=0.2, colorup='k', colordown='r',
colorup : the color of the rectangle where close >= open
colordown : the color of the rectangle where close < open
alpha : the rectangle alpha level
coloredge : the color of the rectangle border
theme : specify if using light theme or dark theme (light theme uses a white grid bg, dark uses black grid bg)

return value is lines, patches where lines is a list of lines
added and patches is a list of the rectangle patches added
Expand All @@ -334,33 +336,43 @@ def candlestick(ax, quotes, width=0.2, colorup='k', colordown='r',

OFFSET = width/2.0

# Adjust candlestick body edges based upon theme
if theme == 'light':
coloredge = 'k'
coloredgeup = 'k'
coloredgedown = 'k'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of odd that the default behavior is to ignore explicitly passed kwargs. I think it would be better hove have all of the color kwargs default to None. If theme is None, then assume it is 'light'. Then, if coloredge is not None over ride the theme values.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, although looking this over I think I could probably remove the coloredge kwarg since it's either going to be black (if using a light theme) or the candlestick body color (if using a dark theme).

else:
coloredgeup = colorup
coloredgedown = colordown

lines = []
patches = []
for q in quotes:
t, open, close, high, low = q[:5]

# Make candlestick bodies fill based upon stock price movement for given period
if close>=open :
color = colorup
lower = open
height = close-open
vline = Line2D(xdata=(t, t), ydata=(low, high), color=coloredgeup, linewidth=0.5, antialiased=True, zorder=0)
else :
color = colordown
lower = close
height = open-close
vline = Line2D(xdata=(t, t), ydata=(low, high), color=coloredgedown, linewidth=0.5, antialiased=True, zorder=0)

vline = Line2D(
xdata=(t, t), ydata=(low, high),
color='k',
linewidth=0.5,
antialiased=True,
)
# Should look for a better way to do this
if theme != 'light':
coloredge = color

rect = Rectangle(
xy = (t-OFFSET, lower),
width = width,
height = height,
facecolor = color,
edgecolor = color,
edgecolor = coloredge,
zorder=1,
)
rect.set_alpha(alpha)

Expand Down