本文介绍了使用matplotlib或使用Python3绘制颜色数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用OHLC值绘制蜡烛图.但是我现在只愿意绘制蜡烛的颜色.
我试图预测收盘价,并使用matplotlib进行绘制.请参阅以下内容:

I am trying to plot the candles using the OHLC values. But I am now willing to plot only the color of the candles.
I am trying to predict the close price and plotting it using the matplotlib. See the following:

plt.figure(figsize=(21,7))
plt.plot(yTest,label='Price',color='blue')
plt.plot(test_pred_list,label='Predicted',color='red')
plt.title('Price vs Predicted')
plt.legend(loc='upper left')
plt.show()

我愿意实现的是绘制的图形像一个大小相同的盒子,盒子的颜色应该类似于测试中和预测的蜡烛的颜色.请参阅我愿意实现的示例图像:

What I am willing to achieve is the graph plotted like a box of same size, and the color of the box should resemble the color of the candle in the test and predicted. See the example image of what I am willing to achieve:

以上输出仅包含通过检查打开和关闭值确定的蜡烛颜色.

The above output consists of only the color of the candles that is decided by checking the open and close values.

这是示例数据. 真实数据集收盘价的预测值.

Here is the sample data. The Real dataset and the Predicted values of the Close price.

已编辑
请建议我以上是无法实现的,然后可以使用此类数据集进行以下操作.

Edited
Please suggest me the above is unachievable then can the below is possible with such dataset.

推荐答案

因此,据我了解,您实际上只是想绘制一系列矩形.这可以通过在matplotlib中添加补丁(通过打开>关闭,

So, if I understand, you really just want to draw a series of rectangles. This can be done by adding patches in matplotlib coloured by open > close,

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
from matplotlib.patches import Rectangle

def draw_rects(ax, quotes, width=5., height=1., yloc=1., colorup='g',
               colordown='r', edgecolor='k', alpha=1.0):

    OFFSET = width / 2.0
    patches = []
    for q in quotes:
        t, open, close, high, low = q[:5]
        if close > open:
            color = colorup
        else:
            color = colordown

        rect = Rectangle(
            xy=(t - OFFSET, yloc),
            width=width,
            height=height,
            facecolor=color,
            edgecolor=edgecolor,
        )
        rect.set_alpha(alpha)
        patches.append(rect)
        ax.add_patch(rect)

    ax.autoscale_view()

    return patches

fig, ax = plt.subplots(1,1)
quotes = np.genfromtxt("./out.csv", skip_header=1, delimiter=',')
p1 = draw_rects(ax, quotes, yloc=1)
p2 = draw_rects(ax, quotes, yloc=4)
labels = [item.get_text() for item in ax.get_yticklabels()]
labels[2] = 'Predicted'
labels[8] = 'Real'
ax.set_yticklabels(labels)
plt.show()

看起来像这样

您可以根据需要调整宽度,边缘颜色等.我已经绘制了两者的真实数据,因为您所拥有的预测链接的格式不同.我已经将相同的数据添加到了draw_rects的另一个yloc上,并更改了y刻度标签为例.

you can adjust width, edgecolor, etc as needed. I've plotted the real data for both as the predicted link you had is not formatted in the same way. I've added the same data at a different yloc to draw_rects and changed the y tick labels as an example.

out.csv中的数据只是

The data in out.csv is just

time,open,high,low,close
10,1.1661,1.16615,1.16601,1.16603
20,1.16623,1.16623,1.1661,1.1661
30,1.16617,1.16624,1.16617,1.16623
40,1.16613,1.16618,1.16612,1.16618
50,1.16615,1.16615,1.16612,1.16613
60,1.16613,1.16615,1.16613,1.16615
70,1.16617,1.16621,1.16612,1.16612
80,1.16618,1.16626,1.16615,1.16617
90,1.16614,1.16619,1.16614,1.16618
100,1.16618,1.16618,1.16609,1.16614

这篇关于使用matplotlib或使用Python3绘制颜色数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:49