0

With xticks automatically generated by gnuplot we find too often that the labels are too tight / cramped together as shown in this snapshot. How can we fix this issue?

xtick labels are too tight / cramped together

skink
  • 5,133
  • 6
  • 37
  • 58
Carl
  • 19
  • 3
  • 1
    Use less tic labels (`set xtics 2000`)? – user8153 Oct 19 '17 at 16:43
  • @n00b He can't because not enough reputation – Michael Oct 20 '17 at 00:22
  • I think that gnuplot should be able to compute the width of the tick labels based on the font used, and automatically adjust the xtics. I am writing a code where the user does not have access to customize the xtics, I believe that this should be done by gnuplot automatically. – Carl Oct 21 '17 at 19:45
  • Maybe this post will help: https://stackoverflow.com/questions/23152207/gnuplot-how-to-lower-the-number-of-tics-in-x-axis – Vinicius Placco Oct 23 '17 at 18:50
  • Gnuplot automatically calculates the number of tics solely based on the ranges, but doesn't take into account font, font size or canvas size for that. You must yourself increase the image width or decrease the xtic font size. I also guess, that your example does not make use of automatic tics, you have too many – Christoph Oct 23 '17 at 19:28
  • Christoph, I was not setting xtics. As you pointed out: "Gnuplot automatically calculates the number of tics solely based on the ranges, but doesn't take into account font, font size or canvas size for that"! Clearly if this is the case, then we will always have cases as the one I illustrated, keeping everything unchanged and reducing the width of the plot. Gnuplot should be able to compute the width of all the digits/characters used to render the xtics, and if the total width is more than say 60% of the x-axis width, then the xtics frequency needs to be reduced. My 2c. – Carl Nov 18 '17 at 14:29
  • You could file a feature request, https://sourceforge.net/p/gnuplot/feature-requests. – Christoph Nov 19 '17 at 10:22

2 Answers2

0

This is a very crude workaround. The idea is telling how many (approximate) number of tick labels one wants, and have gnuplot "translate" that into a suitable tick spacing according to the whole plotting range.

I am posting what I am using now, and it works reasonably well. It takes xmin=0. You could guess the way it works and tune it.

# Get/print stats about input data, ...
stats "output.csv"  using 2:5  nooutput
# ... and use them for setting the number of tick labels for x axes, to avoid overlap
#tmin = STATS_min_x
tmin = 0
tmax = STATS_max_x
nxtics = 5                        # Tune this
# Do not count 0 as a tick
nxtics = nxtics - 1
# Shift numbers to the range [1,10)
ttic1 = tmax / nxtics
nshift_digits = -floor(log10(ttic1))
shift = 10.0**nshift_digits
tmax_shift = tmax * shift
ttic1_shift = ttic1 * shift
# ttic1_shift should be between [1,10)
# Use (arbitrary) specified tick spacing (here at 1, 2, 5 in the first significant digit, but one could use others, including 2.5, e.g.), which better matches the data range and selected number of tick labels. Note that the number of tick labels would not be strictly maintained.
# Tune these numbers
ttic_shift = 1.0
if (ttic1_shift < 1.3) {
    ttic_shift = 1.0
} else { if (ttic1_shift < 3.0) {
    ttic_shift = 2.0
} else { if (ttic1_shift < 7.0) {
    ttic_shift = 5.0
} else {
    ttic_shift = 10.0
} } }
ttic = ttic_shift / shift
print "ttic=", ttic

PS: I could not have this working, although I did not try "hard". I guess that solution might work for a single plotted dataset, but not sure it would work for more than one.

  • This issue with too many xtics can only be fixed if we take into account: – Carl Nov 18 '17 at 14:30
  • The width of the plot in pixels, and the number of pixels needed to render all the xtics. If the latter is say 60% of the plot's width, the most likely we are fine, although there may be some extreme cases where this may not be enough, most likely this will fix most cases. – Carl Nov 18 '17 at 14:32
  • @Carl - What you mention points to a very general way of assigning xtics. The workaround posted is one step behind this, since it requires "telling how many (approximate) number of tick labels one wants". One would ultimately want to compute this automatically. Nevertheless, you could use a given number which will work well for a wide range of cases. For instance, I am using a `multiplot` array of 3x3 plots, and setting `nxtics = 4` worked well for me in all cases I found. Of course, if I change the array to 2x2, or I change the font size significantly, I would have to change `nxtics` by hand. – sancho.s ReinstateMonicaCellio Nov 21 '17 at 03:44
  • I agree that your approach is an improvement, but it is not a robust solution that takes into account the most important things: how much space is available, and the width needed to render all the ticks. – Carl Nov 22 '17 at 05:23
  • If the values associated with tick marks only have 1 or 2 digits, then the plot can accommodate many more xticks than if the numbers have say 7 digits. If we are writing software that is general purpose, not software that we can tweak to fit our personal needs, then we need a robust and general solution :). – Carl Nov 22 '17 at 05:24
  • @Carl - The variable `nshift_digits` would perfectly acommodate for your last comment, with a minor tweak. Hope you find it somewhat useful ayway. – sancho.s ReinstateMonicaCellio Nov 22 '17 at 16:35
-1

If they aren't too cramped, you can rotate them 90 degrees i.e.

set xtics rotate by 90