Posted by José Lopes.
If you are using ReportLab to generate graphics you may find necessary to costumize the label axis to show only some of the entries, in order to get a more readable graphic.
The solution I propose is simple.
You must create an auxiliary function like:
(1)def axisCostumized (axis, space):
axis_costumized = []
k = 0
for entry in axis:
(2) if k == 0:
axis_costumized.append(entry)
else:
(3) if k == len(axis)-1:
axis_costumized.append(entry)
else:
(4) if (k/space) > (k//space):
axis_costumized.append("")
else:
axis_costumized.append(entry)
k += 1
return axis_costumized
Call this function to costumize your axis before generating the graph. You will not need to change any of your graphic functions, you just format the list that defines the axis.
Explaining the code:
(1) The function takes two arguments, the axis is a list with the graphic axis labels, and
space is an integer that you define to set the space between visible labels.
(2) Here we assure that the first label of the axis is always visible.
(3) Here we do the same for the last label of the axis.
(4) Here we check if the entry (label name) should be or not included by checking if the entry position divided by
the integer given (space) is also an integer.
As you can see the length of the costumized axis is the same as the original and the "hidden" labels were replaced by an empty string.