使用matplotlib或plotly Python3打印颜色数组

2024-04-23 06:38:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用OHLC值绘制蜡烛。但我现在只想描绘蜡烛的颜色。
我试图预测收盘价,并绘制它使用matplotlib。请参见以下内容:

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()

output image

我想实现的是把图绘制成一个同样大小的盒子,盒子的颜色应该与测试和预测中蜡烛的颜色相似。请参见我愿意实现的示例图像:
test output

上面的输出只包括蜡烛的颜色,这是通过检查打开和关闭值来决定的。你知道吗

这是示例数据。收盘价的Real datasetPredicted values。你知道吗

编辑
请告诉我以上是不可能实现的,那么下面的数据集是可能的。
output achieveable or not


Tags: 数据示例plot颜色绘制pltpricelabel
1条回答
网友
1楼 · 发布于 2024-04-23 06:38:36

所以,如果我明白了,你真的只想画一系列的矩形。这可以通过在matplotlib中添加由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()

看起来像这样

enter image description here

您可以根据需要调整宽度、边颜色等。我已经绘制了两者的真实数据,因为您所预测的链接的格式不同。我在不同的yloc中添加了相同的数据到draw_rects,并更改了y记号标签作为示例。你知道吗

中的数据输出.csv只是

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

相关问题 更多 >