Gnuplot 6 and Python: Seamless Integration for Data Visualization
The venerable gnuplot, a powerful and versatile plotting utility, has undergone significant enhancements in its latest major release, version 6.0. This update introduces a wealth of new features and improvements that enhance its usability.
As shown here, gnuplot 6.0 solidifies its position as an essential tool for scientists, engineers, and data analysts alike.
The recent updates to gnuplot have improved its handling of week-date time formats, addressing previous deficiencies in its support for these conventions. Specifically, the time specifier formas %W has been aligned with established standards. The %W format now adheres to the ISO 8601 week date standard, which defines how weeks are numbered in a year.
Gnuplot 6.0 has introduced new functions to facilitate the conversion and retrieval of week dates. The weekdate_iso(year, week, day) function converts an ISO standard week date to the unix time.
Gnuplot 6.0 can be easily installed on Windows through the MSYS2/MinGW64 subsystem, which provides a convenient package management system, by using the command:
pacman -S gnuplot
At the time of writing, the version available for installation is gnuplot 6.0 patchlevel 0. To verify the installed version, users can simply run the command:
gnuplot.exe -V
in the terminal. This command will display the current version of gnuplot, confirming that the installation was successful.
Suppose you want to find out the calendar date for the ISO week date corresponding to the 4th week of the year 2025, specifically the 4rd day of that week (which is a Thursday). You would use the weekdate_iso function as follows:
# Define the year, week, and day
year = 2025
week = 4
day = 4
# Convert ISO week date to unix time
calendar_time = weekdate_iso(year, week, day)
# Format the Unix time into a human-readable date
formatted_date = strftime("%Y-%m-%d", calendar_time)
# Print the result
print "The calendar date for ISO week 4, day 4 of 2025 is: ", formatted_date
|
Weekly Planner Notebook |
To combine Python and Gnuplot, I use the script shown below. The core of the script revolves around a set of Gnuplot commands defined in a preamble that prepares the plotting environment. This preamble includes commands to set the title, label the axes, enable the grid, and define the output format as a PNG file. Crucially, it also includes the command plot "-", which tells Gnuplot to expect data from standard input (stdin).
import seaborn as sns
from subprocess import Popen, PIPE
# preamble
gnuplot_command = [
'gnuplot',
'-e',
'''
set title "Plot of Dataset I";
set xlabel "X-axix";
set ylabel "Y-axis";
set grid; set pointsize 2;
set terminal png;
set output "output.png";
set key;
f(x) = a*x + b;
fit f(x) "-" using 1:2 via a, b;
plot "-" using 1:2 title "Dataset I" with points , f(x) title "Fitted Line" with lines;
''']
# Load the Anscombe dataset
df = sns.load_dataset('anscombe')
# Filter the DataFrame for dataset 'I' and select the x and y columns
x = df[df['dataset'] == 'I']['x']
y = df[df['dataset'] == 'I']['y']
# Create a subprocess to run Gnuplot
with Popen(gnuplot_command, stdin = PIPE, stderr = PIPE, stdout = PIPE) as proc:
# fit points
for i in range(0,len(x)):
proc.stdin.write(f"{x[i]} {y[i]}\n".encode())
proc.stdin.write(b"EOF\n") # Use EOF to signal the end of data
# plot
for i in range(0,len(x)):
proc.stdin.write(f"{x[i]} {y[i]}\n".encode())
proc.stdin.write(b"EOF\n") # Use EOF to signal the end of data
proc.stdin.close() # Close the stdin to signal that we're done
stdout_output, stderr_output = proc.communicate()
proc.wait() # Wait for Gnuplot to finish
# Print the stdout output
if stdout_output:
print("Gnuplot output:")
print(stdout_output.decode('ASCII'))
# Print the stderr output
if stderr_output:
print("Gnuplot output:")
print(stderr_output.decode('ASCII'))
After defining this preamble, the script loads the Anscombe dataset using Seaborn and filters it to extract the x and y values for dataset 'I'. Via a subprocess, it then writes the x and y data points to Gnuplot's stdin. This enables Gnuplot to plot the data points and a linear fit line based on the provided input, as shown in the figure below.