问题描述
如何在numpy/matplotlib或Pandas中获得排序的累积图?
How can I get sorted cumulative plots in numpy/matplotlib or Pandas?
让我用一个例子来解释一下.假设我们有以下数据:
Let me explain this with an example. Say we have the following data:
number_of_items_sold_per_store = [10, 6, 90, 5, 102, 10, 6, 50, 85, 1, 2, 3, 6]
我们想绘制一个图表,对于给定的(x,y)值,其读取方式为:销售量最高的%X
个商店销售的%Y
个商品.也就是说,它显示数据如下:
We want to plot a chart that, for a given (x,y) value is read as: the top %X
selling stores sold %Y
items. That is, it displays the data as follows:
最畅销商店位于左侧(即地块的斜率单调减小).我该如何在numpy或Pandas中执行此操作? (即假设以上是系列).
where the best selling stores are to the left (i.e. the slope of the plot decreases monotonically). How can I do this in numpy or Pandas ? (i.e. assuming the above is a Series).
推荐答案
假设您希望表现最好的商店排在首位:
Assuming that you want the best performing stores to come first:
import numpy as np
import matplotlib.pyplot as plt
number_of_items_sold_per_store = [10, 6, 90, 5, 102, 10, 6, 50, 85, 1, 2, 3, 6]
ar = sorted(number_of_items_sold_per_store,reverse=True)
y = np.cumsum(ar).astype("float32")
#normalise to a percentage
y/=y.max()
y*=100.
#prepend a 0 to y as zero stores have zero items
y = np.hstack((0,y))
#get cumulative percentage of stores
x = np.linspace(0,100,y.size)
#plot
plt.plot(x,y)
plt.show()
这篇关于排序累积图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!