소개

Gnuplot은 트랜드 및 그래프를 생성하는데 사용되는 도구이다. Gnuplot은 일반적으로 시계열 기반 데이터 수집에 사용되지만 여기에 한정되지 않으며 정적 데이터도 사용할 수 있다. Gnuplot은 일괄처리로 실행되거나 작동 중에 실행될 수 있으며 그래픽 표시기 또는 웹 브라우저로 표시된다.

설정

gnuplot은 그래프를 표현하는데 있어 다양한 설정들을 제공한다.

설정

설정 값 설명 사용 예시
xtics x 축 설정 xtics 2 font “Times-Roman, 10”
ytics y 축 간격 설정 ytics 2
xrange x 축 범위 설정 xrange [0:10]
yrange y 축 범위 설정 yrange [0:10]

그래프 선 설정

설정 값 설명
lw 선 두께
lt 선 색상
pt 점 종류
ps 점 크기

ex)
plot “temp.dat” u 1:2 with lp lw 5 lt 2 pt 1 ps 1 title “test”

테스트

gnuplot은 명령라인 프로그램으로 리눅스 환경에서는 gnuplot 프로그램을 실행시키면 gnuplot 프롬프트가 출력된다. 그 상태에서 그래프를 생성하기 위한 작업들을 수행하는데 본 예제에서는 gnuplot 프로그램을 c 프로그램에서 파이프로 호출하여 실행하도록 하겠다.

테스트용 데이터값들

2014-04-11 13:06:15 46.00000 38.00000 207.00000 14.00000 30.15625 22.00000
2014-04-11 13:07:45 15.00000 17.00000 200.00000 1.00000 30.18750 21.93750
2014-04-11 13:08:46 46.00000 2.00000 196.00000 19.00000 30.15625 21.93750
2014-04-11 13:09:38 24.00000 9.00000 193.00000 1.00000 30.15625 21.87500
2014-04-11 13:10:25 49.00000 0.00000 196.00000 18.00000 30.15625 21.93750
2014-04-11 13:11:31 29.00000 14.00000 194.00000 10.00000 30.15625 21.93750
2014-04-11 13:12:41 20.00000 15.00000 196.00000 10.00000 30.15625 22.00000
2014-04-11 13:14:01 41.00000 34.00000 205.00000 7.00000 30.12500 22.00000
2014-04-11 13:15:13 48.00000 5.00000 206.00000 13.00000 30.18750 22.00000
2014-04-11 13:16:20 33.00000 26.00000 198.00000 3.00000 30.15625 21.93750
2014-04-11 13:17:31 6.00000 10.00000 193.00000 12.00000 30.15625 22.06250
2014-04-11 13:18:42 34.00000 39.00000 205.00000 4.00000 30.18750 22.00000
2014-04-11 13:19:49 0.00000 41.00000 204.00000 18.00000 30.18750 22.12500
2014-04-11 13:20:59 31.00000 35.00000 197.00000 19.00000 30.18750 22.00000
2014-04-11 13:22:11 16.00000 42.00000 207.00000 12.00000 30.18750 22.06250
2014-04-11 13:23:19 21.00000 0.00000 197.00000 5.00000 30.21875 22.18750
2014-04-11 13:24:30 10.00000 39.00000 196.00000 6.00000 30.15625 22.37500
2014-04-11 13:25:41 29.00000 19.00000 193.00000 15.00000 30.09375 22.43750
2014-04-11 13:26:50 27.00000 23.00000 208.00000 3.00000 30.06250 22.50000
2014-04-11 13:28:00 39.00000 45.00000 209.00000 18.00000 30.06250 22.62500   

소스코드


#include <stdio.h>

int main(void)
{
    FILE *gnuplotPipe;
    char buf[1024];

    gnuplotPipe = popen("gnuplot -persistent", "w");
    fprintf(gnuplotPipe, "set title '%s'\n", "Test Gnuplot Chart");
    fprintf(gnuplotPipe, "set xdata time\n");
    fprintf(gnuplotPipe, "set xlabel 'Time'\n");
    fprintf(gnuplotPipe, "set ylabel '°C'\n");   
    fprintf(gnuplotPipe, "set timefmt '%%Y-%%m-%%d %%H:%%M:%%S'\n");
    fprintf(gnuplotPipe, "set xrange['%s' : '%s']\n", "2014-04-11 13:06:15", "2014-04-11 13:28:00");
    fprintf(gnuplotPipe, "set format x '%%H:%%M:%%S'\n");
    fprintf(gnuplotPipe, "set xtics 240\n");
    fprintf(gnuplotPipe, "set datafile separator ' '\n");
    fprintf(gnuplotPipe, "set style line 2 linecolor rgb '#00ff00' lw 1\n");
    fprintf(gnuplotPipe, "set grid\n");
    fprintf(gnuplotPipe, "set terminal jpeg size 600,300\n");
    fprintf(gnuplotPipe, "set output 'test.jpeg'\n"); 
    fprintf(gnuplotPipe, "plot 'temp.dat' using 1:3 title '%s' with line ls 1\n", "temperature");
    fclose(gnuplotPipe);

    return 0;
}

위 코드에서 보는바와 같이 c에서 단순히 gnuplot 프로그램을 파이프로 열어 쓰기 데이터들을 gnuplot 명령으로 주어 test.jpeg 그래프 이미지를 생성한 것이다.

결과

참고