简体   繁体   English

Python中具有正值和负值的堆积面积图

[英]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. 我想做一个堆叠的面积图,其中一些组为正,因此将出现在x轴上方(堆叠),而其他组为负,因此将出现在x轴下方。 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. 在我执行stackplot的那一刻,它只是添加了实际值,因此带有负值的组不会出现在图中,但是所有其他区域都向下移动。 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. 基本上,我想组合两个面积图,一个用于x轴上方的正向基团,另一个用于x轴下方的负向基团。

Assume you have a pandas DataFrame df with the groups as columns, then one can do something like: 假设您有一个以组为列的pandas DataFrame df ,那么可以执行以下操作:

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: 以下代码可在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. 该例程实际上用于绘制具有数千个x轴数据点的图表。 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: 以下是生成的真实图表的示例:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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