@[TOC]win10 c++ 绘图快捷方式
For a quick and efficient way to plot graphs in C++ on Windows 10, you can use the gnuplot tool. Gnuplot is a portable command-line driven graphing utility that is easy to set up and use. Here’s how you can quickly get started:
Step 1: Install Gnuplot
-
Download Gnuplot:
Download the Windows installer for Gnuplot from the official website. -
Install Gnuplot:
Run the installer and follow the instructions to complete the installation. Ensure you add Gnuplot to your system PATH during installation.
Step 2: Create and Compile C++ Code
-
Create a C++ File:
Create a file namedplot.cpp
with the following content:#include <iostream> #include <vector> #include <cmath> #include <fstream> void plotData(const std::vector<double>& x, const std::vector<double>& y) { std::ofstream file("data.dat"); for (size_t i = 0; i < x.size(); ++i) { file << x[i] << " " << y[i] << "\n"; } file.close(); std::ofstream gp("plot.gnuplot"); gp << "set terminal wxt\n"; gp << "plot 'data.dat' with lines\n"; gp << "pause -1\n"; gp.close(); system("gnuplot plot.gnuplot"); } int main() { std::vector<double> x, y; for (double i = 0; i < 10; i += 0.1) { x.push_back(i); y.push_back(std::sin(i)); } plotData(x, y); return 0; }
-
Compile the C++ File:
You can use MinGW or any other C++ compiler to compile the code. If you have MinGW installed, you can compile the code with the following command:g++ -o plot plot.cpp
Step 3: Run the Program
Run the compiled executable to display the sine wave plot. If everything is set up correctly, you should see a Gnuplot window displaying the sine wave.
./plot
This method is straightforward and leverages the powerful plotting capabilities of Gnuplot without the need for complex library setups in C++.