1 說明
1.1 認識visvismk
Visvis is a pure Python library for visualization of 1D to 4D data in an object oriented way.
是純python的以面向物件的方式的1D~4D的資料視覺化作圖模組。
1.2 github地址:
https://github.com/almarklein/visvis
1.3 使用的人不多,網上介紹太少了,所以我來介紹技術,是一個可以作為python的基礎知識學習的好工具。
1.4 中文設定暫時還不能。
2 安裝
pip install visvis
3 bar3d=3D柱狀圖
bai3d.py程式碼
#匯出模組import visvis as vv#初始化定義,基本固定app = vv.use()#f = vv.clf() #可要可不要a = vv.cla()#資料定義,可自定義,資料是我假設的#x軸為月份x=[1,2,3,4,5,6,7,8,9,10,11,12]#bar的厚度=1y=[1]*12#溫度數值z=[12,20,21,14,17,15,13,18,14,22,21,19]#定義3d的bar,是bar3bar =vv.bar3(x, y, z, width=0.8)#柱子的顏色設定,預設是灰色bar.colors = ["r","g","b"] * 4#顯示網格,可要可不要,預設是不要的Falsea.axis.showGridZ = Truea.axis.showGridY = Truea.axis.showGridX = True#顯示x,y,z的比值,預設是1,1,1,但是由於z如果比值太大,顯示不好看,bar就會太高a.daspect = 1,1,0.3#z和x標籤a.axis.zLabel = 'Temperature [C^o]'a.axis.xLabel = 'month[m]'#注意起始點是0,所以0的位置標記為空=’‘a.axis.xTicks = ['','Jan ','Feb','Mar',' Apr', 'May', 'Jun', 'Jul',' Aug',' Sep',' Oct',' Nov',' Dec']#標題定義vv.title('bar3d')app.Run()
效果圖
4 如果有人說,我不要3d柱狀圖,就要普通的條形圖,可不可以呢?可以,簡單。
bar1d.py程式碼
#匯出模組import visvis as vv#初始化定義,基本固定app = vv.use()#f = vv.clf() #可要可不要a = vv.cla()#資料定義,可自定義,資料是我假設的#x軸為月份x=[1,2,3,4,5,6,7,8,9,10,11,12]#bar的厚度=1#y=[1]*12#溫度數值z=[12,20,21,14,17,15,13,18,14,22,21,19]bar =vv.bar(x,z, width=0.8)#柱子的顏色設定,預設是灰色bar.colors = ["r","g","b"] * 4#z和x標籤a.axis.zLabel = 'Temperature [C^o]'a.axis.xLabel = 'month[m]'#注意起始點是0,所以0的位置標記為空=’‘,如果註釋掉就是預設的資料標籤,Dec沒有標出來,buga.axis.xTicks = ['','Jan ','Feb','Mar',' Apr', 'May', 'Jun', 'Jul',' Aug',' Sep',' Oct',' Nov',' Dec']#標題定義vv.title('bar1d')app.Run()
效果圖
5 折線圖
line.py程式碼:
# 官方的plotting作圖# 匯出模組import visvis as vvapp = vv.use()#f = vv.clf()a = vv.cla()#資料vv.plot([12,34,21,38], lc='b', ls=':',mc='b', mw=7, lw=2, ms='s', mec='r')vv.plot([1,3,4],[33,47,12], lc='r', mc='r', ms='.')vv.plot([20,24,45,21], lc='g', ls='--', mc='g', mw=12, lw=3, ms='')vv.plot([35,14,40,31], lc='k', ls='-.', mc='g', mw=12, lw=3, ms='*')a = vv.gca()a.legend = 'line 1', 'line 2', 'line 3'a.axis.showGrid = 1a.axis.xlabel = 'measurement number'a.axis.ylabel = 'some quantity [unit]'#標題,字型加粗vv.title('An example of \\b{plotting}')app.Run()
效果圖:
6 4d茶壺
teapot4d.py程式碼
import visvis as vvapp = vv.use() #初始化app和最後的app的run,如果沒有,那麼動畫結束後就會指定關閉視窗# Create something to show, let's show a red teapot!mesh = vv.solidTeapot()#茶壺體的顏色設定mesh.faceColor = 'y'# PrepareNangles = 360a = vv.gca()f = vv.gcf()# Rotate camerafor i in range(Nangles): a.camera.azimuth = 360 * float(i) / Nangles if a.camera.azimuth>180: a.camera.azimuth -= 360 a.Draw() # Tell the axes to redraw f.DrawNow() # Draw the figure NOW, instead of waiting for GUI event loopapp.Run()
效果圖
7 聯合OpenGL製作三角體
程式碼:
import visvis as vvimport OpenGL.GL as glclass CustomWobject(vv.Wobject): def __init__(self, parent): vv.Wobject.__init__(self, parent) def _drawTraingles(self, how, color): a = 0, 0, 0 b = 1, 0, 0 c = 1, 1, 0 d = 1, 1, 1 gl.glColor(*color) gl.glBegin(how) gl.glVertex(*a); gl.glVertex(*b); gl.glVertex(*c) gl.glVertex(*a); gl.glVertex(*c); gl.glVertex(*d) gl.glVertex(*b); gl.glVertex(*c); gl.glVertex(*d) gl.glVertex(*a); gl.glVertex(*d); gl.glVertex(*b) gl.glEnd() def _GetLimits(self): # Get limits x1, x2 = 0, 1 y1, y2 = 0, 1 z1, z2 = 0, 1 # Return return vv.Wobject._GetLimits(self, x1, x2, y1, y2, z1, z2) def OnDraw(self): """ To draw the object. """ self._drawTraingles(gl.GL_TRIANGLES, (0.2, 0.8, 0.4)) gl.glLineWidth(3) self._drawTraingles(gl.GL_LINE_LOOP, (0,0,0)) def OnDrawShape(self, clr): """ To draw the shape of the object. Only necessary if you want to be able to "pick" this object """ self._drawTraingles(gl.GL_TRIANGLES, clr) # Create an instance of this class and set axes limitsa = vv.cla()c = CustomWobject(a)a.SetLimits()# Enter main loopapp = vv.use() # let visvis chose a backend for meapp.Run()
效果圖:
8 還可以畫sun-earth-moon三體運動
程式碼:
import visvis as vv# Create floorfloor = vv.solidBox((0,0,-1.5), (6,6,1))# Create hierachy objectssun = vv.solidSphere()earth = vv.solidSphere((2,0,0),(0.3,0.3,0.2))moon = vv.solidSphere((2,0,0),scaling=(0.2, 0.2, 0.3))moon.parent = earthearth.parent = sun# Add transformationssunTrans = sun.transformations[0]earthRot = vv.Transform_Rotate(20)moonRot = vv.Transform_Rotate(20)earth.transformations.insert(0,earthRot)moon.transformations.insert(0,moonRot)# Set appearanceearth.faceColor = 'b'moon.faceColor = 'y'sun.faceColor = 'r'# Set axes settingsaxes = vv.gca()axes.SetLimits(rangeZ=(-2,3))# Define timer funcsun.zSpeed = 0.2def onTimer(event): # Move moon moonRot.angle += 20 if moonRot.angle > 360: moonRot.angle = 0 # Move earth earthRot.angle += 5 if earthRot.angle > 360: earthRot.angle = 0 # Move sun sun.zSpeed -= 0.01 sunTrans.dz += sun.zSpeed # Detect bounce if sunTrans.dz < 0: sun.zSpeed *= -1 sunTrans.dz = 0 # Update! axes.Draw()# Create time and enter main looptimer = vv.Timer(axes, 100, False)timer.Bind(onTimer)timer.Start()app = vv.use()app.Run()
效果圖:
介紹到這裡了,喜歡就好。
最新評論