Question

All of the examples I find online have to do with pulling data from the internet. I have my own data and I only want the max and min aspect of the candlestick (don't need beginning and ending levels on the candlesticks).

I have a list of decimal values:

values=[[5,6],[6,7],[7,8.8],...]

I have

from matplotlib.pyplot import subplots, draw
from matplotlib.finance import candlestick, candlestick2

fig, ax = subplots()
candlestick(ax, values, width=0.5)
plt.show()

I wrote this based on online examples. I am not sure what the ax means and how it is used. Also, I am not sure how to input the values list correctly. Right now I get the following error:

Traceback (most recent call last):
  File "algor.py", line 41, in <module>
    candlestick(ax, values, width=0.5)
  File "/usr/lib/pymodules/python2.7/matplotlib/finance.py", line 330, in candlestick
    t, open, close, high, low = q[:5]
ValueError: need more than 2 values to unpack

is it complaining because I have only 2 instead of 4 inputs? I only want a rectangles, without the tails. Should I change values to

values=[[5,5,6,6],[7,7,8.8,8.8],...]

edit: this gives me the same error

Was it helpful?

Solution

The quotes argument to candlestick must be a sequence of (time, open, close, high, low, ...) according to the documentation here. If you only want rectangles without tails, maybe you could use a box plot instead.

OTHER TIPS

DISCLAIMER: I really don't have a clue what the candlestick plots is trying to do

I can however tell you the following, looking at the documentation for this function we get:

Definition: candlestick(ax, quotes, width=0.2, colorup=u'k', colordown=u'r', alpha=1.0)
Docstring:
Plot the time, open, close, high, low as a vertical line ranging
from low to high.  Use a rectangular bar to represent the
open-close span.  If close >= open, use colorup to color the bar,
otherwise use colordown

Now to go through this with you, the ax is an axis instance for a set of axis. You created it when calling fig, ax = plt.subplots(). ax has methods which can be called by ax.plot() or ax.set_xlims(). Welcome to object orientated programming.

So now why are you getting this error? Well look in the docs we look at what candlestickexpects forquotes`:

quotes : sequence of (time, open, close, high, low, ...) sequences
    As long as the first 5 elements are these values,
    the record can be as long as you want (eg it may store volume).

So I'm afraid you have to give it the begining and end values that you mentioned you didn't want to give it. Perhaps you can just make some up? So basically to get this to run, try something like

values = [[1, 2, 3, 4, 5], [2, 3, 4, 5, 6]]
candlestick(ax, values)

Each item in values will the be plotted as mentioned in the docs.

To learn how to find these things yourself, especially as the finance package is impossible to find the docs online, I recommend you download and installed ipython . This is a python interpreter where you can query something to find out more, e.g type

candlestick?

and it will print the documentation, type

ax. # and then press tab after the full stop

and it will print all the things you could do with the axis!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top