Search code examples
c++graphc++17gnuplotgnuplot-iostream

Inconsistent Dot Output on Gnuplot: Extra Dot Appearing


there's 6 dot instead of 5

I currently use gnuplot to make graph. I tried to put 5 dot in it which I stored the coordinate on txt file

txt file :
5 2
2 4
3 6
1 8
5 10

but the output on png file there's 6 dot?? where's that 1 addition dot came from??

code:
#include <iostream>
#include <fstream>
#include <cstdlib>

bool generateImg(){
    FILE* gnuplotPipe = _popen("gnuplot -persist", "w");
    if (!gnuplotPipe) {
        std::cerr << "Error opening Gnuplot pipe." << std::endl;
        return 1;
    }

    fprintf(gnuplotPipe, "set terminal png\n");
    fprintf(gnuplotPipe, "set output 'graph.png'\n");
    fprintf(gnuplotPipe, "plot 'coordinate.txt' using 1:2 with linespoint pt 7 ps 2\n");
    
    _pclose(gnuplotPipe);

    return 0;
}

bool generateVisualizeTerminal(){
    FILE* gnuplotPipe = popen("gnuplot", "w");
    if (!gnuplotPipe) {
        std::cerr << "Error opening Gnuplot pipe." << std::endl;
        return 1;
    }

    fprintf(gnuplotPipe, "set terminal dumb size 0,0\n");
    fprintf(gnuplotPipe, "plot 'coordinate.txt' with lines\n");

    pclose(gnuplotPipe);

    return 0;
}

int main() {
    std::ofstream dataFile("coordinate.txt");
    if (!dataFile) {
        std::cerr << "Error opening data file." << std::endl;
        return 1;
    }

    dataFile << "5 2\n";
    dataFile << "2 4\n";
    dataFile << "3 6\n";
    dataFile << "1 8\n";
    dataFile << "5 10";
    dataFile.close();

    generateVisualizeTerminal();
    generateImg();

    return 0;
}

what did I expecting:

  1. There's 5 dot only

what did I try:

  1. I try to give it a command on writing coordinate in txt so I can see perhaps it's my code fault but here its turns out. I wonder if it's the icon perhaps?

here's the picture: there's 2 dot when I write 1 coordinate


Solution

  • I just find out that the dot on the right top corner are label and not only that, "coordinate.txt using 1:2 (dot)" on the top right corner are label so to turn it off I have to add :

    fprintf(gnuplotPipe, "set key off\n");

    updated code:

    fprintf(gnuplotPipe, "set terminal png\n");
    fprintf(gnuplotPipe, "set output 'graph.png'\n");
    fprintf(gnuplotPipe, "set key off\n");
    fprintf(gnuplotPipe, "plot 'coordinate.txt' pt 7 ps 2\n");