I want to plot a histogram of two data sets in the following way:

enter image description here

I've written this code, and following are my results:

enter image description here

Can you help me out? What should I do?


closed as off-topic by MarcoB, anderstood, Coolwater, m_goldberg, José Antonio Díaz Navas May 7 '18 at 17:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – MarcoB, anderstood, Coolwater, José Antonio Díaz Navas
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    $\begingroup$ Please stop posting images of your code instead include a copyable working example. See asking guidelines. As stated in linked page you should show some effort, is Histogram[{a,b}] all you've tried? Have you seen its documentation? And 'see also' section? p.s. See BarChart/BinCounts etc too. $\endgroup$ – Kuba May 7 '18 at 10:56
  • $\begingroup$ Yes, I did try BarChart, but it just displays one value at one place, and not that how many times those numbers appeared. BinCounts also didn't help. $\endgroup$ – Rishabh May 7 '18 at 11:50
  • $\begingroup$ You have to pore through the examples. Try something like bins = Range[0, 3]/2. - 0.25; BarChart[Transpose[BinCounts[#, {bins}] & /@ {data1, data2}], ChartLabels -> {Range[0, 2]/2, None}, ImageSize -> 600]. $\endgroup$ – Michael E2 May 7 '18 at 12:03
  • $\begingroup$ Thanks Michael.. $\endgroup$ – Rishabh May 7 '18 at 13:09
  • $\begingroup$ You should look at HistogramList as a source of the data you need to transform your data into a bar chart. $\endgroup$ – m_goldberg May 7 '18 at 16:50

I don't like your choice of example datasets (which always produces mirror image histograms), so I am going to contrive my own.

SeedRandom[42]; data1 = RandomChoice[{0, 1/2, 1}, 100]
SeedRandom[24]; data2 = RandomChoice[{0, 1/2, 1}, 100]

Now, I will use HistogramList list to get the data transformed into something useful for making a bar chart.

{lblData, barHeight1} = HistogramList[data1, 3];
{notUsed, barHeight2} = HistogramList[data2, 3];
labels = Row[{#1, " – ", #2}] & @@@ Partition[lblData, 2, 1];

With the computed data, the bar chart can be made with

BarChart[Transpose[{barHeight1, barHeight2}], ChartLabels -> {labels, None}]



Not the answer you're looking for? Browse other questions tagged or ask your own question.