Open v0dro opened 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::
x
is the normalised device coordinate of the label in x directiony
is the normalised device coordinate of the label in y directionsvalue
is the internal string representation respectively the text usually drawn by gr at x, yvalue
is the floating point representation of the label drawn at x, yWithin 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
Ah. Would be great to see this documented. I can send a PR if you're OK with it?
Feel free to submit a PR 👍
On it :)
@v0dro great. Here is also some example output:
@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
.
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.