【问题标题】:How to use Gridspec with Pandas plot如何将 Gridspec 与 Pandas 绘图一起使用
【发布时间】:2015-09-07 05:21:13
【问题描述】:

我想使用 Gridspec 配置子图大小,如本问题中所述。 Python/Matplotlib - Change the relative size of a subplot

如果我想使用 Pandas Dataframe 的绘图功能,该怎么做?有可能吗?

【问题讨论】:

  • 您需要更具体。您可以通过传入 plot 方法在任何 Axes 对象上使用 df.plot,例如,df.plot(..., ax=ax1)

标签: pandas matplotlib


【解决方案1】:

你可以这样做。

import pandas as pd
import pandas.io.data as web
import matplotlib.pyplot as plt

aapl = web.DataReader('AAPL', 'yahoo', '2014-01-01', '2015-05-31')
# 3 x 1 grid, position at (1st row, 1st col), take two rows
ax1 = plt.subplot2grid((3, 1), (0, 0), rowspan=2) 
# plot something on this ax1
aapl['Adj Close'].plot(style='r-', ax=ax1)
# plot moving average again on this ax1
pd.ewma(aapl['Adj Close'], span=20).plot(style='k--', ax=ax1)
# get the other ax
ax2 = plt.subplot2grid((3, 1), (2, 0), rowspan=1) 
ax2.bar(aapl.index, aapl.Volume)

【讨论】:

    【解决方案2】:

    我认为您不能将 gridspec 与 pandas plot 一起使用。 pandas plot 模块是 matplotlib pyplot 的封装,不一定实现所有功能。

    如果您检查 github 上的 pandas 源并在 gridspec 上进行搜索,您会注意到 plot 没有提供配置 gridspec 的选项

    https://github.com/pydata/pandas

    【讨论】:

      猜你喜欢
      • 2016-03-20
      • 2020-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 2011-09-09
      • 1970-01-01
      • 2019-10-20
      相关资源
      最近更新 更多