python如何绘制无界线和跨距?

2024-10-01 17:30:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我在IPython笔记本中使用plotly(离线版本),我非常喜欢。然而,我找不到一个方法来绘制一条垂直线或一个垂直带。在

matplotlib中的等价物是:

import matplotlib.plyplot as plt
plt.axvline(x=0)
plt.axvspan(xmin=0, xmax=1)

提前谢谢


Tags: 方法import版本matplotlibasipython绘制笔记本
3条回答

可以将形状添加到绘图布局中。形状可以包括直线或矩形。也可以通过相对于打印区域(而不是特定轴)绘制它们,使其无边界。请看一下plotly shapes docs中的示例。在

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,
                }
            }
        ],
    }

我会在布局中使用“形状”选项。例如,要获得x=6处的垂直线:

layout = {'title' : 'example',
          'shapes' : [{'type' : 'line', 'x0' : 6, 
                      'x1' : 6, 'y0' : 0, 'y1' : 10, 
                       'width' : 1}]}

您可以更改width参数以绘制垂直标注栏。在

我刚开始和我自己密谋,到目前为止,我还没有找到一个好的方法。但如果你只需要一条水平或垂直线,下面的代码似乎是一个可行的黑客,想法是使用默认网格,但只在所需的高度绘制一条网格线。在布局dict中包含以下内容,它将在yline处绘制一条水平线。在

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

相关问题 更多 >

    热门问题