Plotly python 如何绘制无界线和跨度?

Jav*_*bie 6 python plotly

我在 IPython 笔记本中使用 plotly(离线版本),我非常喜欢它。但是我找不到绘制垂直线或垂直带的方法。

matplotlib 中的等价物是:

import matplotlib.plyplot as plt
plt.axvline(x=0)
plt.axvspan(xmin=0, xmax=1)
Run Code Online (Sandbox Code Playgroud)

提前致谢

Kie*_*ran 5

您可以向绘图布局添加形状。形状可以包括线条或矩形。也可以通过相对于绘图区域而不是特定轴绘制它们来使它们不受限制。查看plotly 形状文档中的示例。

layout = {
        'title': "My Chart",
        'shapes': [
            {  # Unbounded line at x = 4
                'type': 'line',
                # x-reference is assigned to the x-values
                'xref': 'x',
                # y-reference is assigned to the plot paper [0,1]
                'yref': 'paper',
                'x0': 4,
                'y0': 0,
                'x1': 4,
                'y1': 1,
                'line': {
                    'color': 'rgb(55, 128, 191)',
                    'width': 3,
                }
            },
            {  # Unbounded span at 6 <= x <= 8
                'type': 'rect',
                # x-reference is assigned to the x-values
                'xref': 'x',
                # y-reference is assigned to the plot paper [0,1]
                'yref': 'paper',
                'x0': 6,
                'y0': 0,
                'x1': 8,
                'y1': 1,
                'fillcolor': '#d3d3d3',
                'opacity': 0.2,
                'line': {
                    'width': 0,
                }
            }
        ],
    }
Run Code Online (Sandbox Code Playgroud)


小智 0

我自己才刚刚开始,到目前为止我还没有找到一个好的方法来做到这一点。但是,如果您只需要一条水平线或垂直线,下面的代码似乎是一个可行的技巧,其想法是使用默认网格,但仅在所需高度绘制一条网格线。在布局字典中包含以下内容,它将在yline处绘制一条水平线。

  yaxis=dict(
         zeroline=False,
         autotick=False,
         showgrid=True,
         tick0=yline,
         dtick=<something very large, so that next lines are out of range>
  )
Run Code Online (Sandbox Code Playgroud)