kramer65
kramer65

Reputation: 54023

How to get a matplotlib Axes instance

I need to make a candlestick chart using some stock data. For this I want to use the function matplotlib.finance.candlestick(). I need to supply quotes to this function and "an Axes instance to plot to". I created some sample quotes as follows:

quotes = [(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)]

I now also need an Axes instance though, at which I am a bit lost. I created plots before using matplotlib.pyplot. I now need to do something with matplotlib.axes though, but I am unsure what exactly.

Could anybody help me?

Upvotes: 122

Views: 193816

Answers (3)

cottontail
cottontail

Reputation: 23509

Every Figure instance has Axes defined on it. As the other answers mentioned, plt.gca() returns the current Axes instance. To get other Axes instances defined on a Figure, you can check the list of Axes in the Figure through the axes property of the Figure.

import matplotlib.pyplot as plt

plt.plot(range(3))
plt.gcf().axes     # [<Axes: >]


fig, axs = plt.subplots(1, 3)
fig.axes   # [<Axes: >, <Axes: >, <Axes: >]

This returns a list, so you can just index it for the specific Axes you want.


This is especially useful if you create a plot using a library that doesn't obviously return an Axes instance. As long as said library uses matplotlib in the background, every plot has a Figure instance, through which any Axes in it can be accessed.

For example, if you plot seasonal decomposition using statsmodels, the returned object is a matplotlib Figure object. To change something on any of the subplots, you can use the axes property. For example, the following code makes the markersize smaller on the residual plot in the decomposition.

import pandas as pd
from statsmodels.tsa.seasonal import seasonal_decompose
# plot seasonal decomposition
data = pd.Series(range(100), index=pd.date_range('2020', periods=100, freq='D'))
fig = seasonal_decompose(data).plot()

fig.axes  # get Axes list
# [<Axes: >, <Axes: ylabel='Trend'>, <Axes: ylabel='Seasonal'>, <Axes: ylabel='Resid'>]

ax = fig.axes[3]               # last subplot
ax.lines[0].set_markersize(3)  # make marker size smaller on the last subplot

result

Upvotes: 3

wim
wim

Reputation: 363616

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

ax = plt.gca()

Example:

import matplotlib.pyplot as plt
import matplotlib.finance
quotes = [(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

Upvotes: 241

Francesco Montesano
Francesco Montesano

Reputation: 8668

You can either

fig, ax = plt.subplots()  #create figure and axes
candlestick(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

Upvotes: 24

Related Questions