# Using gnuplot to do everything from data plotting to fitting and reuse

Hello, guys! It’s me! Warotan!

This time, I wrote about the important point when plotting and fitting the data with gnuplot.
Please read this if you are a student or a member of society.
Japanese version.

I have written articles about gnulot.
This time, I would like to introduce how to install it in connection with that.

## How to use gnuplot

gnuplot is a free and popular plotting tool.

### Start gnuplot

Enter the following command to start gnuplot.

gnuplot [Enter]

When gnuplot starts up, it waits for key-in.

plot sin(x) [Enter]

It plots the sin curve, right?

Here, let us use this data.

plot “Energy.txt” u 1:3 [Enter]

and get this.

By the way, you can get this image in the png format by

set terminal png enhanced [Enter]
set output “foo.png” [Enter]
rep [Enter]

Next, we will fit the data with a linear function,

f(x) = a*x [Enter]
fit f(x) “Energy.txt” u 1:3 via a [Enter]

You will see a lot of fitting results, but ignore them for the moment and type

rep f(x) [Enter]

Then, you will get a linearly fitted graph

The axis labels are set as

set xlabel “Number of particles” [Enter]
set ylabel “Energy” [Enter]
rep [Enter]

As a side note, $$a = 0.49…$$ shows the slope of the fitted function.

## Reuse the fitted results

If you quit the gnuplot, then the fitting results will vanish.
There is “fit.log”, but this cannot reproduce the fitting.
So, by executing the following command and save the fitting results,

save fit “Energy_linear.fit” [Enter]

The extension was set to .fit for now.
You can reuse the results by only loading this .fit file and don’t have to fit it again.

However, as a caveat, you have to define the function $$f\left(x\right)$$ with the parameter $$a$$,

f(x)=a*x [Enter]
plot “Energy.txt” u 1:3,f(x) [Enter]

You will get the same image above.
In fact, you can use a script file .plt with the whole sequence of commands, but I will leave it for the next time because the article becomes so long.

## Points to note when reusing

### The function to fit should be redefined as it reload only the parameters

I wrote this above, but only the names and values of the parameters will be load, so you should redefine the function.
In the above example, you can save the parameter $$a$$ and the value but not the function $$f$$.

### You can save the fitting results just before the save command

This is a sobering point to keep in mind.
For examle, we just had a fit with function $$f\left(x\right) = ax$$, and immediately after that we had another fit with function $$g\left(x\right) = bx^{2}$$, like this.

fit f(x) “Energy.txt” u 1:3 via a [Enter]
fit g(x) “Energy.txt” u 1:3 via b [Enter]
save fit “Energy_sq.fit” [Enter]

Then, you can save only the results of $$g\left(x\right)$$.
So, it is good to save the fitting results to separate files.

## Summary

Now, that’s all what I wanted to write.

This time, I wrote from the basic usage of gnuplot to a some practical ones.