TransWikia.com

Spacing of rotated elements (labels) in grid

Mathematica Asked by Jānis Lazovskis on April 6, 2021

How can I change the size of a specific subset of cells in a Grid? I’m using the ItemSize parameter for the ones I want to change, but it affects others, and I can’t control the sizes separately.

More detail: I want to present a large matrix (48×48) with labels for each column and row. The way I’m doing it right now is with Grid, by adding an extra row and column with the desired labels. However, since the entries are of varying lengths, I want to adjust the size of these Grid cells so that they’re all the same – but I want to leave the label cells unchanged.

Here is (a MWE of) my code to begin with:

labels = {"Long property name A", "Extremely long Property name B"};
Grid[Partition[Riffle[Join[
    {Null},
    Item[Rotate[#, -90 Degree], Alignment -> Bottom] & /@ labels,
    {1, 3000, 5, 7000}],
   Item[#, Alignment -> Right] & /@ labels, {4, -1, 3}], 3], 
 Frame -> All]

This is the result:

enter image description here

Now, I would like to change the size of the cells in the bottom right 4×4 submatrix, so that they all have the same size. I fix this by changing the table entry {1,3000,5,7000} to:

Item[#, ItemSize -> 4] & /@ {1, 3000, 5, 7000}

But then this is the result:

enter image description here

I tried adding the ItemSize parameter to the Items containing the labels, but the label text appears in a single line only if the cell is extremely wide, such as ItemSize->20, which I don’t want. So – how can I change the size of some of the cells without affecting the size of other cells?

2 Answers

Cheating by using Rasterize

labels = {"Long property name A", "Extremely long Property name B"};
Grid[Partition[
  Riffle[Join[{Null}, 
    Item[Rasterize[Item[Rotate[#, -90 Degree]]], 
       Alignment -> Bottom] & /@ labels, 
    Item[#, ItemSize -> 4] & /@ {1, 3000, 5, 7000}], 
   Item[#, Alignment -> Right] & /@ labels, {4, -1, 3}], 3], 
 Frame -> All]

enter image description here

No claim made that this is an acceptable answer.

Answered by Manuel --Moe-- G on April 6, 2021

I ran into the same problem, but Rasterize slowed down rendering a large grid significantly. Another workaround is to wrap your text in Row. It's maybe faster, more flexible.

labels = {"Long property name A", "Extremely long Property name B"};
Grid[Partition[
  Riffle[Join[{Null}, 
    Rotate[Row[{#}, ImageSize->{230, 20}, Alignment->{Right, Center}], -90 Degree]& /@ labels, 
    Item[#, ItemSize -> 4] & /@ {1, 3000, 5, 7000}], 
   Item[#, Alignment -> Right] & /@ labels, {4, -1, 3}], 3], 
 Frame -> All]

grid with rotated text

You'll need to specify the ImageSize for Row. Its width is the column header height and right align puts it at the bottom when rotated.

Answered by mgrigola on April 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP