plotly python 将 y 轴值更改为您自己的值

plotly python change y-axis values ​to your own

绘制了随机值图。需要把Y轴的值改成其他的。

xg = np.random.rand(100, 1200)
fig = px.imshow(xg, aspect="auto",color_continuous_scale='ice')
fig.show()

要更改的值

y1=np.arange(100)*0.5
y2=np.arange(100)*5

谢谢!

import numpy as np
import plotly.express as px

xg = np.random.rand(100, 1200)
fig = px.imshow(xg, aspect="auto",color_continuous_scale='ice')

# y1
fig.update_layout(yaxis={"tickmode":"array","tickvals":np.arange(10)*10, "ticktext":np.arange(10)*5}).show()
# y2
fig.update_layout(yaxis={"tickmode":"array","tickvals":np.arange(10)*10, "ticktext":np.arange(10)*50}).show()


补充 - 显示两个 y 轴

  • 根据评论,您可以使用记录的方法 https://plotly.com/python/multiple-axes/#multiple-axes
  • 您确实需要每个轴的轨迹,因此创建两条轨迹并修改第二条轨迹以使用 y2
  • 将第二个跟踪设置为 "visible":False 不起作用。所以有两条相同的痕迹相互重叠
import numpy as np
import plotly.express as px

xg = np.random.rand(100, 1200)
fig = px.imshow(xg, aspect="auto", color_continuous_scale="ice").add_traces(
    px.imshow(xg, aspect="auto", color_continuous_scale="ice")
    .update_traces(yaxis="y2")
    .data
)

fig.update_layout(
    xaxis={"domain": [0.05, 1]},
    yaxis={
        "tickmode": "array",
        "tickvals": np.arange(10) * 10,
        "ticktext": np.arange(10) * 50,
    },
    yaxis2={
        "tickmode": "array",
        "tickvals": np.arange(10) * 10,
        "ticktext": np.arange(10) * 5,
        "anchor": "free",
        "position": 0,
        "autorange": "reversed",
    },
).show()