matplotlib - 绘制共享 x 轴的多个 mplfinance 图

标签 matplotlib subplot mplfinance

我正在尝试使用 mplfinance 绘制 5 个图表。

这个有效:

for coin in coins:
    mpf.plot(df_coins[coin], title=coin, type='line', volume=True, show_nontrading=True)

但是,在我的 Python Notebook 单元格输出中,每个图都是一个单独的图像。并对每张图像重复 x 轴标记。

我尝试制作一个包含多个子图/轴的图形,并在每个轴上绘制一个图表:

from matplotlib import pyplot as plt

N = len(df_coins)
fig, axes = plt.subplots(N, figsize=(20, 5*N), sharex=True)

for i, ((coin, df), ax) in zip(enumerate(df_coins.items()), axes):
    mpf.plot(df, ax=ax, title=coin, type='line', volume=True, show_nontrading=True)

这显示了正确尺寸的子图,但是它们没有填充数据。坐标轴标记为 0.0 到 1.0,但没有显示标题。

我错过了什么?

最佳答案

子图有两种方法。一种是设置一个带有 mplfinance 对象的图形。另一种方法是使用您采用的 matplotlib 子图来放置它。

yfinace 数据

import matplotlib.pyplot as plt
import mplfinance as mpf
import yfinance as yf

tickers = ['AAPL','GOOG','TSLA']
data = yf.download(tickers, start="2021-01-01", end="2021-03-01", group_by='ticker')

aapl = data[('AAPL',)]
goog = data[('GOOG',)]
tsla = data[('TSLA',)]

mplfinance

fig = mpf.figure(style='yahoo', figsize=(12,9))
#fig.subplots_adjust(hspace=0.3)
ax1 = fig.add_subplot(3,1,1, sharex=ax3)
ax2 = fig.add_subplot(3,1,2, sharex=ax3)
ax3 = fig.add_subplot(3,1,3)

mpf.plot(aapl, type='line', ax=ax1, axtitle='AAPL', xrotation=0)
mpf.plot(goog, type='line', ax=ax2, axtitle='GOOG', xrotation=0)
mpf.plot(tsla, type='line', ax=ax3, axtitle='TSLA', xrotation=0)
ax1.set_xticklabels([])
ax2.set_xticklabels([])

ma​​tplotlib

N = len(tickers)
fig, axes = plt.subplots(N, figsize=(20, 5*N), sharex=True)

for df,t,ax in zip([aapl,goog,tsla], tickers, axes):
    mpf.plot(df, ax=ax, axtitle=t, type='line', show_nontrading=True)# volume=True 

关于matplotlib - 绘制共享 x 轴的多个 mplfinance 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69777102/

相关文章:

python - mplfinance 图表内的阴影区域

python - 使用 mplfinance/matplotlib 时可能出现内存泄漏。如何解决?

c# - Microsoft.Scripting.SyntaxErrorException : 'unexpected token ' ='' When importing matplotlib with IronPython

Python:将 python subplots() 扩展到多行/多列

python - 将线图添加到 Facetgrid 图中

python - 创建子图,或在保留原始大小的同时合并图形

python - 为什么我无法从 mplfinance 导入 Candlestick_ohlc

python - 让 PyQt4 在按下按钮时打开另一个 Qt 窗口

python - 类型错误 : 'DataFrame' object is not callable

python - 为什么 set_xlim() 没有在我的图中设置 x 限制?