Python 如何获取要绘制的 matplotlib Axes 实例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15067668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:14:03  来源:igfitidea点击:

How to get a matplotlib Axes instance to plot to?

pythonmatplotlibfinanceaxes

提问by kramer65

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

我需要使用一些股票数据制作烛台图(类似这样的东西)。为此,我想使用函数matplotlib.finance.candlestick()。对于这个函数,我需要提供引号和“要绘制到的 Axes 实例”。我创建了一些示例报价如下:

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 think I now need to do something with matplotlib.axesthough, but I am unsure what exactly.

不过,我现在还需要一个 Axes 实例,对此我有点迷茫。我在使用 matplotlib.pyplot 之前创建了绘图。我想我现在需要对matplotlib.axes做一些事情,但我不确定到底是什么。

Could anybody help me out a little bit here? All tips are welcome!

有人可以帮我一下吗?欢迎所有提示!

采纳答案by wim

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

enter image description here

在此处输入图片说明

回答by Francesco Montesano

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

第一个为您提供了更大的灵活性。如果烛台是您唯一想要绘制的东西,则第二个要容易得多