0
$\begingroup$

I am trying to create a bar chart using data imported from Excel. This is how my excel file looks like.

Excel File

I created the following function:

    compareDIF[a_, b_] := (
dados = Import[a];
lateCondCC =dados[[b, 2, 4]];
lateCondEN = dados[[b, 2, 5]];
lateCondER = dados[[b, 2, 6]];
lateExtCC = dados[[b, 3, 4]];
lateExtEN = dados[[b, 3, 5]];
lateExtER = dados[[b, 3, 6]];
testCC = dados[[b, 4, 4]];
testEN = dados[[b, 4, 5]];
testER = dados[[b, 4, 6]];
BarChart[
{
lateCondCC, lateCondEN, lateCondER,
lateExtCC, lateExtEN, lateExtER,
testEN, testER,
},
ChartStyle->{{Black, White, Gray}},
BarSpacing->{0.4, 1.0},
ChartLegends->{{"Group 1", "Group 2", "Group 3"}},
PlotRange->{{0.3,9},{-0.3, 0.5}},
Frame->{False, True, False, False},
FrameLabel->{{"Time 1", "Time 2", "Time 3"}, "label"},
Ticks->{False, False},
GridLines->{{2.8, 4.8}, {0.0}},
GridLinesStyle-> Dashed,
ImageSize->550
]
);`compareDIF[a_, b_] := (
dados = Import[a];
lateCondCC =dados[[b, 2, 4]];
lateCondEN = dados[[b, 2, 5]];
lateCondER = dados[[b, 2, 6]];
lateExtCC = dados[[b, 3, 4]];
lateExtEN = dados[[b, 3, 5]];
lateExtER = dados[[b, 3, 6]];
testCC = dados[[b, 4, 4]];
testEN = dados[[b, 4, 5]];
testER = dados[[b, 4, 6]];
BarChart[
{
lateCondCC, lateCondEN, lateCondER,
lateExtCC, lateExtEN, lateExtER,
testEN, testER,
},
ChartStyle->{{Black, White, Gray}},
BarSpacing->{0.4, 1.0},
PlotRange->{{0.3,9},{-0.3, 0.5}},
Frame->{False, True, False, False},
Ticks->{False, False},
GridLines->{{2.8, 4.8}, {0.0}},
GridLinesStyle-> Dashed,
ImageSize->550
]
);

However, when I try running this function, Mathematica returns a bar chart with no data, such as this:

Bar Chart

I would like to know why my code is not showing the data. I know this must be a very basic question, but I cannot find what I did wrong.

Thank you

$\endgroup$
3
  • $\begingroup$ What about debugging it step by step? Have you tried to see what exactly does {lateCondCC, lateCondEN, lateCondER... contain? $\endgroup$
    – Kuba
    Apr 24, 2018 at 20:29
  • $\begingroup$ show how you use it. is b=1 ? $\endgroup$
    – george2079
    Apr 24, 2018 at 20:46
  • $\begingroup$ I use b = 4 because the data is on the fourth sheet of my excel file. I use it like this: data = compareDif['file path', 4] $\endgroup$ Apr 24, 2018 at 23:22

1 Answer 1

3
$\begingroup$

Assuming it is actually the case that the file you are trying to Import is laid out exactly as shown in the attached screenshot then evaluating something like Import[<path to file>] should return the following:

{ 
  { 
    {"", "", "", ""}, 
    {"", "", "", ""}, 
    {"", "Time 1", "Time 2", "Time 3"}, 
    {"Group 1", 1., 2., 3.}, 
    {"Group 2", 3., 2., 1.}, 
    {"Group 3", 1., 2., 3.}
   }
  }

Notice how Dimensions[%] evaluates to {1, 6, 4}; this means that in function compareDIF[a_, b_]-assuming a stands for the path to the file, which it should, since it is the only argument in Import[a]-b is in all probability redundant or defaults-manually-to 1 .

Given the Dimensions of the imported expression, it seems that the third index in the Part expressions must reference its rows while the second index is used to represent the columns; provided all this is correct, the variables ...Cond... correspond to the first column of data, the variables ...Ext... correspond to the second column of data and lastly the test... variables correspond to the last column of data.

If my guess is correct, then I think that the following template should help you obtain the result you're opting for-with appropriate adjustments:

BarChart[
  Transpose[Drop[Drop[impt[[1]], 3], None, 1]],
  ChartLabels -> {{"Time 1", "Time 2", "Time 3"}, None},
  ChartLegends -> SwatchLegend[Automatic, {"Group 1", "Group 2", "Group 3"}],
  ChartStyle -> {Automatic}
 ]

enter image description here

(in the place of Automatic you can put eg Black, White, Gray and use the rest of the Options you prefer-impt above stands for Import[a])

ps. in the initial code, the lists containing the variables in BarChart had a trailing , which probably didn't help.

$\endgroup$
1
  • $\begingroup$ Ok, now I understand what the problem is. When asking Mathematica to import data from Excel, I was indexing the wrong way. In my Excel file, the variable Time 1 in Group 1 is in cell B4. If the second index in Part expressions must reference its rows and the third must reference its columns, then the indexing should be [[1, 4, 2]] in order to retrieve data from cell B4. I was doing the opposite of that - lateCondCC =dados[[b, 2, 4]], which retrieves data from cell D2. Since this is an empty cell, no data appears on my bar chart. Thanks for the help! $\endgroup$ Apr 25, 2018 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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