You are reading an old version of the documentation (v1.4.1). For the latest version see https://matplotlib.org/stable/
matplotlib

Table Of Contents

Previous topic

figure

Next topic

font_manager

This Page

finance

matplotlib.finance

A collection of functions for collecting, analyzing and plotting financial data. User contributions welcome!

This module is deprecated in 1.4 and will be moved to mpl_toolkits or it’s own project in the future.

matplotlib.finance.candlestick(ax, quotes, width=0.2, colorup=u'k', colordown=u'r', alpha=1.0)

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

This function has been deprecated in 1.4 in favor of candlestick_ochl, which maintains the original argument order, or candlestick_ohlc, which uses the open-high-low-close order. This function will be removed in 1.5

Parameters:

ax : Axes

an Axes instance to plot to

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

time must be in float days format - see date2num

width : float

fraction of a day for the rectangle width

colorup : color

the color of the rectangle where close >= open

colordown : color

the color of the rectangle where close < open

alpha : float

the rectangle alpha level

Returns:

ret : tuple

returns (lines, patches) where lines is a list of lines added and patches is a list of the rectangle patches added

matplotlib.finance.candlestick2(ax, opens, closes, highs, lows, width=4, colorup=u'k', colordown=u'r', alpha=0.75)

Represent the open, close as a bar line and high low range as a vertical line.

This function has been deprecated in 1.4 in favor of candlestick2_ochl, which maintains the original argument order, or candlestick2_ohlc, which uses the open-high-low-close order. This function will be removed in 1.5

Parameters:

ax : Axes

an Axes instance to plot to

opens : sequence

sequence of opening values

closes : sequence

sequence of closing values

highs : sequence

sequence of high values

lows : sequence

sequence of low values

ticksize : int

size of open and close ticks in points

colorup : color

the color of the lines where close >= open

colordown : color

the color of the lines where close < open

alpha : float

bar transparency

Returns:

ret : tuple

(lineCollection, barCollection)

matplotlib.finance.candlestick2_ochl(ax, opens, closes, highs, lows, width=4, colorup=u'k', colordown=u'r', alpha=0.75)

Represent the open, close as a bar line and high low range as a vertical line.

Preserves the original argument order.

Parameters:

ax : Axes

an Axes instance to plot to

opens : sequence

sequence of opening values

closes : sequence

sequence of closing values

highs : sequence

sequence of high values

lows : sequence

sequence of low values

ticksize : int

size of open and close ticks in points

colorup : color

the color of the lines where close >= open

colordown : color

the color of the lines where close < open

alpha : float

bar transparency

Returns:

ret : tuple

(lineCollection, barCollection)

matplotlib.finance.candlestick2_ohlc(ax, opens, highs, lows, closes, width=4, colorup=u'k', colordown=u'r', alpha=0.75)

Represent the open, close as a bar line and high low range as a vertical line.

Parameters:

ax : Axes

an Axes instance to plot to

opens : sequence

sequence of opening values

highs : sequence

sequence of high values

lows : sequence

sequence of low values

closes : sequence

sequence of closing values

ticksize : int

size of open and close ticks in points

colorup : color

the color of the lines where close >= open

colordown : color

the color of the lines where close < open

alpha : float

bar transparency

Returns:

ret : tuple

(lineCollection, barCollection)

matplotlib.finance.candlestick_ochl(ax, quotes, width=0.2, colorup=u'k', colordown=u'r', alpha=1.0)

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

Parameters:

ax : Axes

an Axes instance to plot to

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

time must be in float days format - see date2num

width : float

fraction of a day for the rectangle width

colorup : color

the color of the rectangle where close >= open

colordown : color

the color of the rectangle where close < open

alpha : float

the rectangle alpha level

Returns:

ret : tuple

returns (lines, patches) where lines is a list of lines added and patches is a list of the rectangle patches added

matplotlib.finance.candlestick_ohlc(ax, quotes, width=0.2, colorup=u'k', colordown=u'r', alpha=1.0)

Plot the time, open, high, low, close 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

Parameters:

ax : Axes

an Axes instance to plot to

quotes : sequence of (time, open, high, low, close, ...) 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).

time must be in float days format - see date2num

width : float

fraction of a day for the rectangle width

colorup : color

the color of the rectangle where close >= open

colordown : color

the color of the rectangle where close < open

alpha : float

the rectangle alpha level

Returns:

ret : tuple

returns (lines, patches) where lines is a list of lines added and patches is a list of the rectangle patches added

matplotlib.finance.fetch_historical_yahoo(ticker, date1, date2, cachename=None, dividends=False)

Fetch historical data for ticker between date1 and date2. date1 and date2 are date or datetime instances, or (year, month, day) sequences.

Parameters:

ticker : str

ticker

date1 : sequence of form (year, month, day), datetime, or date

start date

date2 : sequence of form (year, month, day), datetime, or date

end date

cachename : str

cachename is the name of the local file cache. If None, will default to the md5 hash or the url (which incorporates the ticker and date range)

dividends : bool

set dividends=True to return dividends instead of price data. With this option set, parse functions will not work

Returns:

file_handle : file handle

a file handle is returned

Examples

>>> fh = fetch_historical_yahoo('^GSPC', (2000, 1, 1), (2001, 12, 31))
matplotlib.finance.index_bar(ax, vals, facecolor=u'b', edgecolor=u'l', width=4, alpha=1.0)

Add a bar collection graph with height vals (-1 is missing).

Parameters:

ax : Axes

an Axes instance to plot to

vals : sequence

a sequence of values

facecolor : color

the color of the bar face

edgecolor : color

the color of the bar edges

width : int

the bar width in points

alpha : float

bar transparency

Returns:

ret : barCollection

The barrCollection added to the axes

matplotlib.finance.parse_yahoo_historical(fh, adjusted=True, asobject=False)

Parse the historical data in file handle fh from yahoo finance.

This function has been deprecated in 1.4 in favor of parse_yahoo_historical_ochl, which maintains the original argument order, or parse_yahoo_historical_ohlc, which uses the open-high-low-close order. This function will be removed in 1.5

Parameters:

adjusted : bool

If True (default) replace open, close, high, low prices with their adjusted values. The adjustment is by a scale factor, S = adjusted_close/close. Adjusted prices are actual prices multiplied by S.

Volume is not adjusted as it is already backward split adjusted by Yahoo. If you want to compute dollars traded, multiply volume by the adjusted close, regardless of whether you choose adjusted = True|False.

asobject : bool or None

If False (default for compatibility with earlier versions) return a list of tuples containing

d, open, close, high, low, volume

If None (preferred alternative to False), return a 2-D ndarray corresponding to the list of tuples.

Otherwise return a numpy recarray with

date, year, month, day, d, open, close, high, low, volume, adjusted_close

where d is a floating poing representation of date, as returned by date2num, and date is a python standard library datetime.date instance.

The name of this kwarg is a historical artifact. Formerly, True returned a cbook Bunch holding 1-D ndarrays. The behavior of a numpy recarray is very similar to the Bunch.

ochl : bool

Temporary argument to select between ochl and ohlc ordering. Defaults to True to preserve original functionality.

matplotlib.finance.parse_yahoo_historical_ochl(fh, adjusted=True, asobject=False)

Parse the historical data in file handle fh from yahoo finance.

Parameters:

adjusted : bool

If True (default) replace open, close, high, low prices with their adjusted values. The adjustment is by a scale factor, S = adjusted_close/close. Adjusted prices are actual prices multiplied by S.

Volume is not adjusted as it is already backward split adjusted by Yahoo. If you want to compute dollars traded, multiply volume by the adjusted close, regardless of whether you choose adjusted = True|False.

asobject : bool or None

If False (default for compatibility with earlier versions) return a list of tuples containing

d, open, close, high, low, volume

If None (preferred alternative to False), return a 2-D ndarray corresponding to the list of tuples.

Otherwise return a numpy recarray with

date, year, month, day, d, open, close, high, low, volume, adjusted_close

where d is a floating poing representation of date, as returned by date2num, and date is a python standard library datetime.date instance.

The name of this kwarg is a historical artifact. Formerly, True returned a cbook Bunch holding 1-D ndarrays. The behavior of a numpy recarray is very similar to the Bunch.

matplotlib.finance.parse_yahoo_historical_ohlc(fh, adjusted=True, asobject=False)

Parse the historical data in file handle fh from yahoo finance.

Parameters:

adjusted : bool

If True (default) replace open, high, low, close prices with their adjusted values. The adjustment is by a scale factor, S = adjusted_close/close. Adjusted prices are actual prices multiplied by S.

Volume is not adjusted as it is already backward split adjusted by Yahoo. If you want to compute dollars traded, multiply volume by the adjusted close, regardless of whether you choose adjusted = True|False.

asobject : bool or None

If False (default for compatibility with earlier versions) return a list of tuples containing

d, open, high, low, close, volume

If None (preferred alternative to False), return a 2-D ndarray corresponding to the list of tuples.

Otherwise return a numpy recarray with

date, year, month, day, d, open, high, low, close, volume, adjusted_close

where d is a floating poing representation of date, as returned by date2num, and date is a python standard library datetime.date instance.

The name of this kwarg is a historical artifact. Formerly, True returned a cbook Bunch holding 1-D ndarrays. The behavior of a numpy recarray is very similar to the Bunch.

matplotlib.finance.plot_day_summary(ax, quotes, ticksize=3, colorup=u'k', colordown=u'r')

Plots day summary

Represent the time, open, close, high, low as a vertical line ranging from low to high. The left tick is the open and the right tick is the close.

This function has been deprecated in 1.4 in favor of plot_day_summary_ochl, which maintains the original argument order, or plot_day_summary_ohlc, which uses the open-high-low-close order. This function will be removed in 1.5

Parameters:

ax : Axes

an Axes instance to plot to

quotes : sequence of (time, open, close, high, low, ...) sequences

data to plot. time must be in float date format - see date2num

ticksize : int

open/close tick marker in points

colorup : color

the color of the lines where close >= open

colordown : color

the color of the lines where close < open

Returns:

lines : list

list of tuples of the lines added (one tuple per quote)

matplotlib.finance.plot_day_summary2(ax, opens, closes, highs, lows, ticksize=4, colorup=u'k', colordown=u'r')

Represent the time, open, close, high, low, as a vertical line ranging from low to high. The left tick is the open and the right tick is the close.

This function has been deprecated in 1.4 in favor of plot_day_summary2_ochl, which maintains the original argument order, or plot_day_summary2_ohlc, which uses the open-high-low-close order. This function will be removed in 1.5

Parameters:

ax : Axes

an Axes instance to plot to

opens : sequence

sequence of opening values

closes : sequence

sequence of closing values

highs : sequence

sequence of high values

lows : sequence

sequence of low values

ticksize : int

size of open and close ticks in points

colorup : color

the color of the lines where close >= open

colordown : color

the color of the lines where close < open

Returns:

ret : list

a list of lines added to the axes

matplotlib.finance.plot_day_summary2_ochl(ax, opens, closes, highs, lows, ticksize=4, colorup=u'k', colordown=u'r')

Represent the time, open, close, high, low, as a vertical line ranging from low to high. The left tick is the open and the right tick is the close.

Parameters:

ax : Axes

an Axes instance to plot to

opens : sequence

sequence of opening values

closes : sequence

sequence of closing values

highs : sequence

sequence of high values

lows : sequence

sequence of low values

ticksize : int

size of open and close ticks in points

colorup : color

the color of the lines where close >= open

colordown : color

the color of the lines where close < open

Returns:

ret : list

a list of lines added to the axes

matplotlib.finance.plot_day_summary2_ohlc(ax, opens, highs, lows, closes, ticksize=4, colorup=u'k', colordown=u'r')

Represent the time, open, high, low, close as a vertical line ranging from low to high. The left tick is the open and the right tick is the close.

Parameters:

ax : Axes

an Axes instance to plot to

opens : sequence

sequence of opening values

highs : sequence

sequence of high values

lows : sequence

sequence of low values

closes : sequence

sequence of closing values

ticksize : int

size of open and close ticks in points

colorup : color

the color of the lines where close >= open

colordown : color

the color of the lines where close < open

Returns:

ret : list

a list of lines added to the axes

matplotlib.finance.plot_day_summary_oclh(ax, quotes, ticksize=3, colorup=u'k', colordown=u'r')

Plots day summary

Represent the time, open, close, high, low as a vertical line ranging from low to high. The left tick is the open and the right tick is the close.
Parameters:

ax : Axes

an Axes instance to plot to

quotes : sequence of (time, open, close, high, low, ...) sequences

data to plot. time must be in float date format - see date2num

ticksize : int

open/close tick marker in points

colorup : color

the color of the lines where close >= open

colordown : color

the color of the lines where close < open

Returns:

lines : list

list of tuples of the lines added (one tuple per quote)

matplotlib.finance.plot_day_summary_ohlc(ax, quotes, ticksize=3, colorup=u'k', colordown=u'r')

Plots day summary

Represent the time, open, high, low, close as a vertical line ranging from low to high. The left tick is the open and the right tick is the close.
Parameters:

ax : Axes

an Axes instance to plot to

quotes : sequence of (time, open, high, low, close, ...) sequences

data to plot. time must be in float date format - see date2num

ticksize : int

open/close tick marker in points

colorup : color

the color of the lines where close >= open

colordown : color

the color of the lines where close < open

Returns:

lines : list

list of tuples of the lines added (one tuple per quote)

matplotlib.finance.quotes_historical_yahoo(ticker, date1, date2, asobject=False, adjusted=True, cachename=None)

Get historical data for ticker between date1 and date2.

This function has been deprecated in 1.4 in favor of quotes_yahoo_historical_ochl, which maintains the original argument order, or quotes_yahoo_historical_ohlc, which uses the open-high-low-close order. This function will be removed in 1.5

See parse_yahoo_historical() for explanation of output formats and the asobject and adjusted kwargs.

Parameters:

ticker : str

stock ticker

date1 : sequence of form (year, month, day), datetime, or date

start date

date2 : sequence of form (year, month, day), datetime, or date

end date

cachename : str or None

is the name of the local file cache. If None, will default to the md5 hash or the url (which incorporates the ticker and date range)

Examples

>>> sp = f.quotes_historical_yahoo('^GSPC', d1, d2,
                         asobject=True, adjusted=True)
>>> returns = (sp.open[1:] - sp.open[:-1])/sp.open[1:]
>>> [n,bins,patches] = hist(returns, 100)
>>> mu = mean(returns)
>>> sigma = std(returns)
>>> x = normpdf(bins, mu, sigma)
>>> plot(bins, x, color='red', lw=2)
matplotlib.finance.quotes_historical_yahoo_ochl(ticker, date1, date2, asobject=False, adjusted=True, cachename=None)

Get historical data for ticker between date1 and date2.

See parse_yahoo_historical() for explanation of output formats and the asobject and adjusted kwargs.

Parameters:

ticker : str

stock ticker

date1 : sequence of form (year, month, day), datetime, or date

start date

date2 : sequence of form (year, month, day), datetime, or date

end date

cachename : str or None

is the name of the local file cache. If None, will default to the md5 hash or the url (which incorporates the ticker and date range)

Examples

>>> sp = f.quotes_historical_yahoo_ochl('^GSPC', d1, d2,
                         asobject=True, adjusted=True)
>>> returns = (sp.open[1:] - sp.open[:-1])/sp.open[1:]
>>> [n,bins,patches] = hist(returns, 100)
>>> mu = mean(returns)
>>> sigma = std(returns)
>>> x = normpdf(bins, mu, sigma)
>>> plot(bins, x, color='red', lw=2)
matplotlib.finance.quotes_historical_yahoo_ohlc(ticker, date1, date2, asobject=False, adjusted=True, cachename=None)

Get historical data for ticker between date1 and date2.

See parse_yahoo_historical() for explanation of output formats and the asobject and adjusted kwargs.

Parameters:

ticker : str

stock ticker

date1 : sequence of form (year, month, day), datetime, or date

start date

date2 : sequence of form (year, month, day), datetime, or date

end date

cachename : str or None

is the name of the local file cache. If None, will default to the md5 hash or the url (which incorporates the ticker and date range)

Examples

>>> sp = f.quotes_historical_yahoo_ohlc('^GSPC', d1, d2,
                         asobject=True, adjusted=True)
>>> returns = (sp.open[1:] - sp.open[:-1])/sp.open[1:]
>>> [n,bins,patches] = hist(returns, 100)
>>> mu = mean(returns)
>>> sigma = std(returns)
>>> x = normpdf(bins, mu, sigma)
>>> plot(bins, x, color='red', lw=2)
matplotlib.finance.volume_overlay(ax, opens, closes, volumes, colorup=u'k', colordown=u'r', width=4, alpha=1.0)

Add a volume overlay to the current axes. The opens and closes are used to determine the color of the bar. -1 is missing. If a value is missing on one it must be missing on all

Parameters:

ax : Axes

an Axes instance to plot to

opens : sequence

a sequence of opens

closes : sequence

a sequence of closes

volumes : sequence

a sequence of volumes

width : int

the bar width in points

colorup : color

the color of the lines where close >= open

colordown : color

the color of the lines where close < open

alpha : float

bar transparency

Returns:

ret : barCollection

The barrCollection added to the axes

matplotlib.finance.volume_overlay2(ax, closes, volumes, colorup=u'k', colordown=u'r', width=4, alpha=1.0)

Add a volume overlay to the current axes. The closes are used to determine the color of the bar. -1 is missing. If a value is missing on one it must be missing on all

nb: first point is not displayed - it is used only for choosing the right color

Parameters:

ax : Axes

an Axes instance to plot to

closes : sequence

a sequence of closes

volumes : sequence

a sequence of volumes

width : int

the bar width in points

colorup : color

the color of the lines where close >= open

colordown : color

the color of the lines where close < open

alpha : float

bar transparency

Returns:

ret : barCollection

The barrCollection added to the axes

matplotlib.finance.volume_overlay3(ax, quotes, colorup=u'k', colordown=u'r', width=4, alpha=1.0)

Add a volume overlay to the current axes. quotes is a list of (d, open, high, low, close, volume) and close-open is used to determine the color of the bar

Parameters:

ax : Axes

an Axes instance to plot to

quotes : sequence of (time, open, high, low, close, ...) sequences

data to plot. time must be in float date format - see date2num

width : int

the bar width in points

colorup : color

the color of the lines where close1 >= close0

colordown : color

the color of the lines where close1 < close0

alpha : float

bar transparency

Returns:

ret : barCollection

The barrCollection added to the axes