2016-11-19 42 views
0

我正在研究一個關於孤子穿過一個障礙的程序,我試圖讓這個障礙看起來像一個真正的,可以說,由磚塊製成。問題是,我無法找到如何繪製'磚'紋理。我使用fill_between(),但如果有另外一個選項允許使用磚塊,我不會有使用它的問題。磚牆與matplotlib

我的代碼是:

gs=GridSpec(8,1) #7 rows and 1 column 
state=self.fig.add_subplot(gs[2:,:]) 
light=self.fig.add_subplot(gs[0:2,:]) 
state.set_xlabel("Position ($x/ \\xi$)") 
state.set_ylabel("Density $|\psi|^2 \\xi$") 
state.plot(posit,phi2) 
state.fill_between(posit,phi2,0,facecolor='0.80') 
potential=state.twinx() 
potential.plot(posit,pote,'g') 

與所有陣列明確定義等等。運行程序時代碼沒有問題,但如果可能的話,我想知道如何繪製磚塊。

我附上了一張實際情況的圖片,這個屏障在等待磚頭建成的那一刻是空的,讓它更加可視化。

感謝先進!

Image of the plot

+1

如果你能找到一個合適的圖像的例子...... [這個例子]( http://matplotlib.org/examples/pylab_examples/demo_annotation_box.html#pylab-examples-example-code-demo-annotation-box-py)放置在一個圖上的圖像。 – wwii

回答

3

這將是對如何磚牆放入圖像

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.patches 

f = lambda x, x0, sig: np.exp(-(x-x0)**2/sig**2) 

x = np.linspace(0,10, 301) 
pulse = f(x, 2, 0.5)*2.8 


fig, ax = plt.subplots(figsize=(10,4)) 

image = plt.imread("brick_texture104.png") 
#http://p78i.imgup.net/brick_textadf0.png 
im = ax.imshow(image, extent=[4,4+512./256,0,933./256]) # image is 512 x 933, 
ax.set_aspect("equal") 
ax.plot(x, pulse, color="r", alpha = 0.7, lw=4) 

wall_patch = matplotlib.patches.Rectangle((4.5,0),1,3, transform=ax.transData) 
im.set_clip_path(wall_patch) 

ax.set_ylim([0,4]) 
ax.set_xlim([0,10]) 
plt.show() 

enter image description here

+0

謝謝!它非常完美!現在看起來好多了! – Nana20