Search code examples
pythonmatplotlibalpha-transparency

Different alpha in arrows using quiver on top of rectangle


I have a Rectangle plotted with matplotlib with alpha=0.8 . I can plot a marker with different alpha on it but when i try to set the alpha parameter on the arrows of quiver it seems like it doesn't change. I was wondering if there is a way to set a larger amount of transparency for the arrows without changing the transparency of the Rectangle.

import matplotlib.pyplot as plt
figure=plt.figure(figsize=(10,8))  
ax=plt.gca()
plt.xticks(np.arange(-20,20,1))
plt.yticks(np.arange(-20,20,1))
rect=plt.Rectangle((1,1),10,10,facecolor='#32CD32',alpha=0.8)
ax.plot(2,2,marker='o',alpha=1,color='red')
ax.quiver( 2,2, 2,2, color='black', scale_units='inches', scale=10,width=0.0015,headlength=5,headwidth=3,alpha=1)
ax.add_patch(rect)
plt.axis('off')
plt.show()

enter image description here

That's the plot I am getting. I want the arrow to be dark blue without changing the alpha of the green above.


Solution

  • The quiver alpha is working as expected. The problem is with the zorder of the objects you are drawing. The rectangle is being drawn over the arrow but beneath the red dot:

    import numpy as np               # v 1.19.2
    import matplotlib.pyplot as plt  # v 3.3.2
    
    figure=plt.figure(figsize=(6,4))  
    ax=plt.gca()
    plt.xticks(np.arange(-20,20,1))
    plt.yticks(np.arange(-20,20,1))
    rect=plt.Rectangle((1,1),10,10,facecolor='#32CD32',alpha=0.8)
    ax.plot(2,2,marker='o',alpha=1,color='red')
    ax.quiver( 2,2, 20,20, color='black', scale_units='inches', scale=10, width=0.015,
              headlength=5,headwidth=3,alpha=1)
    ax.add_patch(rect)
    plt.axis('off')
    plt.show()
    

    problem

    This can be solved by setting the zorder parameter in each object so that the arrow is drawn over the rectangle:

    figure=plt.figure(figsize=(6,4))  
    ax=plt.gca()
    plt.xticks(np.arange(-20,20,1))
    plt.yticks(np.arange(-20,20,1))
    rect=plt.Rectangle((1,1),10,10,facecolor='#32CD32',alpha=0.8,zorder=1)
    ax.plot(2,2,marker='o',alpha=1,color='red')
    ax.quiver( 2,2, 20,20, color='black', scale_units='inches', scale=10, width=0.015,
              headlength=5,headwidth=3,alpha=1,zorder=2)
    ax.add_patch(rect)
    plt.axis('off')
    plt.show()
    

    solution