如何绘制每个元素在列表中的次数

时间:2021-04-11 13:42:25

I'm trying to do something slightly that I wouldn't think would be hard, but I can't figure out how to get python/matplotlib/pylab to do.

我正在尝试做一些我认为不会很难的事情,但我无法弄清楚如何让python / matplotlib / pylab来做。

Given an input, I want a histogram that shows me the number of times each element appears.

给定一个输入,我想要一个直方图,显示每个元素出现的次数。

Given a list

给出一个清单

x=range(10)

I'd like output that has a single bar, y value of 10, equal to x=1, no other graphs.

我想要输出具有单个条形,y值为10,等于x = 1,没有其他图形。

given a list

给出一个清单

x=range(10)
x.append(1)

I'd like the output to have two bars, a y value of 9 for x=1, a y value of 1 for x=2. How can I do this?

我希望输出有两个条形,x = 1时y值为9,x = 2时y值为1。我怎样才能做到这一点?

4 个解决方案

#1


8  

This code gives you a histogram like the one you like:

此代码为您提供了您喜欢的直方图:

import matplotlib.pyplot as plt
import numpy as np
y = np.array([0,1,2,3,4,5,6,7,8,9,1])
plt.hist(y);
plt.show()

#2


5  

Something like this? This code uses a Counter to count the number of instances that an object occurs in an array (in this case counting the number of times an integer is in your list).

像这样的东西?此代码使用Counter来计算对象在数组中出现的实例数(在这种情况下,计算整数在列表中的次数)。

import matplotlib.pyplot as plt
from collections import Counter

# Create your list
x = range(10)
x.append(1)

# Use a Counter to count the number of instances in x
c = Counter(x)

plt.bar(c.keys(), c.values())
plt.show()

#3


1  

You'll of course have to start by counting the elements:

你当然必须从计算元素开始:

>>> from collections import Counter
>>> counts = Counter(my_iterator)

Then, we wish to count these counts:

然后,我们希望计算这些计数:

>>> count_von_count = Counter(counts)

Then you've got the size of your bars. Make it into a list and plot it:

那你就得到了酒吧的大小。将其放入列表并绘制它:

>>> bars = [count_von_count[i] for i in range(max(count_von_count) + 1)]

Example for your extended list:

扩展列表的示例:

>>> from collections import Counter
>>> counts = Counter([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1])
>>> counts
Counter({1: 2, 0: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1})
>>> count_von_count = Counter(counts.values())
>>> count_von_count
Counter({1: 9, 2: 1})
>>> bars = [count_von_count[i] for i in range(max(count_von_count) + 1)]
>>> bars
[0, 9, 1]

#4


0  

If you collect your data into a list of lists, then you might do something like this:

如果您将数据收集到列表列表中,那么您可能会执行以下操作:

import numpy as np
import matplotlib.pyplot as plt

x = [range(10)]
x.append([1])

count = map(len, x)
plt.bar(range(len(count)), count)
plt.show()

如何绘制每个元素在列表中的次数

Note that the first bar has height 10, not 9. I don't know if that is what you desire or if I'm misunderstanding your intention.

请注意,第一个栏的高度为10,而不是9.我不知道这是你想要的,还是我误解了你的意图。

#1


8  

This code gives you a histogram like the one you like:

此代码为您提供了您喜欢的直方图:

import matplotlib.pyplot as plt
import numpy as np
y = np.array([0,1,2,3,4,5,6,7,8,9,1])
plt.hist(y);
plt.show()

#2


5  

Something like this? This code uses a Counter to count the number of instances that an object occurs in an array (in this case counting the number of times an integer is in your list).

像这样的东西?此代码使用Counter来计算对象在数组中出现的实例数(在这种情况下,计算整数在列表中的次数)。

import matplotlib.pyplot as plt
from collections import Counter

# Create your list
x = range(10)
x.append(1)

# Use a Counter to count the number of instances in x
c = Counter(x)

plt.bar(c.keys(), c.values())
plt.show()

#3


1  

You'll of course have to start by counting the elements:

你当然必须从计算元素开始:

>>> from collections import Counter
>>> counts = Counter(my_iterator)

Then, we wish to count these counts:

然后,我们希望计算这些计数:

>>> count_von_count = Counter(counts)

Then you've got the size of your bars. Make it into a list and plot it:

那你就得到了酒吧的大小。将其放入列表并绘制它:

>>> bars = [count_von_count[i] for i in range(max(count_von_count) + 1)]

Example for your extended list:

扩展列表的示例:

>>> from collections import Counter
>>> counts = Counter([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1])
>>> counts
Counter({1: 2, 0: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1})
>>> count_von_count = Counter(counts.values())
>>> count_von_count
Counter({1: 9, 2: 1})
>>> bars = [count_von_count[i] for i in range(max(count_von_count) + 1)]
>>> bars
[0, 9, 1]

#4


0  

If you collect your data into a list of lists, then you might do something like this:

如果您将数据收集到列表列表中,那么您可能会执行以下操作:

import numpy as np
import matplotlib.pyplot as plt

x = [range(10)]
x.append([1])

count = map(len, x)
plt.bar(range(len(count)), count)
plt.show()

如何绘制每个元素在列表中的次数

Note that the first bar has height 10, not 9. I don't know if that is what you desire or if I'm misunderstanding your intention.

请注意,第一个栏的高度为10,而不是9.我不知道这是你想要的,还是我误解了你的意图。