51 def __init__(self, parent, xvals, G, ylim=(0, 1),
56 colors=DEFAULT_COLORS, size=(1200, 600),
60 Initialize a bar scene
63 xvals: labels and x positions of the bars
64 G: Number of bars per x value
65 ylim: y limits of the figure
66 xlabel: label below x-axis
67 ylabel: label on left side of the y-axis
70 size: size of the plot in pixels
71 legend: list of legend strings to show.
72 legendpos: position of legend w.r.t. default position, in pixels
75 self.setSceneRect(QRect(0, 0, *size))
93 Lx = xsize - rightoffset - leftoffset
94 Ly = ysize - topoffset - bottomoffset
104 for i
in range(nyticks):
105 y = bottomoffset+Ly*i/(nyticks-1)
107 ytick = self.
addLine(leftoffset,
109 leftoffset-ticklength,
112 ygrid = self.
addLine(leftoffset,
115 y, pen=QPen(Qt.gray))
117 range_ = ylim[1]-ylim[0]
118 ytickval = i/(nyticks-1)*range_ + ylim[0]
119 yticklabel = f
'{ytickval:.0f}'
121 txtwidth = txt.boundingRect().width()
122 txtmaxwidth = max(txtmaxwidth, txtwidth)
123 txt.setPos(leftoffset-10-txtwidth,
124 ysize - y-.022*self.
ysize)
129 self.addItem(mainframe)
136 baritem = QGraphicsRectItem(barrect, brush=QBrush(Qt.blue))
138 self.addItem(baritem)
145 xticklabel = f
'{xvals[n]}'
149 self.
ysize-bottomoffset+xticklabeloffset)
152 xticklabels.append(txt)
155 if xlabel
is not None:
157 width = xlabel.boundingRect().width()
158 txtxpos = xsize/2-width/2
159 txtypos = ysize - xlabelbottomoffset
160 xlabel.setPos(txtxpos, txtypos)
164 if ylabel
is not None:
166 ylabel.setPos(ylabelleftoffset,
167 (ysize-topoffset-bottomoffset)/2+topoffset)
172 if title
is not None:
174 width = xlabel.boundingRect().width()
175 txtxpos = self.
xsize/2-width/2
176 txtypos = (1-.998)*self.
ysize
177 title.setPos(txtxpos, txtypos)
180 if legend
is not None:
182 legposx, legposy = (0, 0)
if legendpos
is None else legendpos
184 legpos = (xsize-rightoffset-300+legposx,
185 ysize-topoffset-30+legposy)
192 boxtopleft = QPointF(legpos[0]-legrectmargin,
193 ysize-legpos[1]-Lylegrect-legrectmargin)
195 legbox = self.addRect(QRectF(0, 0, 0, 0),
196 pen=QPen(), brush=QBrush(Qt.white))
198 for i, leg
in enumerate(legend):
202 pos = (legpos[0], legpos[1] - i*dyleg)
205 legrect = self.
createRect(*pos, Lxlegrect, Lylegrect)
207 legrect.setBrush(QBrush(color))
209 maxlegtxtwidth = max(maxlegtxtwidth,
210 legtxt.boundingRect().width())
212 self.addItem(legrect)
215 legtxt.setPos(legpos[0]+Lxlegrect,
216 ysize-pos[1]-dylegtxt-3)
218 legboxsize = QSize(maxlegtxtwidth+Lxlegrect+2*legrectmargin,
219 (i+1)*dyleg+legrectmargin)
220 legboxrect = QRectF(boxtopleft, legboxsize)
221 legbox.setRect(legboxrect)
225 Save bar image as a jpg file. Overwrites a file already existing in
228 https://stackoverflow.com/questions/7451183/how-to-create-image-file\
229 -from-qgraphicsscene-qgraphicsview#11642517
237 pixelsx = max(1200, size[0])
238 pixelsy = int(pixelsx*size[1]/size[0])
239 imagesize = (pixelsx, pixelsy)
240 image = QImage(pixelsx,
242 QImage.Format_ARGB32_Premultiplied)
244 painter = QPainter(image)
245 painter.setRenderHint(QPainter.Antialiasing)
246 painter.setBrush(Qt.white)
247 painter.setPen(Qt.white)
248 painter.drawRect(QRect(0, 0, *imagesize))
250 targetrect = QRectF(0, 0, *imagesize)
251 sourcerect = QRectF(0, 0, *size)
252 self.render(painter, targetrect, sourcerect)
255 return image.save(fn)
259 Save bar image as a eps file.
263 force: if True, overwrites an existing file. If false, raises a
264 RuntimeError if file already exists.
266 printer = QPrinter(QPrinter.HighResolution)
267 printer.setOutputFormat(QPrinter.PdfFormat)
268 printer.setOutputFileName(fn)
269 printer.setFullPage(
True)
270 printer.setPageSize(QPrinter.Custom)
271 printer.setPaperSize(QSizeF(*self.
size), QPrinter.Millimeter)
272 printer.setPageMargins(0, 0, 0, 0, QPrinter.Millimeter)
274 painter = QPainter(printer)
275 painter.setRenderHint(QPainter.Antialiasing)
276 painter.setBrush(Qt.white)
277 painter.setPen(Qt.white)
278 painter.drawRect(QRect(0, 0, *self.
size))
280 targetrect = QRectF(0, 0, printer.width(), printer.height())
281 sourcerect = QRectF(0, 0, *self.
size)
282 self.render(painter, targetrect, sourcerect)