python 条形图 负值_使用python matplotlib计算正负值的堆积条形图

中的底部关键字斧杆或者板条允许精确设置每个棒盘的下限。我们对负值应用0-neg底部,对正值应用0-pos底部。在

此代码示例创建所需的绘图:import numpy as np

import matplotlib.pyplot as plt

# Juwairia's data:

a = [4,-6,9]

b = [2,7,1]

c = [3,3,1]

d = [4,0,-3]

data = np.array([a, b, c, d])

data_shape = np.shape(data)

# Take negative and positive data apart and cumulate

def get_cumulated_array(data, **kwargs):

cum = data.clip(**kwargs)

cum = np.cumsum(cum, axis=0)

d = np.zeros(np.shape(data))

d[1:] = cum[:-1]

return d

cumulated_data = get_cumulated_array(data, min=0)

cumulated_data_neg = get_cumulated_array(data, max=0)

# Re-merge negative and positive data.

row_mask = (data<0)

cumulated_data[row_mask] = cumulated_data_neg[row_mask]

data_stack = cumulated_data

cols = ["g", "y", "b", "c"]

fig = plt.figure()

ax = plt.subplot(111)

for i in np.arange(0, data_shape[0]):

ax.bar(np.arange(data_shape[1]), data[i], bottom=data_stack[i], color=cols[i],)

plt.show()

这是结果图:

4DYKT.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值