How to get a matplotlib Axes instance to plot to? How to get a matplotlib Axes instance to plot to? python python

How to get a matplotlib Axes instance to plot to?


Use the gca ("get current axes") helper function:

ax = plt.gca()

Example:

import matplotlib.pyplot as pltimport matplotlib.financequotes = [(1, 5, 6, 7, 4), (2, 6, 9, 9, 6), (3, 9, 8, 10, 8), (4, 8, 8, 9, 8), (5, 8, 11, 13, 7)]ax = plt.gca()h = matplotlib.finance.candlestick(ax, quotes)plt.show()

enter image description here


You can either

fig, ax = plt.subplots()  #create figure and axescandlestick(ax, quotes, ...)

or

candlestick(plt.gca(), quotes) #get the axis when calling the function

The first gives you more flexibility. The second is much easier if candlestick is the only thing you want to plot