Change x- and yrange?

Next script:

set view map set dgrid3d 2,2 splot "-" with points title "Data" 0 0 1 0 1 2 1 0 3 1 1 4 e 

displays four dots at the four corners of the chart. Is there a way to expand the range so that there is a small border between the border and the points?

I know that this can be accomplished with the xrange or yrange commands. But I would prefer to say that the 10pt space is between the outer point and the border.

A solution like this:

  • Get current xmin and xmax values
  • Do some math with the return values โ€‹โ€‹and set the new xmin and xmax values โ€‹โ€‹accordingly.

It would be nice.

Any hints, links and help are appreciated!

Thanks in advance

Woltan

+1
source share
2 answers

I found the following opportunity to compensate for the boundary by a certain factor:

 set view map set dgrid3d 2,2 splot "-" with points title "Data" 0 0 1 0 1 2 1 0 3 1 1 4 e save set "Tmp.txt" system("python SetRange.py Tmp.txt 1.1") load "Tmp.txt" splot "-" with points title "Data" 0 0 1 0 1 2 1 0 3 1 1 4 e 

As you can see, the state of gnuplot is saved in the Tmp.txt file. In other cases, if the data is read from a file, the lower splot command can be replaced with replot.

Using a system call, you can modify the xrange and yrange entries in a temporary file. I did this with a python script since I'm not too good with awk ^^.

 import os import sys class Range(object): def __init__(self, line, factor): self.Line = line self.Factor = factor self.min, self.max = self._GetRange(self.Line) self.min_new, self.max_new = self._SetRange(self.min, self.max, self.Factor) def Write(self, file): Line_new = self.Line[0:self.Line.find("*")] Line_new = Line_new + str(self.min_new) + ":" + str(self.max_new) Line_new = Line_new + self.Line[self.Line.find("]") : self.Line.find("#")] file.write(Line_new + "\n") def _GetRange(self, line): min, max = (line[line.rfind("[") + 1 : line.rfind("]")]).split(":") return (float(min), float(max)) def _SetRange(self, min, max, factor): dist_new = (max - min) * factor mean = (max - min) / 2 + min min_new = mean - dist_new / 2 max_new = mean + dist_new / 2 return (min_new, max_new) if __name__ == '__main__': if not os.path.exists(sys.argv[1]): raise Exception("Cannot find file " + sys.argv[1]) file = open(sys.argv[1]) fileContents = file.read().split("\n") file.close() if len(sys.argv) > 2: factor = float(sys.argv[2]) else: factor = 1.05 for line in fileContents: if line.find("set xrange") != -1: xrange = Range(line, factor) elif line.find("set yrange") != -1: yrange = Range(line, factor) file = open(sys.argv[1], "w") xrange.Write(file) yrange.Write(file) file.close() 

If there is a shorter way to do this with awk, I would be more than happy to know;)

Cherio Voltan

+1
source

It depends on what you want to do.

AFAIK (I'm not an expert on gnuplot internals) gnuplot conveys most of how the plot is actually plotted on the terminal. This way, xrange and yrange will be able to give you a little more on both sides, but how much space actually (exactly) will depend on the size of the graph (so it will be different for png, small to png large - for example).

If you want to precisely control the size, then I think you need to work directly with a specific terminal, and not with gnuplot. However, Gnuplot supports a wide range of terminals. For this task, it is probably easiest to work with the metapost mp terminal. This allows you to reposition the border and ticks accurately.

To get this working you need a script

 set term mp latex set output "xrange_example.mp" set view map set dgrid3d 2,2 splot "-" with points title "Data" 0 0 1 0 1 2 1 0 3 1 1 4 e set output set term pop 

And your latex document (suppose you use this)

 \documentclass{scrartcl} \usepackage{emp,ifpdf} \ifpdf \DeclareGraphicsRule{*}{mps}{*}{} \fi \begin{document} \includegraphics{xrange_example.0} \end{document} 

Then you create your image using

 > TEX=latex > gnuplot your_gnuplot.gp > mpost xrange_example.mp > pdflatex xrange.tex 

This is the xrange_example.mp file that you are modifying. If you open it, you will find (about halfway down)

 beginfig(0); w:=5.000in;h:=3.000in; a:=w/1200.0;b:=h/720.0; 

a and b define scaling in width and height. After that add

 numeric x[], y[]; x[0] = -10pt; x[1] = 10pt; y[0] = -10pt; y[1] = 10pt; 

They enter the distances that you are going to add to the border and ticks. Ticks are defined like this

 draw (193.0a,591.2b)--(193.0a,569.6b); % On the left draw (355.8a,165.4b)--(355.8a,187.0b); % on the right put_text( btex 0.2 etex, 355.8a, 117.8b, 0, 2); % the text 

You want to change them like this:

 draw (193.0a+x[0],591.2b)--(193.0a+x[0],569.6b); % On the left draw (355.8a+x[1],165.4b)--(355.8a+x[1],187.0b); % on the right put_text( btex 0.2 etex, 355.8a+x[0], 117.8b, 0, 2); % the text 

As you can see, this is easy to do with search and replace (or in a script), because you only change the numbers 193.0a, 355.8a.

You need to do the same for xtics and borders

 draw (192.9a,591.2b)--(192.9a,165.3b)--(1007.0a,165.3b)--(1007.0a,591.2b)--(192.9a,591.2b); 

In general, I think you need to change 8 numbers (many times - very scripted).

As an example, I include a PDF plot sin(x) with a modified border. The border has been changed to exactly 10pt . (you can also choose this in mm or cm or inches, etc.)

image

+1
source

All Articles