如何获取要打印到的matplotlib轴实例?

2024-04-29 11:16:14 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

不过,我现在还需要一个Axes实例,在这个实例中我有点迷失了。我在使用matplotlib.pyplot之前创建了绘图。我想我现在需要用matplotlib.axes做点什么,但是我不确定具体是什么。

有人能帮我一下吗?欢迎所有小费!


Tags: 数据实例函数示例matplotlib图表绘制引号
2条回答

使用^{}(“获取当前轴”)帮助函数:

ax = plt.gca()

示例:

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

你也可以

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

或者

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

第一种给你更多的灵活性。如果烛台是你唯一想设计的东西,第二个就容易多了

相关问题 更多 >