问题描述
我想使用matplotlib将烛台的烛芯部分变黑吗?我在文档中找不到任何提及,但我已经看到一些图片示例,表明可以做到这一点。
这是我目前拥有的东西:
以下是灯芯被染成黑色的示例:
更新:
我使用了下面提供的解决方案,但略微更改了代码以删除烛台主体区域上的垂直线:
从matplotlib.lines导入Line2D
从matplotlib.patches导入矩形
def blackwickcandlestick(ax,quotes,width = 0.2,colorup ='# 00FF00',colordown ='#FF0000',
alpha = 1.0,shadowCol ='k',ochl = True):
偏移量=宽度/ 2.0
线= []
色块= []
用于报价中的q:
如果ochl:
t,打开,关闭,高,低= q [:5]
其他:
t,打开,高,低,关闭= q [:5]如果关闭> =打开,则
:
颜色=着色
较低=打开
高度=关闭-打开
垂直线= Line2D(
xdata =(t,t),ydata =(low,high),
color = colorup,#这与默认实现
linewidth = 0.5,
抗锯齿= True,
)
else:
color = colordown
low =关闭
高度=打开-关闭
vline = Line2D(
xdata =(t,t),ydata =(低,高),
color = colordown,#从默认更改为t实现
linewidth = 0.5,
antialiased = True,
)
rect =矩形(
xy =(t-偏移量,下),
width = width,
height = height,
facecolor = color,
edgecolor = color,
)
rect.set_alpha(alpha)
lines.append(vline)
补丁.append(rect)
ax.add_line(vline)
ax.add_patch(rect)
ax。 autoscale_view()
返回行,补丁
导入matplotlib.finance为mpl_finance
mpl_finance._candlestick = blackwickcandlestick
正如Paul所说,MCVE将帮助人们极大地帮助您。 / p>
但是-只需快速浏览一下matplotlib中烛台图的源代码,就可以看到它同时使用了colorup / colordown参数
因此,为了使它们成为不同的颜色,您很可能需要重新实现该方法和/或对基本实现进行猴子修补。 / p>
因此,在绘图模块中,请使用以下方式:
从matplotlib.lines导入Line2D
从matplotlib.patches导入矩形
def blackwickcandlestick(ax,引号,宽度= 0.2,colorup ='k',colordown ='r ',
alpha = 1.0,ochl = True):
偏移量=宽度/ 2.0
线= []
色块= []
用于报价中的q:
如果ochl:
t,打开,关闭,高,低= q [:5]
其他:
t,打开,高,低,关闭= q [:5]如果关闭> =打开,则
:
颜色=着色
较低=打开
高度=关闭-打开
否则:
颜色=降色
低= cl ose
height =打开-关闭
vline = Line2D(
xdata =(t,t),ydata =(low,high),
color ='k ',#这是唯一从默认实现更改的行
linewidth = 0.5,
antialiased = True,
)
rect = Rectangle(
xy =(t-偏移量,下),
width = width,
height = height,
facecolor = color,
edgecolor = color,
)
rect.set_alpha(alpha)
lines.append(vline)
patch.append(rect)
ax.add_line(vline)
ax.add_patch(recting )
ax.autoscale_view()
返回行,将
导入matplotlib.finance作为mpl_finance
mpl_finance。 _candlestick = blackwickcandlestick
然后在此模块的其他位置,您可以使用 mpl_finance.candlestick_ohlc (。 ..)
绘图功能。
I'm looking to make the wick portion of a candlestick stick black using matplotlib? I couldn't find any mention of it in the documentation, but I've seen pictoral examples showing that it can be done.
here's what I currently have:
here's an example of the wicks being colored black:
Update:
I used the solution provided below but changed the code slightly to remove the vertical line over the body area of the candlestick:
from matplotlib.lines import Line2D
from matplotlib.patches import Rectangle
def blackwickcandlestick(ax, quotes, width=0.2, colorup='#00FF00', colordown='#FF0000',
alpha=1.0, shadowCol='k', ochl=True):
OFFSET = width / 2.0
lines = []
patches = []
for q in quotes:
if ochl:
t, open, close, high, low = q[:5]
else:
t, open, high, low, close = q[:5]
if close >= open:
color = colorup
lower = open
height = close - open
vline = Line2D(
xdata=(t, t), ydata=(low, high),
color=colorup, # This changed from the default implementation
linewidth=0.5,
antialiased=True,
)
else:
color = colordown
lower = close
height = open - close
vline = Line2D(
xdata=(t, t), ydata=(low, high),
color=colordown, # This changed from the default implementation
linewidth=0.5,
antialiased=True,
)
rect = Rectangle(
xy=(t - OFFSET, lower),
width=width,
height=height,
facecolor=color,
edgecolor=color,
)
rect.set_alpha(alpha)
lines.append(vline)
patches.append(rect)
ax.add_line(vline)
ax.add_patch(rect)
ax.autoscale_view()
return lines, patches
import matplotlib.finance as mpl_finance
mpl_finance._candlestick = blackwickcandlestick
As Paul says, A MCVE would assist people in helping you greatly.
However - just having a quick glance at the source code for the candlestick plotting in matplotlib shows that it uses the colorup/colordown parameter for both the candle and the wick.
So in order to get them to be different colours you will most likely need to reimplement the method and/or monkey patch the base implementation.
So in your plotting module, use something along the lines of:
from matplotlib.lines import Line2D
from matplotlib.patches import Rectangle
def blackwickcandlestick(ax, quotes, width=0.2, colorup='k', colordown='r',
alpha=1.0, ochl=True):
OFFSET = width / 2.0
lines = []
patches = []
for q in quotes:
if ochl:
t, open, close, high, low = q[:5]
else:
t, open, high, low, close = q[:5]
if close >= open:
color = colorup
lower = open
height = close - open
else:
color = colordown
lower = close
height = open - close
vline = Line2D(
xdata=(t, t), ydata=(low, high),
color='k', # This is the only line changed from the default implmentation
linewidth=0.5,
antialiased=True,
)
rect = Rectangle(
xy=(t - OFFSET, lower),
width=width,
height=height,
facecolor=color,
edgecolor=color,
)
rect.set_alpha(alpha)
lines.append(vline)
patches.append(rect)
ax.add_line(vline)
ax.add_patch(rect)
ax.autoscale_view()
return lines, patches
import matplotlib.finance as mpl_finance
mpl_finance._candlestick = blackwickcandlestick
Then elsewhere in this module you can use the mpl_finance.candlestick_ohlc(...)
plotting functions.
这篇关于如何使用matplotlib为烛台的烛芯部分上色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!