gnuplot

How to avoid xticlabel overlaps?


I am having trouble with getting the X-Axis number ranging on the GNU plot to not scrunch together. I will attach a PNG showing the issue, and here is my code that generates the GNU Plot:

void plotMultiFFT(const vector<Complex>& fftResults) {
    // Create a temporary data file
    std::ofstream dataFile("fft_data.gnu");
    if (!dataFile) {
        std::cerr << "Error creating data file\n";
        return;
    }

    // Write the FFT results to the data file
    for (size_t i = 0; i < fftResults.size(); ++i) {
        dataFile << i << " " << std::real(fftResults[i]) << " " << std::imag(fftResults[i]) << "\n";
    }
    dataFile.close();

    // Create a temporary script file for GNUplot
    std::ofstream scriptFile("plot_script.gnu");
    if (!scriptFile) {
        std::cerr << "Error creating script file\n";
        return;
    }

    scriptFile << "set term png size 1024,768\n";
    scriptFile << "set output 'plot.png'\n";
    scriptFile << "set multiplot layout 2,2 title 'FFT Spectrum'\n";

    // First quadrant: Real part vs index
    scriptFile << "set title 'Real part'\n";
    scriptFile << "plot 'fft_data.gnu' using 1:2 with lines title 'Real'\n";

    // Second quadrant: Imaginary part vs index
    scriptFile << "set title 'Imaginary part'\n";
    scriptFile << "plot 'fft_data.gnu' using 1:3 with lines title 'Imaginary'\n";

    // Third quadrant: Magnitude vs index
    scriptFile << "set title 'Magnitude'\n";
    scriptFile << "plot 'fft_data.gnu' using 1:(sqrt($2**2 + $3**2)) with lines title 'Magnitude'\n";

    // Fourth quadrant: Phase vs index
    scriptFile << "set title 'Phase'\n";
    scriptFile << "plot 'fft_data.gnu' using 1:(atan2($3, $2)) with lines title 'Phase'\n";

    scriptFile << "unset multiplot\n";
    scriptFile.close();

    // Execute the GNUplot script
    system("gnuplot -persist plot_script.gnu");
}

Here is the PNG:

enter image description here


Solution

  • Using auto xticlabels set xtics auto(which is default), can lead to overlapping of xtic labels (especially in multiplots). This depends on graph size, font size, label format and the number itself. You probably have at least 3 ways to avoid overlapping of xticlabels. Check help xtics.

    1. rotate the labels
    2. set a different format (check help format and help format_specifiers)
    3. set the xtic distance manually (and maybe add some minor xtics without labels)

    Script:

    ### avoid overlap of xtic labels
    reset session
    
    set term wxt size 640,384
    set key noautotitle
    set xrange[0:1e6]
    
    set multiplot layout 2,2
    
        plot '+' u 1:1 w l
    
        set xtic rotate by 90 right
        plot '+' u 1:1 w l
    
        set xtic rotate by 0 center
        set format x "%0.t·10^{%T}"
        plot '+' u 1:1 w l
        
        set xtics 500000
        set format x "%g"
        set mxtics 5
        plot '+' u 1:1 w l
    
    unset multiplot
    ### end of script
    

    Result: (from wxt terminal, size 640,384 pixels)

    enter image description here