python - Plotly:如何将 ticktext 重新定位到零线附近?

标签 python plotly

默认情况下,Plotly 似乎将 x 和 y 刻度放置在图形的外围,但是我有一个图形,其 y 零线位于中间。我希望刻度线位于 y 轴旁边,但我找不到实现这一点的命令。有可能吗? Image of my graph in Plotly. I'd like the f(omega) to be close to the central axis

最佳答案

据我所知,目前没有直接的方法可以做到这一点。但是如果你对 y 值使用 xaxis zerolineannotations 的正确组合,你可以得到这个:

enter image description here

下面的代码片段采用 y 值的最大值和最小值,构建具有定义的步骤数的间隔列表,并为这些步骤插入注释。它不是很优雅,当你尝试缩放时它有点失败,但它有效。至少现在您不必自己尝试类似的方法。

完整代码:

import plotly.graph_objects as go
import numpy as np

x = np.arange(-4,5)
y=x**3

yticks=list(range(y.min(), y.max(), 14))
#yticks.append(y.max())9

fig = go.Figure(data=go.Scatter(x=x, y=y))

fig.update_layout(xaxis = dict(zeroline=True,
                               zerolinecolor='firebrick',
                               title_text = "x zeroline",
                               title_standoff = 4))

fig.update_layout(yaxis = dict(zeroline=True,
                               showgrid=False,
                               title_font = {"size": 20},
                               tickfont= dict(color='rgba(0,0,0,0)')))

for i, m in enumerate(yticks):
    fig.add_annotation(dict(font=dict(color='firebrick',size=12),
                                        x=0.066,
                                        y=yticks[i],
                                        showarrow=False,
                                        text=str(yticks[i])+' -',
                                        textangle=0,
                                        xanchor='right',
                                        xref="x",
                                        yref="y"))


fig.show()

关于python - Plotly:如何将 ticktext 重新定位到零线附近?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62708946/

相关文章:

python - 如何从 3D numpy meshgrid 中提取 2D 平面

python - 在 matplotlib 中绘制不同颜色的箭头

python - 如何在 Python 的外部程序中打开文件?

python - 需要使用 Python 脚本从 Linux 机器连接 HP QC

python - python如何区分有符号和无符号数据类型?

python - 动画 plotly 与 `plotly`

javascript - SVG 到 PDF 的转换适用于 Plotly.js 但不适用于 C3.js

javascript - 无法使用 JSON 数组绘制折线图

python-3.x - 使用 pandas 隐藏曲面图上的图例和比例信息,plotly

python - 如何在 Dash 中创建按钮以从系列中排除值