Menu

#2473 [5.5] horizontal key misaligned with newhistogram

None
closed-fixed
nobody
None
2021-12-25
2021-11-21
Anonymous
No

While working on an answer for a question that involves newhistogram I discovered a weird behaviour of the key, if it is set to horizontal. Depending on the number of newhistogram's the key moves around the plot and eventually changes to vertical mode.

Code to reproduce:

N=7
set print $MYDATA
do for [i=1:N] {
    print sprintf('0.5 %f %f %f', rand(0)*20, rand(0)*20, rand(0)*20)
    print sprintf('1.0 %f %f %f', rand(0)*20, rand(0)*20, rand(0)*20)
    print sprintf("2.0 %f %f %f", rand(0)*20, rand(0)*20, rand(0)*20)
}
unset print

set style data histograms
set style fill solid 1.0
set key horizontal

plot newhistogram "System 1" lt 1, for [i=2:4] $MYDATA u i:xtic(1) every ::0::2 title sprintf('Method %.0f',i-1), \
newhistogram "System 2" lt 1, for [i=2:4] $MYDATA u i:xtic(1) every ::3::5 not, \
newhistogram "System 3" lt 1, for [i=2:4] $MYDATA u i:xtic(1) every ::6::8 not, \
newhistogram "System 4" lt 1, for [i=2:4] $MYDATA u i:xtic(1) every ::9::11 not, \
newhistogram "System 5" lt 1, for [i=2:4] $MYDATA u i:xtic(1) every ::12::14 not, \
newhistogram "System 6" lt 1, for [i=2:4] $MYDATA u i:xtic(1) every ::15::17 not, \
newhistogram "System 7" lt 1, for [i=2:4] $MYDATA u i:xtic(1) every ::18::20 not

See the attached figures for just the first line, then first and second one together, and lastly all 7 newhistograms.
Might it be that each newhistogram call creates an invisible entry in the key, which pushes the existing key entries around?

3 Attachments

Discussion

  • Eldrad Ulthran

    Eldrad Ulthran - 2021-11-21

    Forgot to login before creating…

     

    Last edit: Eldrad Ulthran 2021-11-21
  • Ethan Merritt

    Ethan Merritt - 2021-11-22

    Yes, each "newhistogram" inserts a blank line in the key. The intention is that when you give separate titles for each set of histogram bars there is a visual separation between the sets.

    I agree this is unwanted when all the subsequent titles are suppressed ("notitle") or otherwise blank. Unfortunately at the point the program sees a "newhistogram" in the command line it doesn' t know yet whether subsequent parts of the command will generate titles or not.

    Work-around
    Not really a general solution, but in this case you can probably get what you want using

         set key left vertical maxrows 1
    

    You could further tweak the placement by

        set key left at graph 0.2, graph 1.0 maxrows 1
    
     
    • Eldrad Ulthran

      Eldrad Ulthran - 2021-11-22

      That was what I was thinking, and I agree, in some cases it is absolutely reasonable to have a blank line. Unfortunately, in your workaround the key placement is quite difficult, if one wants to place it centered and not at a side.

      However, would it be possible to change the behaviour of "newhistogram" to not insert a blank line? If one wants a blank line a workaround would be to call keyentry title " " directly before or after the newhistogram call – in this way everyone can decide at which places he wants empty lines. Or maybe add a notitle option to newhistogram?

       
      • Ethan Merritt

        Ethan Merritt - 2021-11-22

        Both the "title" and "notitle" options to "newhistogram" itself affect the area under the x axis, not the key. So those keywords are not an option.

        Although I don't really like the idea much, it seems to me preferable to make it an option for the key, "set key noblanklines" or something like that. Or maybe no new keyword but always check as the key is generated that a blank line cannot be followed by another blank line?

         
        • Eldrad Ulthran

          Eldrad Ulthran - 2021-11-22

          Hmm, actually "newhistogram" has no option "title", calling plot newhistogram title "System 1" raises a "syntax error". So "newhistogram" is already expecting a string directly after it which is then placed under the x axis.

          Wouldn't such a check still create at least one unnecessary empty line that might again distort the key in the example I provided?
          An option "noblanklines" for the key doesn't look too good to me either… Imagine someone wants to use several "newhistograms" and have the blank line not at every new histogram, but only at every second one?

          I'd vote for a solution that gives the most direct possibilities for manipulation. So either a "noblankline" option for newhistogram, or disable this blank line completely and instead resort to manually placed empty keyentries (and add a hint in the manual).

           
          • Ethan Merritt

            Ethan Merritt - 2021-11-22

            You are right about title/notitle. I misread the help text for "newhistogram".

            I am thinking there must be some additional subtlety here, however, because I notice that despite what I said about the intent being to place a blank line between histogram sets, the demo "histograms2.dem" shows that the blank lines are all added at the end instead. So there seems to be a bug. Fixing that bug might or might not also solve the original issue.

            If it does require a new keyword, I'm leaning toward "newhistogram nokeyentry", with the explanation

            "By default each newhistogram command inserts a blank entry in the key to separate its component titles from previous entries in the key. You can suppress this blank line with the keyword nokeyentry."

             
  • Ethan Merritt

    Ethan Merritt - 2021-11-23

    Revised plan:

    The default stays the way it is.

    There is a new option set style histogram nokeyentry that suppresses addition of a blank key entry between histograms within a plot command. Note that this is a persistent property of the histogram style, it requires no changes to the actual plot command. This means if you always want this you can add the set style ... variant you prefer to your initialization file and not worry about it for later plots.

    As you say, if for some reason a blank line is still desired between a particular pair of histograms in a plot, adding a keyentry component to the plot command will do the trick.

     
    • Eldrad Ulthran

      Eldrad Ulthran - 2021-11-23

      This sounds reasonable to me, and I understand that changing the default always breaks backwards compatibility.

       
  • Ethan Merritt

    Ethan Merritt - 2021-12-02
    • status: open --> pending-fixed
     
  • Ethan Merritt

    Ethan Merritt - 2021-12-25
    • Status: pending-fixed --> closed-fixed
     

Log in to post a comment.

MongoDB Logo MongoDB