简体   繁体   中英

Stacked area plot in Python with positive and negative values

I would like to do a stacked area plot where some groups are positive so will appear above the x-axis (stacked) and others are negative so will appear below the x-axis. At the moment when I do stackplot it just adds the actual values so the group with negative values in doesn't appear in the plot but all the other areas are shifted down. Basically I want to combine two area plots, one for the positive groups above the x-axis and one for the negative groups below the x-axis.

Assume you have a pandas DataFrame df with the groups as columns, then one can do something like:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
# split dataframe df into negative only and positive only values
df_neg, df_pos = df.clip(upper=0), df.clip(lower=0)
# stacked area plot of positive values
df_pos.plot.area(ax=ax, stacked=True, linewidth=0.)
# reset the color cycle
ax.set_prop_cycle(None)
# stacked area plot of negative values, prepend column names with '_' such that they don't appear in the legend
df_neg.rename(columns=lambda x: '_' + x).plot.area(ax=ax, stacked=True, linewidth=0.)
# rescale the y axis
ax.set_ylim([df_neg.sum(axis=1).min(), df_pos.sum(axis=1).max()])

Might not be exactly what you were looking for but I managed to do an area plot with negative and positive values. The code below worked on Python 3.7 / Windows 10 / Spyder IDE:

import matplotlib.pyplot as plt

x_axis = [1,2,3,4,5,6,7,8,9,10]
cheap = [-5,-4,-6,-8,-4,-2,-4,-8,-7,-3]
expensive = [3,4,8,7,9,6,4,3,2,3]


fig_size = plt.rcParams["figure.figsize"] #set chart size (longer than taller)
fig_size[0] = 39
fig_size[1] = 10
plt.rcParams["figure.figsize"] = fig_size
plt.rcParams.update({'font.size': 18}) 

plt.stackplot(x_axis, expensive, colors=['r'])
plt.stackplot(x_axis, cheap, colors=['g'])

plt.plot([],[],color='r', label='Above great case', linewidth=5)
plt.plot([],[],color='g', label='Below low case', linewidth=5)
plt.legend()

plt.xlabel('Years')
plt.ylabel('Number of companies')
plt.title('Under/over valuation over time')
plt.show()

The chart you should see: 在此处输入图片说明

This routine was actually used to plot a chart with thousands of x-axis data points. I tried before a bar chart and it was taking much longer to plot than this area version. Below is a sample of a real chart produced:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM