Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kaigaox opened this issue May 14, 2017 · 5 comments
Closed

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

kaigaox opened this issue May 14, 2017 · 5 comments

Comments

@kaigaox
Copy link

kaigaox commented May 14, 2017

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
Copy link
Contributor

cfelder commented May 15, 2017

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
Copy link
Collaborator

jheinen commented May 15, 2017

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
Copy link
Author

kaigaox commented May 16, 2017

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
Copy link
Collaborator

jheinen commented May 16, 2017

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
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants