sciapp / gr

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

Fine grained control over X and Y ticks. #79

Open v0dro opened 5 years ago

v0dro commented 5 years ago

What is the best way to gain control over X and Y ticks? I read the PlotAxes class in the Python wrapper but could not find anything concrete. I'm mainly looking for a way to supply tick labels and their various properties like text rotation, color, placement on the Axis etc.

cfelder commented 5 years ago

In order to have fine grained control of the labels you can supply callback functions for the x and y axis which will be called for every tick label.

You can either use gr.axeslbl which has basically the same signature as gr.axes but adds two function pointers fpx and fpy or if you want to use the PlotAxes class you can use setXtickCallback and setYtickCallback to point to the corresponding functions.

The callback functions should have 4 parameters: x, y, svalue, value where::

Within the callback function you can simply use gr drawing primitives to draw the text, e.g: the following code which prints a formatted date and time string on the x axis and rotates the labels:

        gr.setcharup(-1. if self.leftTurnedLegend else 1., 1.)
        gr.settextalign(gr.TEXT_HALIGN_RIGHT, gr.TEXT_VALIGN_TOP)
        dx = .015
        timeVal = time.localtime(value)
        gr.text(x + dx, y, time.strftime(DATEFMT, timeVal))
        gr.text(x - dx , y, time.strftime(TIMEFMT, timeVal))
        gr.setcharup(0., 1.)  # restore default charup vector
v0dro commented 5 years ago

Ah. Would be great to see this documented. I can send a PR if you're OK with it?

jheinen commented 5 years ago

Feel free to submit a PR 👍

v0dro commented 5 years ago

On it :)

cfelder commented 5 years ago

@v0dro great. Here is also some example output: gr_axeslbl

v0dro commented 5 years ago

@cfelder is there a way to set the co-ordinates of each tick and the label without relying on the parameters passed to axeslbl? For example something like pyplot.xticks.