如何将子图集成到一个 matplotlib 轴中?
How to integrate subcharts in one matplotlib axes?
查看backtrader的输出,我发现matplotlib中似乎有一个功能可以将多个子图集成在一个图中:
(另见 https://www.backtrader.com/docu/plotting/plotting/ )
屏幕截图显示了一个图 - 显然没有任何 matplotlib 子图 - 具有三个子图(broker
、trades
、price data
)。
我知道如何使用 matplotlib 创建子图 - 但这不是我要找的。
我想知道如何在一个轴上(这是正确的 matplotlib 术语吗?)创建多个共享相同 x 轴的子图。
您可以通过定义子图中的 height_ratios
来实现图片显示的效果。
several subcharts that shares the same x axis.
关于sharex
和sharey
可以参考matplotlib.pyplot.subplots
When subplots have a shared x-axis along a column, only the x tick
labels of the bottom subplot are created.
Similarly, when subplots have a shared y-axis along a row, only the y tick labels of the first column subplot are created.
import matplotlib.pyplot as plt
fig, axs = plt.subplots(3, 1, sharex='col',
gridspec_kw={'width_ratios': [1], 'height_ratios': [2, 2, 10],
'hspace': 0, 'wspace': 0})
# avoid overlay
axs[1].set_yticks([0, 0.5, 1])
axs[1].set_yticklabels(["", "0.5", ""])
plt.show()
如果你对如何标注y标签感兴趣,可以看看我的this answer。
有一个优秀的股票可视化库等。最初包含在matplotlib中,但现在是一个独立的库,我将对example on Github. The formula can be found here进行简化回答。
import pandas as pd
import mplfinance as mpf
idf = pd.read_csv('data/SPY_20110701_20120630_Bollinger.csv', index_col=0, parse_dates=True)
df = idf.loc['2011-07-01':'2011-12-30',:]
mpf.plot(df, volume=True, tight_layout=True, figscale=0.75, style='checkers')
查看backtrader的输出,我发现matplotlib中似乎有一个功能可以将多个子图集成在一个图中:
屏幕截图显示了一个图 - 显然没有任何 matplotlib 子图 - 具有三个子图(broker
、trades
、price data
)。
我知道如何使用 matplotlib 创建子图 - 但这不是我要找的。
我想知道如何在一个轴上(这是正确的 matplotlib 术语吗?)创建多个共享相同 x 轴的子图。
您可以通过定义子图中的 height_ratios
来实现图片显示的效果。
several subcharts that shares the same x axis.
关于sharex
和sharey
When subplots have a shared x-axis along a column, only the x tick labels of the bottom subplot are created.
Similarly, when subplots have a shared y-axis along a row, only the y tick labels of the first column subplot are created.
import matplotlib.pyplot as plt
fig, axs = plt.subplots(3, 1, sharex='col',
gridspec_kw={'width_ratios': [1], 'height_ratios': [2, 2, 10],
'hspace': 0, 'wspace': 0})
# avoid overlay
axs[1].set_yticks([0, 0.5, 1])
axs[1].set_yticklabels(["", "0.5", ""])
plt.show()
如果你对如何标注y标签感兴趣,可以看看我的this answer。
有一个优秀的股票可视化库等。最初包含在matplotlib中,但现在是一个独立的库,我将对example on Github. The formula can be found here进行简化回答。
import pandas as pd
import mplfinance as mpf
idf = pd.read_csv('data/SPY_20110701_20120630_Bollinger.csv', index_col=0, parse_dates=True)
df = idf.loc['2011-07-01':'2011-12-30',:]
mpf.plot(df, volume=True, tight_layout=True, figscale=0.75, style='checkers')