与熊猫的卷叠加

时间:2015-07-14 15:05:49

标签: python pandas matplotlib

我经常在matplotlib和open-high-low-close的上下文中看到这一点,但我想知道你是否可以在pandas框架中添加一个卷叠加。我们想要的最终图表将接近第一个:(Matplotlib - Finance volume overlay

假设我们有一个像这样的DataFrame:

                       num  rolling_30  rolling_10  rolling_60  Volume
Date
2015-06-23        0.000219    0.000149    0.000168    0.000183       2
2015-06-25        0.000489    0.000162    0.000200    0.000188       3
2015-07-01        0.000164    0.000163    0.000190    0.000186       1
2015-07-02        0.000190    0.000166    0.000190    0.000187       1
2015-07-03        0.000269    0.000171    0.000198    0.000180       1
2015-07-04        0.000935    0.000196    0.000282    0.000193       2
2015-07-08        0.000154    0.000196    0.000288    0.000188       1
2015-07-11        0.000274    0.000202    0.000305    0.000190       1
2015-07-13        0.000872    0.000228    0.000380    0.000201       9

我们怎样才能获得[' num'' rolling_30',' rolling_10'' rolling_60']折线图列出每日交易量的图表?我可以做一个secondary_y来获得正确的音量,但老实说这看起来很糟糕。需要它是图表底部的传统音量条形图。

2 个答案:

答案 0 :(得分:1)

基本思想是使用.twinx创建辅助y轴。以下是一个简短的样本。从图中可以看出左边的y轴是用于价格和移动平均线,而右边的y轴是用于体积的。

import pandas as pd
import matplotlib.pyplot as plt

# your data
# ============================
print(df)


               num  rolling_30  rolling_10,  rolling_60  Volume
Date                                                           
2015-06-23  0.0002      0.0001       0.0002      0.0002       2
2015-06-25  0.0005      0.0002       0.0002      0.0002       3
2015-07-01  0.0002      0.0002       0.0002      0.0002       1
2015-07-02  0.0002      0.0002       0.0002      0.0002       1
2015-07-03  0.0003      0.0002       0.0002      0.0002       1
2015-07-04  0.0009      0.0002       0.0003      0.0002       2
2015-07-08  0.0002      0.0002       0.0003      0.0002       1
2015-07-11  0.0003      0.0002       0.0003      0.0002       1
2015-07-13  0.0009      0.0002       0.0004      0.0002       9

# plotting
# ===========================
fig, ax = plt.subplots(figsize=(10,8))
df.drop('Volume', axis=1).plot(ax=ax)
ax.legend(loc='best')
ax2 = ax.twinx()
df['Volume'].plot(kind='bar', ax=ax2, color='g', alpha=0.1)
ax2.set_ylim([0, ax2.get_ylim()[1] * 10])
ax2.legend(loc='best')

enter image description here

答案 1 :(得分:1)

因此,虽然Jianxun的答案可能完全正确,但它不适用于我的系统。我在Pandas .17和最新的matplotlib。我也在使用Macbook。基本上,如果我尝试与两个图形(线条和条形图)共享一个X轴,第一个实例化的图形就会消失。

我能想到解决这个问题的最好方法就是在一个(子图)上做两个图并隐藏顶图的X轴。这是我做的:

        fig = plt.figure(figsize=(10,6))
        ax1 = plt.subplot2grid((3,3), (0,0), colspan=3, rowspan=2)
        ax2 = plt.subplot2grid((3,3), (2,0), colspan=3)
        ax2.yaxis.tick_right()
        ax1.axes.get_xaxis().set_visible(False)
        fig.subplots_adjust(hspace=0)
        pct.drop('Volume', axis=1).plot(ax=ax1)
        pct['Volume'].plot(kind='bar',ax=ax2, rot=75, legend=True)
        #Set-up the X axis to not show so many labels
        n = 6
        ticks = ax2.xaxis.get_ticklocs()
        ticklabels = [l.get_text() for l in ax2.xaxis.get_ticklabels()]
        ax2.xaxis.set_ticks(ticks[::n])
        ax2.xaxis.set_ticklabels(ticklabels[::n])
        plt.gcf().subplots_adjust(bottom=0.15)
相关问题