Message Boards Message Boards

0
|
201 Views
|
14 Replies
|
4 Total Likes
View groups...
Share
Share this post:

BarChart for multilevel nested lists

Posted 3 days ago

Hello, I have a large nested list nl (over 100 nested lists) of nested pairs of elements {{a,b},{{c,d},{e,f},{h,g}}, {{g,h},{e,h}}, {h,i},...} but nested at deeper levels, I like to draw BarChart, but it is not working, it only works when I set a loop, like For i to n, barchart each element (i) of the list, but i want all the nested lists of the pair of elements to be drawn in one Row next to each other with a legend name for each one.
When i Input Barchart[nl], it ignores the input and keeps reprinting my list.
How can i BarChart all the nested lists of the pair of elements, of nl including the deeper levels in one row?
Appreciate any help

Thanking you

POSTED BY: Adam Cherad
14 Replies

Hello Michael, Thanks for your reply. I am afraid i don't understand at all where you are coming from when you say i quote " it would really be much easier if you showed us a picture or gave us the code that almost works so we don't have to keep guessing at how to interpret your words"

A picture of what? which code? I have nothing of the sort.

I have only my data which i managed to set into nested lists as i have explained, and the sample you chose is good one, notably, {{{2, 3}}, {{4, 5}, {6, 7}, {9, 8}}, {{{8, 4}, {6, 9}}, {{3, 5}, {5, 2}, {7, 8}}}}, this is it! my data is structured in exactly the same way, please consider all single pairs as a nested list as well as i just did to the first one {{2,3}}. My list nl just carries on along the same structure, namely, single pair, 2 pairs, 3 pairs...for over 100 lists. I can't be clearer than this. I have nothing else no picture no code. As i admitted i am a beginner with Mathematica and i was happy having so far been able to set my data into a nested list like i did. Again, all i want is to be able to plot about 15 (+/-5 depending on the requirement but this should not be relevant to you) of my nested lists as separate sets of bar charts (each nested set showing 2 bars of different colors for each pair (which represent a code and a frequency if you must know) it contains either 1 pair,2 pairs, or 3 pairs)) next to each other in a row. From your feedback, i am guessing you are complicating the issue by thinking too much.

Moreover, i am altogether surprised when you say "You can berate us or you can give us what we're asking for so that you can get your problem solved" How can i berate you when i am trying to learn from you. And how can i be holding something to stop you from helping me? You are wrong here,

Anyway, i thank you again and trust with the above clarification, you would catch my drift .

Sincerely, Adam Cherad

POSTED BY: Adam Cherad
Posted 13 hours ago

A picture of what? which code? I have nothing of the sort.

You said in your original post:

it only works when I set a loop, like For i to n, barchart each element (i) of the list

That led me to believe that you were able to achieve an approximation of what you want using a for loop. If you just provide us with that code, we can run it ourselves and see what you're trying to achieve. As for a picture, the picture would either be the result of the for loop, or just something you drew yourself to represent what you want. If you know what you want the result to look like for a given input, then you can draw a picture of that or write "fake" code that creates a picture in a hard-coded fashion. Whatever. Just something that reduces the ambiguity.

I hate to belabor this, but you keep saying surprising things. Here is an exact quote of what you just recently said:

{{{2, 3}}, {{4, 5}, {6, 7}, {9, 8}}, {{{8, 4}, {6, 9}}, {{3, 5}, {5, 2}, {7, 8}}}}, this is it! my data is structured in exactly the same way, please consider all single pairs as a nested list as well as i just did to the first one {{2,3}}. My list nl just carries on along the same structure, namely, single pair, 2 pairs, 3 pairs...for over 100 lists. I can't be clearer than this.

Well, maybe you can't be clearer, but this is absolutely, definitely, NOT clear. A list that is "shaped" like "single pair, 2 pairs, 3 pairs, etc" would look like this:

{{{1,2}}, {{3,4}, {5,6}}, {{7,8}, {9,10}, {10,11}}}

but your nl has deeper structure than this. In words, your nl is "single pair, three pairs, a list with two sublists that themselves contain 2 pairs and 3 pairs". I see no obvious pattern here, so I have no idea what to expect next. I have no idea how deep this structure will go.

But now we're just arguing over how clear your language has been to this point. And that really doesn't matter. We're trying to give you suggestions for how to improve your question, but you just keep insisting that you've already been perfectly clear. Well, that's fine. Just don't expect us to be very helpful, because we're stumped until you give us more to go on. It's your project. Up to you whether the effort is worth it.

POSTED BY: Eric Rimbey
Posted 22 hours ago

I think I'm starting to understand, but again, it would really be much easier if you showed us a picture or gave us the code that almost works so we don't have to keep guessing at how to interpret your words. I know you think you're being clear and that you're amazed that we don't understand, but that's just how it is: we don't understand. You can berate us or you can give us what we're asking for so that you can get your problem solved.

Anyway, what I think I now understand is that you have two problems. You need to "flatten" a data structure in a particular way so that it can be input into BarChart, and you need to put several bar charts into a row. The latter problem is trivial: just use Map along with whatever row/grid structure you want. The first problem is ambiguous, because you haven't given us enough information. Let me demonstrate explicitly.

Here's the sample data you gave us:

nl = {{2, 3}, {{4, 5}, {6, 7}, {9, 8}}, {{{8, 4}, {6, 9}}, {{3, 5}, {5, 2}, {7, 8}}}};

There are two ways to "flatten" this so that each element of nl can be a bar chart:

nl1 = {{2, 3}, {{4, 5}, {6, 7}, {9, 8}}, {{8, 4}, {6, 9}}, {{3, 5}, {5, 2}, {7, 8}}};
nl2 = {{2, 3}, {{4, 5}, {6, 7}, {9, 8}}, {{8, 4}, {6, 9}, {3, 5}, {5, 2}, {7, 8}}};

Which is the one you want?

As for the second problem, I'll assume you want GraphicsRow:

GraphicsRow[BarChart /@ nl1]

enter image description here

Both BarChart and GraphicsRow have options that you can play with to get the display just the way you want. As it stands, the tick labels are obscured, but you can fix that. Or use plain Row instead. Or you can add a layer of List and use Grid or GraphicsGrid.

Grid[{BarChart /@ nl1}]

enter image description here

POSTED BY: Eric Rimbey

Hi Eric I apologise my previous email was supposed to be addressing you. I am digesting your reply and i believe in one way you got it right.

Many thanks Adam Cherad

POSTED BY: Adam Cherad

Hi MiChael, I just looked closer at the bar charts you produced but you seem to have considered only the 2nd number of the pair. I apologize for the confusion; it is clear i am a beginner learning Mathematica, and obviously, my vision is still warped with articulating the critical pivots for an experienced programmer. Anyway, in case you are still interested at wasting few more moments of your time, i clarify as follows: You took the sample nl = {{2, 3}, {{4, 5}, {6, 7}, {9, 8}}, {{{8, 4}, {6, 9}}, {{3, 5}, {5, 2}, {7, 8}}}}; and for this sample what i expected were (ignoring the first list {2,3} which i will nest later), 1) One bar chart set for {{4, 5}, {6, 7}, {9, 8}} with 3 pairs of bars (4-5), (6-7), and (9-8) (call it set1 as a legend), meaning each one of the numbers in the pair should be reflected by a bar. 2) and next to it another Bar chart for {{8, 4}, {6, 9}}, with 2 pairs of bars (8-4) and (6-9) (call it set2) and 3) then another for {{3, 5}, {5, 2}, {7, 8}} with 3 pairs of bars (3-5), (5-2), (7-8) call i t set 3) I hope it is clearer Adam Cherad

POSTED BY: Adam Cherad

Nope, still don't get it. I need an example nl. I, too, am truly taken aback by how difficult it is to provide a small working example. The bar charts you show in a row lack legends, even though I explicitly asked about them.

Here's another shot in the dark, probably still wrong:

nl = {{2, 3}, {{4, 5}, {6, 7}, {9, 8}}, {{{8, 4}, {6, 9}}, {{3, 5}, {5, 2}, {7, 8}}}};
GraphicsRow@
 Cases[nl, 
  data_?MatrixQ :> 
   BarChart[data[[All, 2]], ChartLegends -> data[[All, 1]]], Infinity]

enter image description here

Please note that {2, 3} is left off. There is "a separate legend for each nest of either 1 pair, 2 pairs or 3 pairs," but {2, 3} is not in a "nest."

I guess I should just stop. It's an obvious thing, and I don't see it. I'm probably not helping. Good luck! Someone else will probably show me where I'm confused.

POSTED BY: Michael Rogers

Hi there, I am truly taken aback by how much confusion the set up of my query has caused. All i want is to lay a set of bar charts next to each other on a row. The different barcharts represent lists of different lengths which are nested in the main list {nl}, that is all. It should not matter what the nature of the elements is. Even the nesting level could be an arbitrary (L). I did mention i have over a 100 nested list, but it should not be relevant since the code for doing it for 10 sets of barcharts will work for 15 or 20 (i do not intend to lay all the 100 lists in one row) example Thank you Michael so much in any case for taking the time.

Thank you again, Michael, You were not shooting in the dark, that's about all i need, and i thought my asking for exactly that was explicit. Setting up legends was never a problem, i did mention it earlier just to indicate that each set of bar charts would have its own name (and i know how to do that). Very grateful, Cheers, Adam

POSTED BY: Adam Cherad

By opening my post with "my interpretation," I meant to imply I wasn't confident I understood what was desired. I'm still not. My suggestion at the end of my post is "to give a simple example of nl...and show exactly what you want the arguments of BarChart to become." Maybe drawing what you want would be easier, if you don't know how to do it with BarChart.

Have you seen GraphicsRow[]? It's a way to arrange several bar charts in a row. And some people just use Row[], which is a way to arrange any expressions in a row.

In your example in the OP, {a,b} is a pair at level 1; {c, d} is on level 2, apparently (again, I'm interpreting) to be grouped with the following two pairs; at the end we get {h, j} at level 1 but not, I suppose, to be group with {a, b}, even though both are at the same level. I don't know if there are pairs at deeper levels. You mention hundreds of nested lists, and the lack of a consistent structure leads me to imagine arbitrary levels of nesting. Further I cannot imagine putting hundreds of bar charts in a row and have it be legible. I'm just confused.

Without an example, I do not think I can help further.

POSTED BY: Michael Rogers

Hi there, I am truly taken aback by how much confusion the set up of my query has caused. All i want is to lay a set of bar charts next to each other on a row. The different barcharts represent lists of different lengths which are nested in the main list {nl}, that is all. It should not matter what the nature of the elements is. Even the nesting level could be an arbitrary (L). I did mention i have over a 100 nested list, but it should not be relevant since the code for doing it for 10 sets of barcharts will work for 15 or 20 (i do not intend to lay all the 100 lists in one row) example Thank you Michael so much in any case for taking the time.

POSTED BY: Adam Cherad

Hello Eric "Does that mean you just want multiple BarCharts but arranged more elegantly?" Yes, but it is not for elegance, i need to see the multiple Barchart in one row for other purposes, Thanking you.

POSTED BY: Adam Cherad
Posted 1 day ago

I'm struggling to understand what you want.

All the pairs are made up of a code (integer) and a counter, or a frequency

Does that mean that you actually need to do a multiplication on each pair before displaying the chart?

I like the barchart to show them next to each other in a row, but with a separate legend for each

Does that mean you just want multiple BarCharts but arranged more elegantly?

How can i BarChart all the nested lists of the pair of elements, of nl including the deeper levels in one row?

Are you sure you even want BarChart? What you describe here doesn't sound like a bar chart to me. Have you tried the other chart functions?

it only works when I set a loop, like For i to n, barchart each element (i) of the list

Maybe you should provide this loop code so we can at least see an approximation of what you want. Show us the resulting picture and tell us how it differs from what you want.

POSTED BY: Eric Rimbey

Hello Michael, Many thanks for your time. The problem with Level 2, is that it opens up all the nested lists into individual pairs, that defeats the issue because Iike the nested pairs to be shown in the barchart as a group. Put simply, I have nested lists of 1 pair of elements {{a,b}}, 2 pairs {{a,e},{b,a}} and 3 pairs {{a,c}, {b,d}, {e,a}} All elements are integer numbers (All the pairs are made up of a code (integer) and a counter, or a frequency (also an integer). I like the barchart to show them next to each other in a row, but with a separate legend for each nest of either 1 pair, 2 pairs or 3 pairs. Thank you again Hello Eric "Does that mean you just want multiple BarCharts but arranged more elegantly?" Yes, but it is not for elegance, i need to see the multiple Barchart in one row for other purposes, Thanking you.

POSTED BY: Adam Cherad

Hello Michael, Many thanks for your time. The problem with Level 2, is that it opens up all the nested lists into individual pairs, that defeats the issue because Iike the nested pairs to be shown in the barchart as a group. Put simply, I have nested lists of 1 pair of elements {{a,b}}, 2 pairs {{a,e},{b,a}} and 3 pairs {{a,c}, {b,d}, {e,a}} All elements are integer numbers (All the pairs are made up of a code (integer) and a counter, or a frequency (also an integer). I like the barchart to show them next to each other in a row, but with a separate legend for each nest of either 1 pair, 2 pairs or 3 pairs. Thank you again

POSTED BY: Adam Cherad

Here's my interpretation of what you describe:

nl = {{2, 3}, {{4, 5}, {6, 7}, {9, 8}}, {{8, 9}, {6, 9}}};
With[{flat = Level[nl, {-2}]},
 BarChart[flat, ChartLegends -> {flat, None}, 
  ChartStyle -> {Automatic, None}]
 ]

Bar chart of data

An alternative code for flat is flat = Cases[nl, {__?NumericQ}, Infinity]. Then it would work if the data contained numeric expressions Pi or Sqrt[2], instead of just explicit numbers.

You might want to give a simple example of nl, as I have done, and show exactly what you want the arguments of BarChart to become. Unless I've accidentally hit the nail on the head, so to speak.

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

Group Abstract Group Abstract