简体   繁体   English

如何获取 matplotlib Axes 实例

[英]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() .为此,我想使用 function matplotlib.finance.candlestick() I need to supply quotes to this function and " an Axes instance to plot to ".我需要为这个 function 和“一个 Axes 实例到 plot提供报价”。 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.不过,我现在还需要一个 Axes 实例,对此我有点迷茫。 I created plots before using matplotlib.pyplot .我在使用matplotlib.pyplot之前创建了绘图。 I now need to do something with matplotlib.axes though, but I am unsure what exactly.我现在需要对matplotlib.axes做一些事情,但我不确定具体是什么。

Could anybody help me?有人可以帮助我吗?

Use the gca ("get current axes") helper function:使用gca (“获取当前轴”)辅助函数:

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

在此处输入图片说明

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如果烛台是您唯一想要绘制的东西,则第二个要容易得多

Every Figure instance has Axes defined on it.每个 Figure 实例都定义了 Axes。 As the other answers mentioned, plt.gca() returns the current Axes instance.正如提到的其他答案, plt.gca()返回当前的Axes 实例。 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.要获取图上定义的其他 Axes 实例,您可以通过图的axes属性查看图中的 Axes 列表。

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.如果您使用明显不返回 Axes 实例的库创建 plot,这将特别有用。 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.只要所述库在后台使用 matplotlib,每个 plot 都有一个 Figure 实例,通过它可以访问其中的任何 Axes。

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.例如,如果您使用statsmodels进行 plot 季节性分解,则返回的 object 是一个 matplotlib 图 object。要更改任何子图中的某些内容,您可以使用axes属性。 For example, the following code makes the markersize smaller on the residual plot in the decomposition.例如,下面的代码在分解中的残差 plot 上使 markersize 变小。

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

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM