Search code examples
pythonmatplotlibtextures

Brick wall with matplotlib


I'm working on a program about a soliton going through a barrier and I'm trying to make the barrier look like a real one, so to say, to be made of bricks. The problem is that I couldn't found how to plot the 'brick' texture. I use fill_between() but if there's another option that admits bricks, I wouldn't have problem with using it.

My code is:

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')

with all the arrays well defined and so. There's no problem with the code when running the program, but I would like to know how to draw the bricks if that's possible.

I attach an image of the actual situation, the barrier is empty for the moment waiting to be built with bricks, to make it more visual.

Image of the plot


Solution

  • This would be an example on how to place a brick wall into the image

    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