Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)

C# - PLplot의 16색 이상을 표현하는 방법과 subpage를 이용한 그리드 맵 표현

지난 글에서는 PLplot으로 Histogram 그리기와,

C# - MathNet으로 정규 분포를 따르는 데이터를 생성, PLplot으로 Histogram 표현
; https://www.sysnet.pe.kr/2/0/11946

색상 표현이 기본 16개의 palette로 관리된다고 했습니다.

C# - PLplot 색상 제어
; https://www.sysnet.pe.kr/2/0/11920

이 때문에, 만약 0 ~ 15 범위를 벗어나는 인덱스를 컬러 값 사용으로 바꾸면 오류가 발생합니다.

pl.col0(16);

/* 오류 발생
*** PLPLOT ERROR, ABORTING OPERATION ***
plcol0: Invalid color map entry: 16, aborting operation
*/

물론 16색을 초과하는 색상을 표현하는 방법이 있고, 이에 대해서는 PLplot 2번 예제에서 볼 수 있습니다.

PLplot - Example 02
; http://plplot.org/examples.php?demo=02

예를 들어, 100개의 추가 색상을 palette에 구성한다고 했을 때 R, G, B 색상 값을 담을 배열을 100 + 16만큼 마련합니다.

int[] r = new int[116];
int[] g = new int[116];
int[] b = new int[116];

그다음, 최하위의 기본 색상 16개는 theme 컬러의 원래 값으로 채워주고,

for (i = 0; i <= 15; i ++)
{
    pl.gcol0(i, out r[i], out g[i], out b[i]);
}

나머지 영역에 100개의 원하는 색상을 추가하면 됩니다.

for (i = 0; i <= 99; i++)
{
    r[i + 16] = ...;
    g[i + 16] = ...;
    b[i + 16] = ...;
}

마지막으로, 이렇게 새롭게 구성한 palette을 color map에 설정합니다.

pl.scmap0(r, g, b);

pl.col0(16); // 오류 없이 정상적으로 17번째 색상을 사용




색상은 해결되었으니, 이제 PLplot을 이용해 grid map처럼,

plplot_grid_map_1.png

표현하는 방법을 보겠습니다. 이것 역시, 색상과 동일한 예제에서 그 방법을 찾을 수 있습니다.

PLplot - Example 02
; http://plplot.org/examples.php?demo=02

그런데, 개념이 재미있습니다. ^^ 우선, 하나의 그림을 그리는 Canvas를 Page로 다루는데, 이 Page를 다시 여러 개의 Subpage로 나눌 수 있습니다.

private static void demo1(PLStream pl)
{
    pl.bop();

    int nx = 4;
    int ny = 4;

    {
        pl.ssub(nx, ny); // 하나의 화면을 4x4 canvas로 분할
    }

    pl.eop();
}

일단 subpage로 나눈 시점부터는 drawing 명령어들이 아무런 페이지도 선택되지 않은 상태이므로 pl.adv 명령어를 한번 호출해 subpage를 선택하는 걸로 시작해야 합니다.

pl.adv(0); // next sub page를 선택, 최초 호출이면 (0,0) sub page 선택

그다음, 해당 subpage의 viewport와 window 영역을 설정하고,

double vmin = 0.0, vmax = 1.0;

pl.vpor(vmin, vmax, vmin, vmax);
pl.wind(vmin, vmax, vmin, vmax);

설정된 영역의 최소/최대 값을 기준으로 drawing 명령어를 사용하면 됩니다. 아래는 이것을 모두 통합한 소스 코드입니다.

private static void demo1(PLStream pl)
{
    pl.bop();

    int nx = 4;
    int ny = 4;

    {
        pl.ssub(nx, ny);

        draw_gridmap(pl, nx * ny, 0);
    }

    pl.eop();
}

private static void draw_gridmap(PLStream pl, int nw)
{
    double vmin = 0.0, vmax = 1.0;

    for (int i = 0; i < nw; i ++)
    {
        pl.col0(i);

        pl.adv(0);

        pl.vpor(vmin, vmax, vmin, vmax);
        pl.wind(vmin, vmax, vmin, vmax);

        plfbox(pl);
    }
}

private static void plfbox(PLStream pl)
{
    double[] x = { 0, 0, 1.0, 1.0 };
    double[] y = { 0, 1.0, 1.0, 0 };

    pl.fill(x, y);
}

위의 소스 코드는 기본 컬러 16 색상만 사용했지만, 만약 100개 색상으로 늘려 표현하고 싶다면 다음과 같은 식으로 소스 코드를 만들면 됩니다.

private static void demo2(PLStream pl)
{
    pl.bop();
    pl.ssub(10, 10);

    fillColor(pl, out int[] r, out int[] g, out int[] b);
    pl.scmap0(r, g, b);

    draw_gridmap(pl, 100, 16);

    pl.eop();
}

static void fillColor(PLStream pl, out int [] r, out int [] g, out int [] b)
{
    r = new int[116];
    g = new int[116];
    b = new int[116];

    double lmin = 0.15, lmax = 0.85;

    for (int i = 0; i <= 15; i++)
    {
        pl.gcol0(i, out r[i], out g[i], out b[i]);
    }

    for (int i = 0; i <= 99; i++)
    {
        double h, l, s;
        double r1, g1, b1;

        h = (360.0 / 10.0) * (i % 10);
        l = lmin + (lmax - lmin) * (i / 10) / 9.0;
        s = 1.0;

        pl.hlsrgb(h, l, s, out r1, out g1, out b1);
        r[i + 16] = (int)(r1 * 255.001);
        g[i + 16] = (int)(g1 * 255.001);
        b[i + 16] = (int)(b1 * 255.001);
    }
}

private static void draw_gridmap(PLStream pl, int nw, int cmap0_offset)
{
    double vmin = 0.0, vmax = 1.0;

    for (int i = 0; i < nw; i ++)
    {
        pl.col0(i + cmap0_offset);

        pl.adv(0);

        pl.vpor(vmin, vmax, vmin, vmax);
        pl.wind(vmin, vmax, vmin, vmax);

        plfbox(pl);
    }
}

plplot_grid_map_2.png

(이 글의 예제 코드는 github - PLplotGridmap에서 제공합니다.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 8/29/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 61  62  63  [64]  65  66  67  68  69  70  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
12046정성태10/29/201910381오류 유형: 577. windbg - The call to LoadLibrary(...\sos.dll) failed, Win32 error 0n193
12045정성태10/27/20199721오류 유형: 576. mstest.exe 실행 시 "Visual Studio Enterprise is required to execute the test." 오류 - 두 번째 이야기
12044정성태10/27/20199935오류 유형: 575. mstest.exe - System.Resources.MissingSatelliteAssemblyException: The satellite assembly named "Microsoft.VisualStudio.ProductKeyDialog.resources.dll, ..."
12043정성태10/27/201910747오류 유형: 574. Windows 10 설치 시 오류 - 0xC1900101 - 0x4001E
12042정성태10/26/201911149오류 유형: 573. OneDrive 하위에 위치한 Documents, Desktop 폴더에 대한 권한 변경 시 "Unable to display current owner"
12041정성태10/23/201911159오류 유형: 572. mstest.exe - The load test results database could not be opened.
12040정성태10/23/201911425오류 유형: 571. Unhandled Exception: System.Net.Mail.SmtpException: Transaction failed. The server response was: 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied
12039정성태10/22/20199822스크립트: 16. cmd.exe의 for 문에서는 ERRORLEVEL이 설정되지 않는 문제
12038정성태10/17/20199382오류 유형: 570. SQL Server 2019 RC1 - SQL Client Connectivity SDK 설치 오류
12037정성태10/15/201915591.NET Framework: 867. C# - Encoding.Default 값을 바꿀 수 있을까요?파일 다운로드1
12036정성태10/14/201916349.NET Framework: 866. C# - 고성능이 필요한 환경에서 GC가 발생하지 않는 네이티브 힙 사용파일 다운로드1
12035정성태10/13/201912486개발 환경 구성: 461. C# 8.0의 #nulable 관련 특성을 .NET Framework 프로젝트에서 사용하는 방법 [2]파일 다운로드1
12034정성태10/12/201911835개발 환경 구성: 460. .NET Core 환경에서 (프로젝트가 아닌) C# 코드 파일을 입력으로 컴파일하는 방법 [1]
12033정성태10/11/201915527개발 환경 구성: 459. .NET Framework 프로젝트에서 C# 8.0/9.0 컴파일러를 사용하는 방법
12032정성태10/8/201912002.NET Framework: 865. .NET Core 2.2/3.0 웹 프로젝트를 IIS에서 호스팅(Inproc, out-of-proc)하는 방법 - AspNetCoreModuleV2 소개
12031정성태10/7/20199427오류 유형: 569. Azure Site Extension 업그레이드 시 "System.IO.IOException: There is not enough space on the disk" 예외 발생
12030정성태10/5/201915701.NET Framework: 864. .NET Conf 2019 Korea - "닷넷 17년의 변화 정리 및 닷넷 코어 3.0" 발표 자료 [1]파일 다운로드1
12029정성태9/27/201915790제니퍼 .NET: 29. Jennifersoft provides a trial promotion on its APM solution such as JENNIFER, PHP, and .NET in 2019 and shares the examples of their application.
12028정성태9/26/201911590.NET Framework: 863. C# - Thread.Suspend 호출 시 응용 프로그램 hang 현상을 해결하기 위한 시도파일 다운로드1
12027정성태9/26/20198846오류 유형: 568. Consider app.config remapping of assembly "..." from Version "..." [...] to Version "..." [...] to solve conflict and get rid of warning.
12026정성태9/26/201912524.NET Framework: 862. C# - Active Directory의 LDAP 경로 및 정보 조회
12025정성태9/25/201910869제니퍼 .NET: 28. APM 솔루션 제니퍼, PHP, .NET 무료 사용 프로모션 2019 및 적용 사례 (8) [1]
12024정성태9/20/201912308.NET Framework: 861. HttpClient와 HttpClientHandler의 관계 [2]
12023정성태9/18/201912735.NET Framework: 860. ServicePointManager.DefaultConnectionLimit와 HttpClient의 관계파일 다운로드1
12022정성태9/12/201915797개발 환경 구성: 458. C# 8.0 (Preview) 신규 문법을 위한 개발 환경 구성 [3]
12021정성태9/12/201927720도서: 시작하세요! C# 8.0 프로그래밍 [4]
... 61  62  63  [64]  65  66  67  68  69  70  71  72  73  74  75  ...