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()])
{pandas}可以像cd1>那样,用一个像cd1这样的数据框来做:
可能不是你想要的,但我成功地绘制了一个正负值的面积图。以下代码适用于Python 3.7/Windows 10/Spyder IDE:
您应该看到的图表:
这个例程实际上是用来绘制一个包含数千个x轴数据点的图表。我在条形图之前试过了,它比这个面积图要花更长的时间。以下是制作的真实图表示例:
相关问题 更多 >
编程相关推荐