简体   繁体   English

plotly - 用箭头显示 x 轴

[英]plotly - show x-axis with arrow head

How to draw a x-axis with arrow head at the right side?如何在右侧绘制带箭头的 x 轴? 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:当前结果: 在此处输入图像描述

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.我知道可以在 plotly 中装饰 x 轴,但是没有在它的尖端添加箭头的功能。 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.为此,我们将在 x,y 轴上添加一个限制并确定右端。

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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM