简体   繁体   English

如何将 plot 两个 arrays 的数据做成柱状图并排在一起

[英]How to plot two arrays of data as histograms next to eachother

This might be a very simple question, but I can't figure it out for some reason, and I need to get moving with my work.这可能是一个非常简单的问题,但由于某种原因我无法弄清楚,我需要继续我的工作。

If I have two arrays:如果我有两个 arrays:

a = [3 6 4 9]
b = [4 8 2 7]

and I want to plot them in the form of a histogram, with the pillars next to eachother for each index.我想要 plot 以直方图的形式显示它们,每个索引的支柱彼此相邻。 How would I go about that?我怎么会go呢?

The x-axis would be like 1, 2, 3, 4 - while the y-axis would be from 0 to 10. x 轴可能是 1、2、3、4,而 y 轴可能是 0 到 10。

I think you are misunderstanding the concept of histogram, since the x-axis in a histogram is the bins while the y-axis the frequency.我认为您误解了直方图的概念,因为直方图中的 x 轴是 bin,而 y 轴是频率。 By plotting your a and b data in a histogram, you would find 4 bars of height 1 in each set.通过在直方图中绘制ab数据,您会在每组中找到 4 个高度为 1 的条。

I understand that what you have is the processed frequency and want to plot it in arbitrary bins.我知道您拥有的是处理后的频率,并希望将其放入任意垃圾箱中的 plot。 I would suggest that you use directly the raw data and pyplot.histogram , but to directly plot the data you have shown you may use pyplot.bar :我建议您直接使用原始数据和pyplot.histogram ,但要直接使用 plot 您显示的数据,您可以使用pyplot.bar

import random
import numpy
from matplotlib import pyplot

a = [3, 6, 4, 9]
b = [4, 8, 2, 7]

x = numpy.array([0,1,2,3])

pyplot.bar(x, a, 0.3)
pyplot.bar(x + 0.3, b, 0.3)
pyplot.show()

Note the x-axis hack to show the bars next to each other.请注意 x 轴 hack 以显示彼此相邻的条。 Probably not what you want if you are willing to implement this for more than once.如果您愿意不止一次实施此操作,则可能不是您想要的。

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

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