Group Abstract Group Abstract

Message Boards Message Boards

[?] Plot probability distribution?

Posted 8 years ago

Hi All. I have an issue with plotting. The following data shows the probability distribution of a random variable X (here 25/81, 30/81 etc are the probabilities):

data={{1,25?81},{2,30?81},{4,19?81},{8,6?81},{16,1?81}}

I would like to plot a bar-chart like in the image below. However, using for example, Histogram[WeightedData @@ Transpose[data], Length[data]] command I get a plot which is different. I would like the one where the bars are spaced like in the image below. I appreciate your help.

enter image description here

POSTED BY: Alex Token
10 Replies
Posted 8 years ago

Thank you very much Valeriu. It worked well, have a nice day.

POSTED BY: Alex Token

Yes, I think so. I have done a simple modification in the code which must exclude the trouble. Try this:

data = {{1, 25/81}, {2, 30/81}, {4, 19/81}, {8, 6/81}, {16, 1/81}};
len = Length[data];
data1 = {Range[data[[1, 1]], data[[len, 1]]], Table[0, data[[len, 1]]]};
Do[data1[[2, data[[i, 1]]]] = data[[i, 2]], {i, len}];
BarChart[data1[[2]], ChartLabels -> data1[[1]], Frame -> True, GridLines -> {None, Automatic}]
Posted 8 years ago
POSTED BY: Alex Token

Dear Alex,

I have executed the code from your file. The result you can see below:

enter image description here

What is the version of your Wolfram Mathematica?

Posted 8 years ago

Dear Valeriu. I am using Wolfram 10.0. Maybe this is the issue? Thank you.

POSTED BY: Alex Token
Posted 8 years ago

Thank you for your reply. I copied and pasted the code but it does not produce the same outcome as in yours. I attach here a mat file. Thank you.

Attachments:
POSTED BY: Alex Token

Does the following code satisfy your desire?

data = {{1, 25/81}, {2, 30/81}, {4, 19/81}, {8, 6/81}, {16, 1/81}};
data1 = {Range[data[[1, 1]], data[[Length[data], 1]]], 
   Table[0, data[[Length[data], 1]]]};
Do[data1[[2, data[[i, 1]]]] = data[[i, 2]], {i, Length[data]}];
BarChart[data1[[2]], ChartLabels -> data1[[1]], Frame -> True, 
 GridLines -> {None, Automatic}]

enter image description here

Posted 8 years ago

Thank you very much for your help. I also noticed the numbers on the horizontal axis are evenly spaced between each other. This is the issue in my plot. Realistically, the distance between “8” and “16” is not the same as the distance between “1” and “2”. Is it possible to “correct” the axis to take care of this? Thank you.

POSTED BY: Alex Token

Use BarChart:

data={{1,25/81},{2,30/81},{4,19/81},{8,6/81},{16,1/81}}
BarChart[data[[All,2]],ChartLabels->data[[All,1]],Frame->True,GridLines->{None,Automatic}]

You can fine-tune the axes, legend, gridlines and so on…

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard