Skip to content Skip to sidebar Skip to footer

Plotting Color Array Using Matplotlib Or Plotly Python3

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

Solution 1:

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

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

which looks like this,

enter image description here

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.

The data in out.csv is just

time,open,high,low,close10,1.1661,1.16615,1.16601,1.1660320,1.16623,1.16623,1.1661,1.166130,1.16617,1.16624,1.16617,1.1662340,1.16613,1.16618,1.16612,1.1661850,1.16615,1.16615,1.16612,1.1661360,1.16613,1.16615,1.16613,1.1661570,1.16617,1.16621,1.16612,1.1661280,1.16618,1.16626,1.16615,1.1661790,1.16614,1.16619,1.16614,1.16618100,1.16618,1.16618,1.16609,1.16614

Post a Comment for "Plotting Color Array Using Matplotlib Or Plotly Python3"