简体   繁体   English

堆积面积图的问题,一直显示百分比堆积面积图?

[英]Problem with stacked area chart, keeps showing up percent stacked area chart?

So I've spent about 10 hours on this simple task already, I'm out of patience.所以我已经在这个简单的任务上花了大约 10 个小时,我已经没有耐心了。 I'm unable to get the chart I want.我无法获得我想要的图表。

What I want:我想要的是:

在此处输入图片说明

What I get:我得到的:

在此处输入图片说明

Yes I am using percentages, but I do not want my chart to be full, I just want the percentages to be stacked one on top of eachother.是的,我正在使用百分比,但我不希望我的图表是完整的,我只想将百分比堆叠在一起。

My code:我的代码:

plt.figure()
plt.stackplot(unique_years, my_percentages[:, 0], my_percentages[:, 1],my_percentages[:, 2], labels=['Petrol','Diesel', 'Hybrid'])
plt.legend(loc='upper left')
plt.show()

The my_percentage is a numpy table with 3 columns named 0,1 and 2, each representing a type of fuel as a percentage. my_percentage 是一个 numpy 表,有 3 列名为 0,1 和 2,每列代表一种燃料类型的百分比。

Unique_years is an array for each unique year (1997-2020). Unique_years 是每个唯一年份(1997-2020)的数组。

Can anybody please help?有人可以帮忙吗? I am desperate lol我绝望了哈哈

You need to put the three lists into a list:您需要将三个列表放入一个列表中:

from matplotlib import pyplot as plt
import numpy as np

unique_years = np.arange(1997, 2021)
my_values = np.random.rand(len(unique_years), 5)
my_percentages = 100 * my_values / my_values.sum(axis=1, keepdims=True)
plt.stackplot(unique_years, [my_percentages[:, 0], my_percentages[:, 1], my_percentages[:, 2]])
plt.show()

带有 3 个列表的堆栈图

Or you could just transpose the percentages array:或者你可以转置百分比数组:

from matplotlib import pyplot as plt
from matplotlib.ticker import PercentFormatter
import numpy as np

unique_years = np.arange(1997, 2021)
my_values = np.random.rand(len(unique_years), 5)
my_percentages = 100 * my_values / my_values.sum(axis=1, keepdims=True)
plt.stackplot(unique_years, my_percentages.T[:3], labels=['fuel a', 'fuel b', 'fuel c'], colors=plt.cm.Set2.colors[:3])
plt.margins(x=0)
plt.gca().yaxis.set_major_formatter(PercentFormatter(100))
plt.legend(loc='upper left', bbox_to_anchor=[1.02, 1.02])
plt.tight_layout()
plt.show()

带有转置数组的堆栈图

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

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