首頁>技術>

1 說明:

=====

1.1 Glumpy:是一個OpenGL+NumPy庫,它用OpenGL來進行快速Numpy視覺化。

1.2 即:Python+Numpy+OpenGL實現快速,可擴充套件和美觀的科學視覺化。

1.3 它是一個由Nicolas Rougier啟動的開源專案,致力於高效視覺化。

1.4 Glumpy使用OpenGL紋理(textures)來表示陣列,因為這恐怕是在現代圖形硬體上最快的視覺化方法了。

2 準備:

=====

2.1 官網:

Glumpy:高效美觀的科學視覺化庫

Glumpy - Python+Numpy+OpenGL: fast, scalable and beautiful scientific visualization

2.2 環境:

華為膝上型電腦、深度deepin-linux作業系統、谷歌瀏覽器、python3.8和微軟vscode編輯器。

2.3 安裝:

pip install glumpy#本機安裝sudo pip3.8 install glumpy#本機推薦安裝sudo pip3.8 install -i https://mirrors.aliyun.com/pypi/simple glumpy

3 優美動畫賞析:

===介紹入門和防止掉坑===

4 第一個視窗:

===========

4.1 註釋版程式碼:

from glumpy import app#本機安裝過pyglet,而且glumpy預設是pygletapp.use("pyglet") #預設這個,但本機推薦這個#注意沒有上面的use的指定,會報錯,也會自動預設pyglet開啟#比如下面的報錯#[w] Backend (<module 'glumpy.app.window.backends.backend_glfw' #from '/usr/local/python3.8/lib/python3.8/site-packages/glumpy/app/window/backends/backend_glfw.py'>) not available#可以用的視窗模組#backend : ['osxglut', 'freeglut', 'pyglet', 'glfw', 'sdl', 'sdl2', 'pyside'] Graphical toolkit to use#window = app.Window()#例項化視窗,大小設定,視窗名#背景顏色color=(0,0,0,1)=黑色;color=(1,1,1,1)=白色;注意RGBA=RGB-A=RGB三基色+A透明度window = app.Window(800,800,'The First win(第一個視窗)',color=(1,1,1,1)) #支援中文#window = app.Window(width=800,height=800,title='The First win(第一個視窗)') #等同上面#注意:如果沒有這個視窗時間,那麼上面的背景顏色設定就是預設黑色,一直沒有反應@window.eventdef on_draw(dt): window.clear()app.run()#duration=5==持續5秒後自動關閉視窗#app.run(duration=5)#run的引數:framerate=幀率=60(秒)#clock=None, framerate=None, interactive=None, duration=sys.maxsize, framecount=sys.maxsize

4.2 上面簡潔版程式碼:

from glumpy import appapp.use("pyglet") #預設這個,但本機推薦這個window = app.Window(800,800,'The First win(第一個視窗)',color=(1,1,1,1)) #支援中文@window.eventdef on_draw(dt): window.clear()app.run()

4.3 圖:

5 你好世界:Helloworld:

==================

5.1 程式碼:

from glumpy import appfrom glumpy.graphics.text import FontManagerfrom glumpy.graphics.collections import GlyphCollectionfrom glumpy.transforms import Position, OrthographicProjection#視窗大小和標題名設定window = app.Window(1000, 1000,'你好世界')#window = app.Window(width=1000, height=1000,title='你好世界') #等同於上面的#app.use("glfw") app.use("pyglet") #本機採用這個@window.eventdef on_draw(dt): window.clear() label.draw()x,y,z = 0,500,0#自定義中文字型:路徑和字型名,字型大小設定font = FontManager.get("/home/xgj/Desktop/glumpy/hwfs.ttf", 20, mode='agg')label = GlyphCollection('agg', transform=OrthographicProjection(Position()))#color=(1,1,1,1)為白色#anchor_x = 'center', anchor_y = 'center'label.append("Hello World 你好世界!", font,anchor_x = 'left', anchor_y = 'center',origin=(x,y,z), color=(0.6,0.4,0.2,1))window.attach(label["transform"])app.run()

5.2 圖:

6 多彩愛心:

========

6.1 程式碼:

import numpy as npfrom glumpy import appfrom glumpy.graphics.collections import MarkerCollectionapp.use("pyglet")#視窗大小,視窗名,背景顏色白色window = app.Window(1024,1024, '多彩愛心滿屏',color=(1,1,1,1))@window.eventdef on_draw(dt): window.clear() markers.draw() markers['orientation'] += np.random.uniform(0.0,0.1,len(markers)) del markers[0] #逐個刪除列表的0位愛心,逐個減少 if not len(markers): #最後沒有退出 app.quit()n = 556markers = MarkerCollection(orientation='local')markers.append(np.random.uniform(-1,1,(n,3)), bg_color = np.random.uniform(0,1,(n,4)), size = 64, fg_color=(0,0,0,1))window.attach(markers["transform"])window.attach(markers["viewport"])app.run()

6.2 效果圖:

7 colorful Scatter:

============

7.1 程式碼:

import numpy as npfrom glumpy import appfrom glumpy.graphics.collections import PointCollectionapp.use("pyglet")#背景顏色color=(1,1,1,1)為白色window = app.Window(1024,1024,'多彩散點圖', color=(.2,.2,.2,1))points = PointCollection("agg", color="local", size="local")@window.eventdef on_draw(dt): window.clear() points.draw() if len(points) < 100000: points.append(np.random.normal(0.0,0.5,(1,3)), color = np.random.uniform(0,1,4), size = np.random.uniform(1,24,1))window.attach(points["transform"])window.attach(points["viewport"])app.run()

7.2 效果圖:

8 green wave:

==========

8.1 程式碼:

#oscilloscope==green waveimport numpy as npfrom glumpy import app, gl, glm, gloovertex = """attribute float x, y, intensity;varying float v_intensity;void main (void){ v_intensity = intensity; gl_Position = vec4(x, y, 0.0, 1.0);}"""fragment = """varying float v_intensity;void main(){ gl_FragColor = vec4(0,v_intensity,0,1);}"""app.use("pyglet") #預設這個,但本機推薦這個window = app.Window(width=1024, height=512,title='greenwave')@window.eventdef on_draw(dt): global index window.clear() oscilloscope.draw(gl.GL_LINE_STRIP) index = (index-1) % len(oscilloscope) oscilloscope['intensity'] -= 1.0/len(oscilloscope) oscilloscope['y'][index] = np.random.uniform(-0.25, +0.25) oscilloscope['intensity'][index] = 1.0index = 0oscilloscope = gloo.Program(vertex, fragment, count=150)oscilloscope['x'] = np.linspace(-1,1,len(oscilloscope))app.run()

8.2 圖:

9 matplotlib的API:

=============

9.1 程式碼:

import numpy as npfrom glumpy.api.matplotlib import *#推薦增加這2行#from glumpy import app#app.use("pyglet") #預設這個,但本機推薦這個# Create a new figurefigure = Figure((24,12))# Create a subplot on left, using trackball interface (3d)left = figure.add_axes( [0.010, 0.01, 0.485, 0.98], xscale = LinearScale(clamp=True), yscale = LinearScale(clamp=True), zscale = LinearScale(clamp=True), interface = Trackball(name="trackball"), facecolor=(1,0,0,0.25), aspect=1 )# Create a subplot on right, using panzoom interface (2d)right = figure.add_axes( [0.505, 0.01, 0.485, 0.98], xscale = LinearScale(domain=[-2.0,+2.0], range=[0.25,1.00]), yscale = LinearScale(domain=[-2.0,+2.0], range=[0,2*np.pi]), projection = PolarProjection(), interface = Trackball(name="trackball"), facecolor=(0,0,1,0.25), aspect=1 )# Create a new collection of pointscollection = PointCollection("agg")# Add a view of the collection on the left subplotleft.add_drawable(collection)# Add a view of the collection on the right subplotright.add_drawable(collection)# Add some pointscollection.append(np.random.uniform(-2,2,(10000,3)))# Show figurefigure.show()

9.2 圖:

10 簡化qt5:

==========

10.1 程式碼:

import numpy as npfrom glumpy import appfrom PyQt5.QtWidgets import QMainWindow, QWidget, QVBoxLayout, QPushButtonapp.use("qt5")window = app.Window()#window = app.Window(width=1000, height=1000,title='qt5') #設定視窗大小和名稱無效qwindow = QMainWindow()#原始碼中無下面2行,自己增加的,這樣設定主視窗位置和大小、標題名qwindow.setGeometry(0,0,800,800) #自己增加qwindow.setWindowTitle("Glumpy qt5") #自己增加widget = QWidget()button = QPushButton("Press me!")qwindow.setCentralWidget(widget)widget.setLayout(QVBoxLayout())widget.layout().addWidget(window._native_window)widget.layout().addWidget(button)@window.eventdef on_draw(dt): window.clear()@button.clicked.connectdef on_click(): window.color = np.random.rand(4)qwindow.show()app.run()

10.2 圖:

===自己整理並分享出來===

最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • Azure Application Gateway(一)對後端 Web App 進行負載均衡