3
$\begingroup$

I'm trying to make a legend by hand. I know you can do it with various functions, but I'd like to do it by hand. All I really want is, I pass it a list of pairs of the form {label, legend properties}, and it will display a line segment with those properties next to the label. Here's my code:

labellist = {"a", "b", "c"};
dashingunit = .005;
thick = .005;
proplist = {Directive@{Dashing@dashingunit, Thickness[thick]}, 
   Directive@{Dashing@(3*dashingunit), Thickness[thick]}, 
   Directive@{Thickness[thick]}};
labelproplist = Transpose@{labellist, proplist};

llength = .8;
linelist = 
  Table[Row@{Graphics[{labelproplist[[i, 2]], 
       Line[{{0, 0}, {llength, 0}}]}, ImagePadding -> 0], 
     Style[labelproplist[[i, 1]], 18]}, {i, Length@labelproplist}];
Print@linelist;
Print@Column@linelist;

And it spits out: enter image description here

I've selected the middle one to emphasize what I mean. For some reason, it's adding a ton of whitespace above and below each line, so when I form the Column[], they're not that close to each other.

Adding ImagePadding -> 0 to Graphics[] doesn't change anything.

Why is it adding this whitespace, and how can I get rid of it?

$\endgroup$
4
  • $\begingroup$ Easiest workaround would be to add , AspectRatio -> 1/3 to your Graphics call. i.sstatic.net/CkKjA.png $\endgroup$
    – Jason B.
    Sep 26, 2016 at 19:30
  • $\begingroup$ @JasonB thanks, maybe I'll try that. I want to know why the whitespace is there though. Does AspectRatio scale the image (as in, will it stretch the line and distort the dashing)? $\endgroup$ Sep 26, 2016 at 19:36
  • 2
    $\begingroup$ I can't speak to how Graphics determines the ImageDimensions when they aren't supplied, and it will even change these based on whether you are viewing just the Graphics or part of a list. So the safest thing to do is to supply your own ImageSize option (both x and y) $\endgroup$
    – Jason B.
    Sep 26, 2016 at 19:43
  • $\begingroup$ If you explicitly set the plot range in Graphics, e.g. PlotRange -> {{0, 1}, {-0.1, 0.1}} you can get rid of the whitespace. $\endgroup$
    – N.J.Evans
    Sep 26, 2016 at 20:07

1 Answer 1

5
$\begingroup$

This is probably caused by Mathematica trying to force a set aspect ratio on your graphics. Since a mathematical horizontal line has an aspect ratio of 0 my guess is that somewhere Mathematica divides by the aspect ratio (0) leading it into difficulties.

Compare the results for two lines that are virtually identical:

Graphics[Line[{{0, 0}, {1, 0}}]]

Mathematica graphics

and

Graphics[Line[{{0, 0.0000001}, {1, 0}}]]

Mathematica graphics

Anything that will give the graphics object a non-zero vertical dimension will work, for instance adding an invisible point slightly off the x-axis:

Graphics[
  {Line[{{0, 0}, {1, 0}}], 
   RGBColor[{0, 0, 0, 0}], Point[{0, 0.00000001}]
  }
]

Mathematica graphics

A good combination of ImageSize and AspectRatio will work as well:

Graphics[{Line[{{0, 0}, {1, 0}}]}, ImageSize -> {400, 10}, AspectRatio -> 1/50]

Mathematica graphics

$\endgroup$
4
  • 1
    $\begingroup$ Awesome, this looks like it works! $\endgroup$ Sep 26, 2016 at 20:59
  • $\begingroup$ It doesn't divide by zero, it's just avoiding degenerate graphics. $\endgroup$ Sep 26, 2016 at 21:42
  • $\begingroup$ @BrettChampion You should know, of course, but I'd be tempted to say that a simple horizontal line shouldn't be that difficult to draw in a reasonable way. $\endgroup$ Sep 26, 2016 at 21:50
  • 3
    $\begingroup$ Graphics doesn't allow degenerate values for PlotRange and will use {-1,1} if all the values are zero, or {0, 2v} if all the values are v. $\endgroup$ Sep 26, 2016 at 22:01

Your Answer

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

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