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

Dear Valeriu. Thank you. But I again receive the following error:

During evaluation of In[1]:= Table::itform: Argument data[[len,1]] at position 2 does not have the correct form for an iterator. >>

During evaluation of In[1]:= Table::itform: Argument data[[len,1]] at position 2 does not have the correct form for an iterator. >>

During evaluation of In[1]:= Set::partw: Part 4 of Table[25/81,10/27] does not exist. >>

During evaluation of In[1]:= Set::partw: Part 8 of Table[25/81,10/27] does not exist. >>

During evaluation of In[1]:= Set::partw: Part 16 of Table[25/81,10/27] does not exist. >>

During evaluation of In[1]:= General::stop: Further output of Set::partw will be suppressed during this calculation. >>

During evaluation of In[1]:= Table::itform: Argument 10/27 at position 2 does not have the correct form for an iterator. >>

During evaluation of In[1]:= BarChart::ldata: Table[25/81,10/27] is not a valid dataset or list of datasets. >>

During evaluation of In[1]:= BarChart::ldata: Table[25/81,10/27] is not a valid dataset or list of datasets. >>

During evaluation of In[1]:= BarChart::ldata: Table[25/81,10/27] is not a valid dataset or list of datasets. >>

During evaluation of In[1]:= General::stop: Further output of BarChart::ldata will be suppressed during this calculation. >>

Out[5]= BarChart[Table[25/81, 10/27], ChartLabels -> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, Frame -> True, GridLines -> {None, Automatic}]

POSTED BY: Alex Token

Dear Alex,

I have found the source of the error in the Mathematica 10. Table[0, data[[Length[data], 1]]] must have an iterator form Table[0, {i, data[[Length[data], 1]]}]. So, the following code works perfectly in Mma 10:

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, {i, 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}]  

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
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