matplotlib arrow color not work as expected

Question:

How to change the arrow color in below demo code?

import matplotlib.pyplot as plt

def save_fig(fig,pngname):
    fig.savefig(pngname, dpi=fig.dpi, bbox_inches="tight")
    print("[[%s]]"%pngname)    
    return

def add_arrow(fig,ax,lw,lc):
    xmin,xmax = ax.get_xlim()
    ax.arrow(xmin,0,xmax-xmin+.2,0,#fc='k', ec='k',
         lw = lw,head_width=.1,head_length=0.4, overhang = 0, 
         length_includes_head=False, clip_on = False,edgecolor='k',facecolor=lc) 
    return

def main():
    x = [
    #"FLAGS",
    "INTENDED_VSYNC",
    "VSYNC",
    "OLDEST_INPUT_EVENT",
    "HANDLE_INPUT_START",
    "ANIMATION_START",
    "PERFORM_TRAVERSALS_START",
    "DRAW_START",
    "SYNC_QUEUED",
    "SYNC_START",
    "ISSUE_DRAW_COMMANDS_START",
    "SWAP_BUFFERS",
    "FRAME_COMPLETED",
   ]

    lw = 2
    lc = "grey"
    fig, ax = plt.subplots(1, figsize=(8,.2))
    y = [0]*len(x)
    ax.plot(x,y,color=lc)

    ax.set_ylim([0,0.1])
    plt.xticks(rotation=45,ha='right')
    ax.tick_params(direction = 'inout',color=lc)
    ax.tick_params('both', length=20, width=lw, which='major')
    ax.tick_params('both', length=10, width=lw, which='minor')
    plt.yticks([], [])
    for direction in ["left", "right", "bottom", "top"]:     
        ax.spines[direction].set_visible(False)
    add_arrow(fig,ax,lw,lc)
    #save_fig(fig,sdir + "/vsync.png")
    plt.show()
    return
sdir = "/home/tester"
main()

Output:
enter image description here

Asked By: lucky1928

||

Answers:

The color is defined in ax.arrow() by color, edgecolor and facecolor.

  • edgecolor sets the color of the edge of the arrow.
  • facecolor sets the color in the arrow body.
  • color sets the color of both, edgecolor and facecolor.

If you would like to have a uniformly colored arrow, you can either set color to the desired value or alternatively set both, edgecolor and facecolor to the same value. In this case it would mean removing edgecolor and facecolor and adding color=lc or alternatively replacing edgecolor='k' with edgecolor=lc.


import matplotlib.pyplot as plt

def save_fig(fig,pngname):
    fig.savefig(pngname, dpi=fig.dpi, bbox_inches="tight")
    print("[[%s]]"%pngname)    
    return

def add_arrow(fig,ax,lw,lc):
    xmin,xmax = ax.get_xlim()
    ax.arrow(xmin,0,xmax-xmin+.2,0,#fc='k', ec='k',
         lw = lw,head_width=.1,head_length=0.4, overhang = 0, 
         length_includes_head=False, clip_on = False,color=lc) 
    return

def plot():
    x = [
    #"FLAGS",
    "INTENDED_VSYNC",
    "VSYNC",
    "OLDEST_INPUT_EVENT",
    "HANDLE_INPUT_START",
    "ANIMATION_START",
    "PERFORM_TRAVERSALS_START",
    "DRAW_START",
    "SYNC_QUEUED",
    "SYNC_START",
    "ISSUE_DRAW_COMMANDS_START",
    "SWAP_BUFFERS",
    "FRAME_COMPLETED",
   ]

    lw = 2
    lc = "grey"
    fig, ax = plt.subplots(1, figsize=(8,.2))
    y = [0]*len(x)
    ax.plot(x,y,color=lc)

    ax.set_ylim([0,0.1])
    plt.xticks(rotation=45,ha='right')
    ax.tick_params(direction = 'inout',color=lc)
    ax.tick_params('both', length=20, width=lw, which='major')
    ax.tick_params('both', length=10, width=lw, which='minor')
    plt.yticks([], [])
    for direction in ["left", "right", "bottom", "top"]:     
        ax.spines[direction].set_visible(False)
    add_arrow(fig,ax,lw,lc)
    #save_fig(fig,sdir + "/vsync.png")
    plt.show()
    return

plot()

enter image description here

Answered By: fabianegli
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.