I have a matrix, here called result that I want to place in a grid. The first rows of the matrix newcolumnlabels contains the column labels each of which is a string containing the column label rotated 90 degrees, so that each column label is but one character wide, as are the subsequent values in the remaining rows. This matrix is obtained by selecting among a matrix of row value pairs as seen from the commented out line immediately below:

 (\*Grid[Join[{newcolumnlabels},Map[Column[#,Alignment\[Rule]Center]&/@\Transpose[{Values@#}]&]@Transpose@result],Frame\[Rule] All]\*)

However, for purposes of example, the values are irrelevant and are simply created arbitrarily using a couple of ConstantArrays to demonstrate the column labeling issue I encountered but can not find a workaround. Nonetheless, like the original data derived from values of a key->value pair, the datum for each element is a single character. Likewise, the columnlabels used below are simply numbered consecutively here for simplicity.

If the following code is run, one gets a nicely formatted grid, with the label values each properly rotated by 90 degrees and placed into a column a single character wide, as desired, except for some some row labels that remain to be added.

 n = 50;

 result = Join[ConstantArray["A", {3, n}], ConstantArray["T", {3, n}]];

 columnlabels = Range[Last[Dimensions[result]]];
 newcolumnlabels = 
 Rotate[StringTake["00000" <> ToString[#], -5], \[Pi]/2] & /@ columnlabels;
 Map[Column[#, Alignment -> Center] & /@ Transpose[{#}] &]@result], 
 Frame -> All]

However, if one sets n=100 and runs the remaining commands again, one gets each column label and subsequent columns as before, but with more (undesired) space added on either side. If one sets n = 200 then runs the remaining commands again the labels are no longer properly rotated 90 degrees, but rather 180 degrees and not in a simple linear fashion that suggests some spacing issue or some long line issue in the notebook front end is involved.

How can one force the labels in the output to remain properly rotated 90 degrees in a single column one character wide (as when n=50) even for a matrix with very many columns? As I may have as many as 20,000 columns, but more typically 2000-3000 columns, each requiring a suitably formatted label, it is important that each column be as narrow as possible (a single character wide with a minimum of spacing for readability). Currently I do not understand why, with respect to label formatting, the results for larger values of n are different than those for n=50.

I've played with options Alignment and Spacings, but to no practical avail, but perhaps I've missed something subtle.

Creation of a large grid takes quite a while and I wonder if exporting the resultant graph to a .PDF file might not be faster than the time it takes to reformat the notebook given the very long lines in the output.


You can wrap Grid with Style with the option LineBreakWithin -> False :

n = 100;
result = Join[ConstantArray["A", {3, n}], ConstantArray["T", {3, n}]];
columnlabels = Range[Last[Dimensions[result]]];
newcolumnlabels = Rotate[StringTake["00000" <> ToString[#], -5], \[Pi]/2] & /@ 

Style[Grid[Prepend[result, newcolumnlabels], Frame -> All, 
   Alignment -> {Center, Center}, Spacings -> {0, 1}], 
 FontSize -> 8, 
 LineBreakWithin -> False]

enter image description here

| improve this answer | |
  • $\begingroup$ The LineBreakWithin Function is extremely useful, but as yet not fully integrated into the WL as of 12.1. Now, one can only hope it stays there. That it is listed within the Text Layout Options set of functions is encouraging. This is particularly nice since Spacings will accept real values (eg. {0.5,1} as well as integers for finer control for purposes of visibility. Thanks for the insight. $\endgroup$ – Stuart Poss May 6 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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