sciapp / gr

GR framework: a graphics library for visualisation applications
Other
329 stars 54 forks source link

How to use drawimage to render a 2D array? #39

Closed kaigaox closed 5 years ago

kaigaox commented 7 years ago

Hi,

New user to GR. One small question: I tried to use drawimage to render a 2D float-type array using whichever colormap, but did not figure out how to do that. Can anyone indicate?

Also, could anyone indicate where I can find the source code for the image plot (the first image on the homepage, http://gr-framework.org/, the 2D image plot with a logarithmic colorbar)?

Thanks very much! Kai

cfelder commented 7 years ago

Hi,

the following code renders a 2D image (4, 4) containing rgba colours:

#!/usr/bin/env python
# coding: utf-8
import numpy as np
import gr

gr.clearws()
gr.setviewport(.1, .9, .1, .9)
gr.setwindow(-.5, 3.5, -.5, 3.5)
gr.axes(1, 1, -0.5, -0.5, 1, 1, .01)
a = np.zeros(16, dtype=np.uint32) | (0xff << 24)  # alpha
a[0] |= 0xff          # red
a[1] |= (0xff << 8)   # green
a[2] |= (0xff << 16)  # blue
gr.drawimage(-0.5, 3.5, 3.5, -0.5, 4, 4, a)
gr.updatews()

gks

But as far as I understood you want to colour your image based on a colormap. Therefore you can use gr.cellarray (http://gr-framework.org/gr.html#output-functions)

#!/usr/bin/env python
# coding: utf-8
import numpy as np
import gr

gr.clearws()
gr.setviewport(.1, .9, .1, .9)
gr.setwindow(-.5, 3.5, -.5, 3.5)
gr.axes(1, 1, -0.5, -0.5, 1, 1, .01)
a = np.arange(16, dtype=np.uint32)
amin, amax = a.min(), a.max()
a = 1000 + 255 * (a - amin) / (amax - amin)
gr.cellarray(-0.5, 3.5, 3.5, -0.5, 4, 4, a)
gr.updatews()

gks

jheinen commented 7 years ago

This is the same GR example using convenience functions in Julia:

using GR
x = reshape(linspace(0,1,16),4,4)
heatmap(x',colormap=1)

screen shot 2017-05-15 at 13 35 35 ... or Python:

from numpy import linspace, reshape, rot90
from gr.pygr import *
x = linspace(0,1,16).reshape(4,4)
heatmap(rot90(x.T),colormap=1)
kaigaox commented 7 years ago

Thanks very much for your kind replies. I was trying to develop a small array-plotting program using C and GR, and met the above problem.

I met another question when using cellarray: when I try to use gr_cellarray(-0.5, 3.5, 3.5, -0.5, 4, 4, a) in C, the error is error #165: too few arguments in function call. Not quite sure why is that.

Could someone kindly indicate?

Also, is there any way to add a colorbar using GR in C?

Many thanks. Kai

jheinen commented 7 years ago

Yes:


#include <stdio.h>
#include <stdlib.h>

#include "gr.h"

int main(int argc, char *argv[])
{
  double a[16], amin, amax;
  int i, colia[16];

  amin = 0; amax = 1;
  for (i = 0; i < 16; i++)
    a[i] = i / 15.0;

  gr_setviewport(.1, .8, .2, .9);
  gr_setwindow(-.5, 3.5, -.5, 3.5);
  gr_axes(1, 1, -0.5, -0.5, 1, 1, -0.01);

  for (i = 0; i < 16; i++)
    colia[i] = 1000 + (int) (255 * (a[i] - amin) / (amax - amin));

  gr_setcolormap(1);
  gr_cellarray(-0.5, 3.5, 3.5, -0.5, 4, 4, 1, 1, 4, 4, colia);

  gr_setviewport(.825, .85, .2, .9);
  gr_colorbar();

  gr_updatews();
  getchar();
}
danielkaiser commented 5 years ago

We are closing this issue due to inactivity. Please reopen the issue and let us know if its cause still persists with the current version of GR.