To create a GNUplot script within a shell script that graphs the contents of your three files with different colors, you can use the following template. This shell script will generate a plot where each dataset is represented by a different color.
Create a new file, for example plot_graph.sh, and make it executable with chmod +x plot_graph.sh. Then, paste the following content into the file:
#!/bin/bash
# Define the output image file
OUTPUT_IMAGE="output.png"
# Create the GNUplot script
gnuplot -persist <<-EOFMarker
# Set terminal to png image
set terminal png size 800,600
# Set the output file
set output '${OUTPUT_IMAGE}'
# Set titles and labels
set title "Threads vs Time Taken"
set xlabel "Number of Threads"
set ylabel "Time taken in seconds"
# Set grid
set grid
# Set the keys
set key outside
# Plot the data
plot "Merge.dat" using 1:2 with linespoints title 'Merge' lt rgb "red", \
"Count.dat" using 1:2 with linespoints title 'Count' lt rgb "blue", \
"Quick.dat" using 1:2 with linespoints title 'Quick' lt rgb "green"
EOFMarker
echo "Plot has been generated at ${OUTPUT_IMAGE}"
Here's an explanation of the script:
-
We use a here-document EOFMarker to pass our GNUplot commands directly into the gnuplot -persist command.
-
The set terminal command specifies that the output will be a PNG image, with the dimensions 800x600 pixels.
-
The set output command defines the name of the output image that GNUplot will create.
-
The set title, set xlabel, and set ylabel commands are used to set the title of the plot and the labels for the x and y axes.
-
The set grid command puts a grid on the plot for better readability.
-
The set key outside command places the legend (key) outside the plot area.
-
In the plot command, the using 1:2 option specifies that the x-axis data comes from column 1 and the y-axis data comes from column 2 of your files. with linespoints means that the plot will show both lines connecting the points and the points themselves. title labels each plot, and lt rgb assigns a color to each dataset. red, blue, and green are the colors used for each of the .dat files.
-
The echo command just prints out a message indicating where the plot has been saved.
To run the script, simply execute this command in the terminal:
./plot_graph.sh
Please ensure that Merge.dat, Count.dat, and Quick.dat files are available in the directory from where you run the script, and that the data is formatted as expected. If you need to customize the gnuplot commands (for instance, to change colors, add line types, or modify any other plot properties), you can do so within the here-document block in the script.