lucky1928
lucky1928

Reputation: 8849

plotly - show x-axis with arrow head

How to draw a x-axis with arrow head at the right side? example:

import re
import numpy as np
import plotly.express as px

def save_fig(fig,pngname):
    fig.write_image(pngname,format="png", width=800, height=300, scale=1)
    print("[[%s]]"%pngname)
    #plt.show()
    return

def plot(x,y):
    fig = px.scatter(
        x=x,
        y=y,
        error_y=[0] * len(y),
        error_y_minus=y
    )
    
    tickvals = [0,np.pi/2,np.pi,np.pi*3/2,2*np.pi]
    ticktext = ["0","$\\frac{\pi}{2}$","$\pi$","$\\frac{3\pi}{4}$","$2\pi$"]
    layout = dict(
        title="demo",
        xaxis_title="X",
        yaxis_title="Y",
        title_x=0.5,   
        margin=dict(l=10,t=20,r=0,b=40),
        height=300,
        xaxis=dict(
            tickangle=0,
            tickvals = tickvals,
            ticktext=ticktext,
        ),
        yaxis=dict(
            showgrid=True,
            zeroline=False,
            showline=False,
            showticklabels=True
        )
    )

    fig.update_traces(
        marker_size=14,
    )    
    fig.update_layout(layout)
    
    fig.show()

Current results: enter image description here

Upvotes: 1

Views: 469

Answers (1)

r-beginners
r-beginners

Reputation: 35155

I understand that it is possible to decorate the x-axis in plotly, but there is no function to add an arrow to the tip of it. So a possible technique would be to use the arrow in the annotation. To do that, we would add a limit on the x,y axis and determine the right end.

import re
import numpy as np
import plotly.express as px

def save_fig(fig,pngname):
    fig.write_image(pngname,format="png", width=800, height=300, scale=1)
    print("[[%s]]"%pngname)
    #plt.show()
    return

def plot(x,y):
    fig = px.scatter(
        x=x,
        y=y,
        error_y=[0] * len(y),
        error_y_minus=y
    )

    tickvals = [0,np.pi/2,np.pi,np.pi*3/2,2*np.pi]
    ticktext = ["0","$\\frac{\pi}{2}$","$\pi$","$\\frac{3\pi}{4}$","$2\pi$"]
    layout = dict(
        title="demo",
        xaxis_title="X",
        yaxis_title="Y",
        title_x=0.5,   
        margin=dict(l=10,t=20,r=0,b=40),
        height=300,
        xaxis=dict(
            range=[-0.45, 6.75],
            tickangle=0,
            tickvals=tickvals,
            ticktext=ticktext,
        ),
        yaxis=dict(
            range=[-1.2, 1.2],
            showgrid=True,
            zeroline=False,
            showline=False,
            showticklabels=True,
        )
    )
    fig.add_annotation(x=6.75, y=-1.2,
                       xref="x", yref="y",
                       axref='x', ayref='y',
                       showarrow=True, arrowhead=2,
                       arrowsize=2, arrowwidth=1, 
                       arrowcolor="#FF0000",
                       ax=-0.45, ay=-1.2, opacity=1.0)
    fig.update_traces(
        marker_size=14,
    )    
    fig.update_layout(layout)
    fig.show()

plot(np.linspace(0,6.3,20), np.sin(np.linspace(0,6.3,20)))

enter image description here

Upvotes: 1

Related Questions