在 plotly 中自定义带有和不带有标签的刻度

Customize ticks with and without labels in plotly

如何去除非整数的标签而不是刻度,即 4.5、5.5、6.5 …,但同时保留整数的标签和刻度,即 4、5、6 …,在下面的例子?

import plotly.express as px
df = px.data.iris()

fig = px.scatter(df, x="sepal_width", y="sepal_length", facet_col="species")

fig.update_yaxes(linewidth=0.5,
                ticks="outside", tickwidth=0.5, tickcolor='blue', ticklen=10, dtick=0.5,
                tickfont_size=22, 
                )

fig.show()

您可以为 tickvalsticktext

构建数组
import plotly.express as px
import numpy as np

df = px.data.iris()

fig = px.scatter(df, x="sepal_width", y="sepal_length", facet_col="species")

fig.update_yaxes(linewidth=0.5,
                ticks="outside", tickwidth=0.5, tickcolor='blue', ticklen=10, dtick=0.5,
                tickfont_size=22, 
                )

la = int(df["sepal_length"].min())
lm = int(df["sepal_length"].max().round(0))
ticks = np.arange((lm - la) * 2) * 0.5 + la
fig.update_yaxes(
    tickmode="array",
    tickvals=ticks,
    ticktext=np.where(ticks.astype(int) == ticks, ticks, " "),
)

fig.show()