0

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

output image

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:
test output

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.
output achieveable or not

3
  • Can somebody help me, please? I am not getting how can I plot like mentioned in the question Commented Jul 19, 2018 at 6:50
  • The matplotlib finance toolbox has ohlc, see e.g. stackoverflow.com/questions/36334665/… but it's not included so you'd need to get from github.com/matplotlib/mpl_finance Commented Jul 19, 2018 at 7:24
  • I know of that. Actually I am not willing to pot OHLC. I already did it. I just want to have the one I have mentioned in the question. Please let me know. I just want to display the color of the candle and not the candle, as they have varying sizes and I want that there size must remain same. See the image, like a stacked bar, but should reflect the bar values. please let me know. Commented Jul 19, 2018 at 7:27

1 Answer 1

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

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

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,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
Sign up to request clarification or add additional context in comments.

6 Comments

Yes something like this. But can I do the combination of the real and predicted value? Also I won't just use the open and close. That is enough for determining the candle color. Can you modify the code so I can accept your answer.
And if possible can I use the pandas dataframe in it or do I need to convert to numpy array and use it? or will it have no effect?
Is it possible that I calculate the color in separate array and just use it by keeping the y constant ?
I've added to the example how you plot two sets of data and label the y axis. I'd personally just get the numpy array from pandas using df.values but you can adjust the above function to work with dataframes
Anything is possible, the above code simply plots a load of rectangles in a line. You can customise it to do whatever you need.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.