2015-04-25 201 views
2

我正在使用Matplotlib來繪製一些科學圖表,其中熱圖圖(如下圖所示)足夠。我的代碼全部寫在Python之中。在Matplotlib中繪製字形的陰謀

enter image description here

不過,現在我需要展示一個更加「闡述了」的情節,其中包括字形。下面的圖像(第二個)示出一個例子:

enter image description here

在該圖像中,曲線圖中的每個點是表示一個矢量場取向的概率的字形。這是字形被畫成一個具有主方向和與其方向的標準偏差的圓。

我想做類似的事情。我的想法是在每個位置繪製極座標直方圖,並繪製由極座標圖組成的圖。但是,我不認爲Matplotlib是可能的。至少我不知道如何做到這一點。由於我的整個代碼都是用Python編寫的,所以我想知道是否可以用Matplotlib來支持,或者我應該學習如何用OpenGL或其他API/libraty來做到這一點。

謝謝。

+0

我可以從您的問題描述和您發佈的圖片中瞭解您的問題。這可能是,它可能是,你沒有花費足夠的精力來準確地解釋你想要什麼... – gboffi

+1

我會去楔子而不是直方圖 - http://matplotlib.org/api/patches_api.html ?highlight = patch#matplotlib.patches.Wedge – cphlewis

+0

&可能將定義的字形存儲在....三維空間中?空間來定義它們,因爲它看起來像你重複使用它們(至少在讀者可以分辨的範圍內)。大醇'字典或你的字形的東西。 – cphlewis

回答

2

也許一大堆的是這樣的:

enter image description here

import matplotlib.pyplot as plt 
import numpy as np 
from matplotlib.collections import PatchCollection 
from matplotlib.patches import Wedge, Circle 
from math import degrees, pi 

fig, ax = plt.subplots() 
wedges = [] 
circles = [] 

for x in np.arange(0, 3.3, .3): 
    for y in np.arange(0, 3.3, .3): 
     theta, phi = np.random.random(2) # functions of (x,y) in reality 
     for v in (0, pi): 
      wedges.append(Wedge((x, y), 
          .15, 
          degrees(v - phi - theta/2), 
          degrees(v - phi + theta/2), 
          edgecolor='none'), 
          ) 
     circles.append(Circle((x, y), 
          .15, 
          edgecolor='none')) 


colors = np.linspace(0, 1, len(circles)) # function of (x,y) in reality 
collection = PatchCollection(circles, cmap=plt.cm.jet, alpha=0.2) 
collection.set_array(np.array(colors)) 
collection.set_edgecolor('none') 
ax.add_collection(collection) 

#wedgecolors = list(chain.from_iterable(repeat(i,2) for i in colors)) 
wedgecolors = np.array([colors, colors]).flatten('F') # no itertools 
collection = PatchCollection(wedges, cmap=plt.cm.jet, alpha=1) 
collection.set_array(np.array(wedgecolors)) 
collection.set_edgecolor('none') 
ax.add_collection(collection) 

ax.set_xlim(0,3) 
ax.set_ylim(0,3) 
ax.set_aspect('equal') 
plt.show() 

(設置edgecolor有collection.set_array通話之後進行(重做),顯然?)

+0

謝謝@cphlewis。這正是我所期待的。 (: – pceccon

+0

嗨,@cphlewis。我在運行你的代碼時收到一個錯誤。導入itertools模塊後,我得到「Exception in Tkinter callback」。不知道是什麼。謝謝。 – pceccon

+0

我沒有使用Tkinter,不知道......已經放入了一個只使用numpy的替代方法,可能你應該隔離新問題並將其作爲Tk/itertools標籤的問題發佈。 – cphlewis