diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt
index e20a0ef..e7551da 100644
--- a/example/CMakeLists.txt
+++ b/example/CMakeLists.txt
@@ -13,6 +13,8 @@ macro(add_example name switch)
endif()
endmacro()
+add_example(gnuplot_ex ON)
+
if(${GCTL_GRAPHIC_GMT})
add_example(JX_single_ex ON)
endif()
diff --git a/example/gnuplot_ex.cpp b/example/gnuplot_ex.cpp
new file mode 100644
index 0000000..d689705
--- /dev/null
+++ b/example/gnuplot_ex.cpp
@@ -0,0 +1,70 @@
+/********************************************************
+ * ██████╗ ██████╗████████╗██╗
+ * ██╔════╝ ██╔════╝╚══██╔══╝██║
+ * ██║ ███╗██║ ██║ ██║
+ * ██║ ██║██║ ██║ ██║
+ * ╚██████╔╝╚██████╗ ██║ ███████╗
+ * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
+ * Geophysical Computational Tools & Library (GCTL)
+ *
+ * Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
+ *
+ * GCTL is distributed under a dual licensing scheme. You can redistribute
+ * it and/or modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, either version 2
+ * of the License, or (at your option) any later version. You should have
+ * received a copy of the GNU Lesser General Public License along with this
+ * program. If not, see .
+ *
+ * If the terms and conditions of the LGPL v.2. would prevent you from using
+ * the GCTL, please consider the option to obtain a commercial license for a
+ * fee. These licenses are offered by the GCTL's original author. As a rule,
+ * licenses are provided "as-is", unlimited in time for a one time fee. Please
+ * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
+ * to include some description of your company and the realm of its activities.
+ * Also add information on how to contact you by electronic and paper mail.
+ ******************************************************/
+
+#include "../lib/graphic.h"
+
+int main(int argc, char *argv[])
+{
+ gctl::gnuplot gt;
+ //one line test
+ //gt.send("plot [-pi/2:pi] cos(x),-(sin(x) > sin(x+1) ? sin(x) : sin(x+1))");
+
+ //buffer test (bessel animation)
+ gt.to_buffer();
+ gt.send("set terminal gif animate delay 10 size 600,400");
+ gt.send("set output 'bessel.gif'");
+ gt.send("set palette rgb 3,9,9");
+ gt.send("unset key; unset colorbox; unset border; unset tics");
+ gt.send("set lmargin at screen 0.03");
+ gt.send("set bmargin at screen 0");
+ gt.send("set rmargin at screen 0.97");
+ gt.send("set tmargin at screen 1");
+ gt.send("set parametric", true);
+ gt.send("bessel(x,t) = besj0(x) * cos(2*pi*t)");
+ gt.send("n = 6 # number of zeros");
+ gt.send("k = (n*pi-1.0/4*pi)");
+ gt.send("u_0 = k + 1/(8*k) - 31/(384*k)**3 + 3779/(15360*k)**5");
+ gt.send("set urange [0:u_0]");
+ gt.send("set vrange[0:1.5*pi]");
+ gt.send("set cbrange [-1:1]");
+ gt.send("set zrange[-1:1]");
+ gt.send("set isosamples 200,100");
+ gt.send("set pm3d depthorder");
+ gt.send("set view 40,200");
+
+ std::string cmd;
+ for (float t = 0.0f; t < 2.0f; t += 0.02f)
+ {
+ cmd = "splot u*sin(v),u*cos(v),bessel(u," + std::to_string(t) + ") w pm3d ls 1";
+ gt.send(cmd);
+ }
+ gt.send("set output");
+
+ gt.save_buffer("bessel");
+ gt.send_buffer();
+ return 0;
+}
diff --git a/lib/graphic.h b/lib/graphic.h
index eb26671..958a818 100644
--- a/lib/graphic.h
+++ b/lib/graphic.h
@@ -31,5 +31,6 @@
#include "graphic/gmt_JX_single.h"
#include "graphic/mathgl_plot.h"
#include "graphic/mathgl_dens.h"
+#include "graphic/gnuplot.h"
#endif // _GCTL_GRAPHIC_H
\ No newline at end of file
diff --git a/lib/graphic/gnuplot.cpp b/lib/graphic/gnuplot.cpp
new file mode 100644
index 0000000..c50106d
--- /dev/null
+++ b/lib/graphic/gnuplot.cpp
@@ -0,0 +1,83 @@
+/********************************************************
+ * ██████╗ ██████╗████████╗██╗
+ * ██╔════╝ ██╔════╝╚══██╔══╝██║
+ * ██║ ███╗██║ ██║ ██║
+ * ██║ ██║██║ ██║ ██║
+ * ╚██████╔╝╚██████╗ ██║ ███████╗
+ * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
+ * Geophysical Computational Tools & Library (GCTL)
+ *
+ * Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
+ *
+ * GCTL is distributed under a dual licensing scheme. You can redistribute
+ * it and/or modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, either version 2
+ * of the License, or (at your option) any later version. You should have
+ * received a copy of the GNU Lesser General Public License along with this
+ * program. If not, see .
+ *
+ * If the terms and conditions of the LGPL v.2. would prevent you from using
+ * the GCTL, please consider the option to obtain a commercial license for a
+ * fee. These licenses are offered by the GCTL's original author. As a rule,
+ * licenses are provided "as-is", unlimited in time for a one time fee. Please
+ * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
+ * to include some description of your company and the realm of its activities.
+ * Also add information on how to contact you by electronic and paper mail.
+ ******************************************************/
+
+#include "gnuplot.h"
+
+gctl::gnuplot::gnuplot(bool persist)
+{
+ _2buffer = false;
+ pipe = popen(persist ? "gnuplot -persist" : "gnuplot", "w");
+ if (!pipe) throw std::runtime_error("gnuplot pipe failed to open. Try install gnuplot first.");
+}
+
+gctl::gnuplot::~gnuplot()
+{
+ if (pipe) pclose(pipe);
+}
+
+void gctl::gnuplot::to_buffer()
+{
+ _2buffer = true;
+ return;
+}
+
+void gctl::gnuplot::send(const std::string& cmd, bool next_block)
+{
+ if (!pipe) return;
+
+ if (!_2buffer) fputs((cmd + "\n").c_str(), pipe);
+ else
+ {
+ if (next_block) buffer.push_back(cmd + "\n\n");
+ else buffer.push_back(cmd + "\n");
+ }
+ return;
+}
+
+void gctl::gnuplot::send_buffer(size_t repeat)
+{
+ if (!pipe) return;
+ for (size_t i = 0; i < repeat; i++)
+ {
+ for (auto& line : buffer) fputs(line.c_str(), pipe);
+ fputs("\n", pipe);
+ }
+
+ fflush(pipe);
+ buffer.clear();
+ return;
+}
+
+void gctl::gnuplot::save_buffer(const std::string& filename)
+{
+ std::ofstream outfile;
+ open_outfile(outfile, filename, ".gp");
+
+ for (auto& line : buffer) outfile << line;
+ outfile.close();
+ return;
+}
\ No newline at end of file
diff --git a/lib/graphic/gnuplot.h b/lib/graphic/gnuplot.h
new file mode 100644
index 0000000..4cde491
--- /dev/null
+++ b/lib/graphic/gnuplot.h
@@ -0,0 +1,85 @@
+/********************************************************
+ * ██████╗ ██████╗████████╗██╗
+ * ██╔════╝ ██╔════╝╚══██╔══╝██║
+ * ██║ ███╗██║ ██║ ██║
+ * ██║ ██║██║ ██║ ██║
+ * ╚██████╔╝╚██████╗ ██║ ███████╗
+ * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
+ * Geophysical Computational Tools & Library (GCTL)
+ *
+ * Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
+ *
+ * GCTL is distributed under a dual licensing scheme. You can redistribute
+ * it and/or modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, either version 2
+ * of the License, or (at your option) any later version. You should have
+ * received a copy of the GNU Lesser General Public License along with this
+ * program. If not, see .
+ *
+ * If the terms and conditions of the LGPL v.2. would prevent you from using
+ * the GCTL, please consider the option to obtain a commercial license for a
+ * fee. These licenses are offered by the GCTL's original author. As a rule,
+ * licenses are provided "as-is", unlimited in time for a one time fee. Please
+ * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
+ * to include some description of your company and the realm of its activities.
+ * Also add information on how to contact you by electronic and paper mail.
+ ******************************************************/
+
+#ifndef _GCTL_GNUPLOT_H
+#define _GCTL_GNUPLOT_H
+
+#include "gctl/core.h"
+#include "gctl/utility.h"
+
+namespace gctl
+{
+ class gnuplot
+ {
+ public:
+ /**
+ * @brief Construct a new gnuplot object
+ *
+ * @param persist Gnuplot will hold the window after plot if true.
+ */
+ gnuplot(bool persist = true);
+ // destructor
+ virtual ~gnuplot();
+
+ /**
+ * @brief Set this to send all lines to buffer.
+ */
+ void to_buffer();
+
+ /**
+ * @brief Send a line to gnuplot and execute it. If send2buffer is called before, then the line will be stored in the buffer.
+ *
+ * @param cmd command line.
+ * @param next_block If true, an empty line will be added to the buffer after the cmd.
+ */
+ void send(const std::string& cmd, bool next_block = false);
+
+ /**
+ * @brief Send the buffer to gnuplot and execute it at once. Then clear the buffer.
+ *
+ * @param repeat How many times to repeat the buffer.
+ */
+ void send_buffer(size_t repeat = 1);
+
+ /**
+ * @brief Save the buffer to a file. This will not clear the buffer.
+ *
+ * @param filename File name.
+ */
+ void save_buffer(const std::string& filename);
+
+ private:
+ gnuplot(gnuplot const&) = delete;
+ void operator=(gnuplot const&) = delete;
+
+ FILE* pipe;
+ std::vector buffer;
+ bool _2buffer;
+ };
+}
+
+#endif // _GCTL_GNUPLOT_H
\ No newline at end of file