I have to plot a graph using gnuplot with the help of shell script.
I have written this code but its not working. I want to plot the graph in a html file so that I can see that graph in browser.
echo "set term canvas mousing size 500, 500
set output "file_name.html"
plot 'my_file.txt' with lines" | gnuplot
I have saved this file in .bash on the desktop.
Now I wrote this in terminal but it didn't work
sh file_name.bash
Please someone help me out with this. I am totally stuck with this thing since yesterday. :-(
There are multiple errors,
you use double-quotes within the echo which will not work. change it to single-quotes
use semi-colon instead of a line break for multiple gnuplot commands
So resulting script should be something like this
echo "set term canvas mousing size 500, 500; set output 'file_name.html'; plot 'my_file.txt' with lines" | gnuplot
or
cat << __EOF | gnuplot
set term canvas mousing size 500, 500
set output "file_name.html"
plot 'my_file.txt' with lines
__EOF
Related
For example, if I want the output of all commands to be red, how do I do it? Scenario- you type "ls" and then all the file it lists should be red in color. And this should continue for the next commands as well as long as I don't change the color to some other color.
The -P option of the print command allows you to let zsh do prompt expansion and with this insert colour changing codes into the output. For instance,
print -P %F{red]'; cat myfile
prefixes the ansi escape code for red to the output produced by cat. The effect will be that myfile is printed in red (or whatever you have told your terminal what the colour red is supposed to be; most terminals can be configured to map ansi colour codes to arbitrary RGB colours).
For setting the background colour, use %K instead of %F.
I found this very helpful post here on plotting a heat-map on a non-uniform grid: Heatmap with Gnuplot on a non-uniform grid
Now everything works great if I put the suggested commands manually in gnuplot. However, I want to automate the graph by writing a shell-script that I can call directly in my fortran code.
This is the script (Map_Ent.sh):
#!/bin/bash
gnuplot <<- EOF
set pm3d map corners2color c1
set autoscale fix
set ytics 1
splot 'Career_Choice.txt' using 1:($2-0.5):3
EOF
eog Ent_map.png &
And this is how I call it in Fortran:
call SYSTEM('bash ./Map_Ent.sh')
However my graph is completely white when I do it this way. Any ideas on what I am doing wrong? I would be very thankful for any suggestions!
The $2 is interpreted by bash and evaluates to nothing. Therefore, gnuplot plots only -0.5. $2 is a shortcut for column(2). Just use the latter and it should work fine:
#!/bin/bash
gnuplot <<- EOF
set pm3d map corners2color c1
set autoscale fix
set ytics 1
splot 'Career_Choice.txt' using 1:(column(2)-0.5):3
EOF
There is no need to use bash as intermediate step, just use a gnuplot script:
file.gp:
set pm3d map corners2color c1
set autoscale fix
set ytics 1
splot 'Career_Choice.txt' using 1:(column(2)-0.5):3
And then
call SYSTEM('gnuplot -persist file.gp')
(I'm not sure about the fortran call, just copied from the question).
Currently, I do something like the following:
set term png
set output 'file.png'
But hardcoding filename in the script is quite inflexible. Is there some way to tell gnuplot to output image file to stdout, so I will be able to redirect it's output where needed?
If you do want to send your .png to stdout, just don't set the output:
#!/usr/bin/env gnuplot
set term png
plot x
Then run the script
./plot.plt > mypng.png
I think the bash wrapper makes more sense for most purposes, but this is potentially useful as well.
Put your gnuplot script inside a script and use a here document:
#!/bin/bash
gnuplot << EOF
set term png
set output "$1.png"
plot "$1.dat"
EOF
Now invoke the script, say plot.sh, by specifying the prefix to output file and data as argument: bash plot.sh file.
I'm trying to show an animation using gnuplot
I got the following script:
plot ”heat1d.txt ” using 1:2 every :::a::a
pause 0.01
a=a+1
if ( a<b ) reread
that I execute using
a = 0
b = 100
load "a.plot"
it works, but is there a way to execute all of this using only 1 command from a shell?
Alternatively is there a way to integrate the variable definitions into the .plot file so that I can simply execute it? I tried different things like echo 'a=0'|gnuplot etc but it doesn't seem to actually define the variable correctly
thanks
You can use a do for loop.
do for [a = 0:100] {
plot ”heat1d.txt ” using 1:2 every :::a::a
pause 0.01
}
The default terminal on linux is usually wxt, and it has the raise option, which will change the focus to the plot window at every iteration. This will make it difficult if not impossible to stop the animation.
I suggest to put noraise as the terminal option. For example, you can put the following line at the beginning of the script:
set term wxt noraise
Now, if you want to stop the animation halfway, press CtrlC on the gnuplot terminal.
You can pass -e as a commandline argument. For example, if you have the script:
#script test.gp
print foo,bar
Then you could run it with gnuplot using:
gnuplot -e "foo=1;bar=2" test.gp
In your case, it looks like you could accomplish nearly what you want by invoking your script as:
gnuplot -e "a=0;b=100" a.plot
I'm using bash to feed gnuplot from a script. How you pop up only one window? Let say you run
#!/bin/bash
for((i=1;i<6;i++)) do
echo "plot sin(x)"
done | gnuplot -persist
you will get 5 windows of the same plot. Is there an option to get only one?
There was a mistake above. That wasn't exactly the kind of run-time i'm doing. Is more like runing the next script, say, 5 times
#!/bin/bash
echo "plot sin(x)"
I just realized that what I want to do is kill the latest gnuplot process before make the new one.
Sorry for that. I'm sooo tired today :D
the gnuplot x11 terminal uses a separate program called gnuplot_x11 for displaying the results. From the help
The `xlib` terminal driver supports the X11 Windows System. It generates
gnuplot_x11 commands, but sends them to the output file specified by
`set output '<filename>'`. `set term x11` is equivalent to
`set output "|gnuplot_x11 -noevents"; set term xlib.`
`xlib` takes the same set of options as `x11`.
So, if you want to kill plots that are remaining after a gnuplot -persist, it should suffice to killall gnuplot_x11.
You might be interested in the iteration capabilities available in newer versions of Gnuplot (see the section Iteration in the user's guide)
What about the following:
echo `for((i=1;i<6;i++))
do
echo 'plot sin(x)'
done` | gnuplot -persist
In one script one can
echo "set grid" > gpfile
tail -f gpfile | gnuplot '-'
then in another process one can write any gnuplot commands into the gpfile
echo "plot \"whatever\" " >> gpfile
sleep 3
echo "replot" >> gpfile
...etc
Its useful for realtime process control display, where some status file
is being updated at intervals.