python - 如何使用mpl_finance设置一根烛台颜色?

标签 python matplotlib

这是代码:

import numpy as np
import matplotlib.pyplot as plt
import datetime

from mpl_finance import candlestick_ochl
from matplotlib.dates import num2date
%matplotlib notebook 

# data in a text file, 5 columns: time, opening, close, high, low
# note that I'm using the time you formated into an ordinal float
# data = np.loadtxt('finance-data.txt', delimiter=',')
data = np.array([[1, 2, 3, 4, 1],
        [2, 3, 1, 5, 1],
        [3, 4, 3, 5, 2.3]
        ])

# determine number of days and create a list of those days
ndays = np.unique(np.trunc(data[:,0]), return_index=True)
xdays =  []
for n in np.arange(len(ndays[0])):
    xdays.append(datetime.date.isoformat(num2date(data[ndays[1],0][n])))

# creation of new data by replacing the time array with equally spaced values.
# this will allow to remove the gap between the days, when plotting the data
data2 = np.hstack([np.arange(data[:,0].size)[:, np.newaxis], data[:,1:]])

# plot the data
fig = plt.figure(figsize=(10, 5))
ax = fig.add_axes([0.1, 0.2, 0.85, 0.7])
    # customization of the axis
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.tick_params(axis='both', direction='out', width=2, length=8,
               labelsize=12, pad=8)
ax.spines['left'].set_linewidth(2)
ax.spines['bottom'].set_linewidth(2)
    # set the ticks of the x axis only when starting a new day
ax.set_xticks(data2[ndays[1],0])
ax.set_xticklabels(xdays, rotation=45, horizontalalignment='right')

ax.set_ylabel('Quote ($)', size=20)
ax.set_ylim([0, 10])

drawData = candlestick_ochl(ax, data2, width=0.5, colorup='g', colordown='r')

这是输出:

enter image description here

我想把中间的一个改成橙色。我怎样才能找出刻度并改变它的颜色?谢谢。

最佳答案

matplotlib 中的大多数艺术家都有一个 .set_color()方法。

lines, rectangles = candlestick_ochl(ax, data2, width=0.5, colorup='g', colordown='r')

lines[1].set_color("orange")
rectangles[1].set_color("orange")

enter image description here

关于python - 如何使用mpl_finance设置一根烛台颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46326151/

相关文章:

python - 用 NA 值填充 dict 以允许转换为 pandas 数据帧

python - 了解 python 内存装饰器中的参数处理

matplotlib - matplotlib 图例中缺少标签

python - 在 map 上绘制点,但错误代码为 "ValueError: ' box_aspect' 且 'fig_aspect' 必须为正值"

python - 使用 Python 使用嵌套 for 循环对列表中的 T 进行计数

python - 在 python 中使用 OpenCV 分割图像

python - mod_wsgi python conf解析器

python - 如何为不同语言设置 matplotlib.dates.DateFormatter

python - 将 nltk 图保存到 headless (headless)服务器/虚拟机上的文件

python - 访问TKinter脚本中的主线程?