-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest
75 lines (55 loc) · 1.68 KB
/
test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from numpy import array
from numpy.random import rand
from PyQt4.Qt import QImage, QPainter, QBuffer, QIODevice, QByteArray
from pyqtgraph import GraphicsLayoutWidget
from pyqtgraph.opengl import GLViewWidget, GLMeshItem
from pyqtgraph.Qt import QtGui
%gui qt
verts = array([
[0, 0, 0],
[2, 0, 0],
[1, 2, 0],
[1, 1, 1],
])
faces = array([
[0, 1, 2],
[0, 1, 3],
[0, 2, 3],
[1, 2, 3]
])
colors = array([
[1, 0, 0, 0.3],
[0, 1, 0, 0.3],
[0, 0, 1, 0.3],
[1, 1, 0, 0.3]
])
class MyFigure():
def plot_2D(self):
self._widget = GraphicsLayoutWidget()
for i in range(5):
self._widget.addPlot().plot(rand(100))
self._widget.nextRow()
self._widget.show()
def plot_3D(self):
self._widget = GLViewWidget()
self._widget.setCameraPosition(distance=10)
m = GLMeshItem(vertexes=verts, faces=faces, faceColors=colors, smooth=False)
self._widget.addItem(m)
self._widget.show()
def _repr_png_(self):
self._widget.hide()
QtGui.QApplication.processEvents()
try:
self.image = QImage(self._widget.viewRect().size().toSize(),
QImage.Format_RGB32)
except AttributeError:
self._widget.updateGL()
self.image = self._widget.grabFrameBuffer()
painter = QPainter(self.image)
self._widget.render(painter)
byte_array = QByteArray()
buffer = QBuffer(byte_array)
buffer.open(QIODevice.ReadWrite)
self.image.save(buffer, 'PNG')
buffer.close()
return bytes(byte_array)