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.
setAxisScaleDraw(QwtPlot.
xBottom, DiscreteAxisScaleDraw
(labels
))
# ####################################################################
# draw labels for discrete attributes
def __init__(self, labels):
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
]))
plot.setAxisMaxMinor(QwtPlot.xBottom, 0)
plot.setAxisMaxMajor(QwtPlot.xBottom, len(labels))
plot.axisScaleDraw(QwtPlot.xBottom).enableComponent(QwtScaleDraw.Backbone, 1)
plot.axisScaleDraw(QwtPlot.xBottom).enableComponent(QwtScaleDraw.Ticks, 1)
plot.axisScaleDraw(QwtPlot.xBottom).enableComponent(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]))
To copy to clipboard, switch view to plain text mode
However, when rescaling the x-axis by
plot.
setAxisScale(QwtPlot.
xBottom,
-0.2,
20,
4.2)
plot.setAxisScale(QwtPlot.xBottom, -0.2, 20,4.2)
To copy to clipboard, switch view to plain text mode
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.
Bookmarks