PDA

View Full Version : QWTPlot x-axis labels with float spacing



Pyquestor
12th May 2011, 13:40
Hi there,

I have a QwtPlot where I plotted 5 groups of histograms and an average line above.
To label the different groups individually by quarters (e.g. Q1/2010, Q2/2010, Q3/2010, Q4/2010, Q1/2011) I used for the x-axis:



plot.setAxisMaxMinor(QwtPlot.xBottom, 0)
plot.setAxisMaxMajor(QwtPlot.xBottom, len(labels))
plot.axisScaleDraw(QwtPlot.xBottom).enableComponen t(QwtScaleDraw.Backbone, 1)
plot.axisScaleDraw(QwtPlot.xBottom).enableComponen t(QwtScaleDraw.Ticks, 1)
plot.axisScaleDraw(QwtPlot.xBottom).enableComponen t(QwtScaleDraw.Labels, 1)

plot.setAxisScaleDraw(QwtPlot.xBottom, DiscreteAxisScaleDraw(labels))

# ################################################## ##################
# draw labels for discrete attributes
class DiscreteAxisScaleDraw(QwtScaleDraw):
def __init__(self, labels):
apply(QwtScaleDraw.__init__, (self,))
self.labels = labels


def label(self, value):
index = int(round(value))
if index != value: return QwtText("") # if value not an integer value return ""
if index >= len(self.labels) or index < 0: return QwtText("")
return QwtText(str(self.labels[index]))


However, when rescaling the x-axis by


plot.setAxisScale(QwtPlot.xBottom, -0.2, 20,4.2)


as to set the ticks at the beginning of each of the histogram groups,
the labels disappear apart from the first one (see figure). I guess this is due to the fact that the ticks are placed at a non-integer distance (step size 4.2).
When using a step size =1, the labels are visible but not at the right position due to the integer spacing.
Does anyone know how to solve this problem?

Thanks a lot in advance.