简体   繁体   English

堆积条形图使用python matplotlib正面和负面值

[英]Stacked bar charts using python matplotlib for positive and negative values

I am trying to plot a stacked bar chart with python Matplotlib and I have positive and negative values that I want to draw. 我试图用python Matplotlib绘制一个堆积的条形图,我有正负值,我想绘制。 I have had a look at other posts talking about how to plot stacked bar charts with positive and negative values, but none of them has done it using Matplotlib so I couldn't find my solution. 我已经看过其他帖子,谈论如何绘制带有正值和负值的叠加条形图,但是他们都没有使用Matplotlib完成它,所以我找不到我的解决方案。 Stacked bar chart with negative JSON data d3.js stacked bar chart with positive and negative values Highcharts vertical stacked bar chart with negative values, is it possible? 具有负JSON数据的 堆积条形图d3.js具有正值和负值的 堆积条形图具有负值的Highcharts垂直堆积条形图,是否可能?

I have used this code to plot stacked bar chart in python with matplotlib: 我使用此代码在matthon中使用matplotlib绘制堆积条形图:

import numpy as np
import matplotlib.pyplot as plt
ind = np.arange(3)
a = np.array([4,-6,9])
b = np.array([2,7,1])
c = np.array([3,3,1])
d = np.array([4,0,-3])
p1 = plt.bar(ind, a, 1, color='g')
p2 = plt.bar(ind, b, 1, color='y',bottom=sum([a])) 
p3 = plt.bar(ind, c, 1, color='b', bottom=sum([a, b]))
p4 = plt.bar(ind, d, 1, color='c', bottom=sum([a, b, c]))
plt.show()

The above code gives the following graph: 上面的代码给出了以下图表:

在此输入图像描述

This is not giving me correct results. 这不是给我正确的结果。 Can someone tell me how I can code it whether using additional if else statement or any other way that would plot both negative and positive values in stacked bar charts. 有人可以告诉我如何编码它是否使用额外的if else语句或任何其他方式可以在堆积条形图中绘制负值和正值。

I am expecting to get the results as in the following chart: expected chart 我期望得到如下图表中的结果: 预期图表

Now the first column is plotted correctly as it has all the positive values, but when python works on the second column, it gets all mixed up due to negative values. 现在第一列被正确绘制,因为它具有所有正值,但是当python在第二列上工作时,由于负值,它会全部混淆。 How can I get it to take the positive bottom when looking at positive values and negative bottom when plotting additional negative values? 在绘制其他负值时,如何在查看正值和负底值时获取正底部?

The bottom keyword in ax.bar or plt.bar allows to set the lower bound of each bar disc precisely. ax.bar或plt.bar中的bottom关键字允许精确设置每个条形盘的下限。 We apply a 0-neg bottom to negative values, and a 0-pos bottom to positive values. 我们将0-neg底部应用于负值,将0-pos底部应用于正值。

This code example creates the desired plot: 此代码示例创建所需的绘图:

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()

This is the resulting plot: 这是结果图:

Matplotlib中带有pos / neg值的堆积条形图

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

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