Search code examples
gnuplot

Saving to BMP file on Windows 10


I'm trying to save a plot to a bmp file on Windows 10. I can do it with the interactive terminals: windows or qt using the GUI. But I can't figure out how to do it with commands. Doing this just results in an empty file.

gnuplot> set terminal windows

Terminal type is now 'windows'
Options are '0 color solid butt enhanced standalone'
gnuplot> set output "file.bmp"  
gnuplot> plot sin(x)
Closing file.bmp

Solution

  • If I start the gnuplot console and set term windows and type plot sin(x) gnuplot is crashing. This is certainly a bug or something wrong with my system (Win10, gnuplot 5.4.1). I can only plot without crash if I set the terminal option to docked layout 1,1. But then, I get an empty BMP file like you got.

    Actually, so far I haven't found any gnuplot version starting from 4.4.0 which seems to be able to create a correct BMP output.

    So, then my suggestion would be to create a PNG output and convert this to BMP, e.g. via ImageMagick.

    • download and unzip the .zip version of ImageMagick

    • copy only the file magick.exe to your gnuplot working directory (or somewhere else where your system can find it)

    • save the script below into the file SO71415908.gp

    • either in Windows command line type gnuplot -c SO71415908.gp, or alternatively, in gnuplot console type load "SO71415908.gp". However, in the latter case I haven't found out how to close the Windows console windows without closing gnuplot.

    Script:

    ### create a BMP file with gnuplot via help of ImageMagick
    reset session
    set terminal pngcairo
    
    FILE = "SO71415908"       # filename without extension
    
    set output sprintf("%s.png",FILE)
    
    plot sin(x)
    
    set output      # close the file, necessary or not?
    
    system sprintf('magick %s.png %s.bmp',FILE,FILE)
    ### end of code
    

    Result:

    Two files: SO71415908.png and SO71415908.bmp