Pandas Dataframe:area plot stacked ='false'not working

时间:2015-12-22 17:33:01

标签: python pandas matplotlib plot

即使我通过设置参数 stacked ='false'明确停用堆叠,我仍然会堆积一个区域图。

以下是一些示例代码:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime as dt

# Generate sample data
sample_data = np.random.rand(24*365, 5)
df = pd.DataFrame(sample_data,
                  index=pd.date_range('1/1/2015 00:00',
                                      periods=len(sample_data), freq='H'))

# Select date range to plot
date_from = dt(2015, 12, 22, 12, 0)
date_to = dt(2015, 12, 22, 23, 0)
df = df.loc[date_from:date_to]

# Plotting
df.plot(kind='line', colormap='Blues')
# Place legend topright
[ax.legend(loc=1) for ax in plt.gcf().axes]
df.plot(kind='bar', stacked='true', colormap='Greens')
# Place legend topright
[ax.legend(loc=1) for ax in plt.gcf().axes]
df.plot(kind='barh', stacked='true', colormap='Oranges')
# Place legend topright
[ax.legend(loc=1) for ax in plt.gcf().axes]

# This one doesn't work as explained in
# http://pandas.pydata.org/pandas-docs/stable/visualization.html
df.plot(kind='area', stacked='false', alpha=0.5, colormap='Reds')
# Place legend topright
[ax.legend(loc=1) for ax in plt.gcf().axes]

区域图在我的机器上看起来像这样:

enter image description here

我有两个问题:

1.。)如何修复区域图以使其不堆叠?

2。)在每个情节之后,有没有办法摆脱重复的“[ax.legend(loc = 1)for ax in plt.gcf()。axes]”?

提前致谢!

1 个答案:

答案 0 :(得分:1)

设置 stacked = False 而不是'false'为我解决了第一个问题!

bar.o

enter image description here

如果有人知道如何修复图例位置(请参阅原始问题),请告诉我们!