Cord Kaldemeyer
Cord Kaldemeyer

Reputation: 6917

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

I have an area plot that remains stacked even if I explicitly deactivate the stacking by setting the argument stacked='false'.

Here's some example code:

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]

The area plot looks like this on my machine:

enter image description here

I have two questions:

1.) How can I fix the area plot so that it's not stacked?

2.) Is there any way to get rid of the repeated " [ax.legend(loc=1) for ax in plt.gcf().axes]" after each plot?

Thanks in advance!

Upvotes: 1

Views: 2958

Answers (1)

Cord Kaldemeyer
Cord Kaldemeyer

Reputation: 6917

Setting stacked=False instead of 'false' solved the first problem for me!

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from datetime import datetime as dt
mpl.style.use('ggplot')

# 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, 0, 0)
date_to = dt(2015, 12, 22, 23, 0)
df = df.loc[date_from:date_to]

# Plot
df.plot(kind='area', stacked=False, alpha=0.5, colormap='Spectral',
        title='Area Plot')
[ax.legend(('Col1', 'Col2', 'Col3', 'Col4', 'Col5'),
           loc='upper right') for ax in plt.gcf().axes]

enter image description here

If anyone knows how to fix the legend position (see original question), let me know!

Upvotes: 1

Related Questions