简体   繁体   中英

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:

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. How would I go about that?

The x-axis would be like 1, 2, 3, 4 - while the y-axis would be from 0 to 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. By plotting your a and b data in a histogram, you would find 4 bars of height 1 in each set.

I understand that what you have is the processed frequency and want to plot it in arbitrary bins. 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 :

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. Probably not what you want if you are willing to implement this for more than once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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