Python:在Plotly中的图形上方创建注释空间

2024-10-01 02:32:16 发布

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

我想在绘图中为注释创建额外的空间(请参见附图中的绿色区域)。 此时,y轴定义了绘图的高度。我是否可以将绘图推到y.max限制之外/在某个点后隐藏y轴(在图像中标记为红色)? 我尽量避免轴心伸入“评论区”(绿色)

谢谢大家!

enter image description here

enter image description here


Tags: 标记图像image区域绘图高度定义评论
1条回答
网友
1楼 · 发布于 2024-10-01 02:32:16

在回答了你的另一个问题后,我无意中发现了这个问题,觉得很有趣

这就是你想要的吗

关键要素:

创建提升注释的关键要素包括:

  • xrefyref被设置为'paper',因此使用图形的背景作为xy坐标,而不是图形数据值
  • 使用>;1.00用于y定位
  • layout['margin']top设置一个值以创建上边距,从而向下推图形区域

示例代码:

它确实比看起来简单得多

import numpy as np
from plotly.offline import iplot

# Create the dataset.
n = 360
x = np.arange(n)
y = [np.sin(i*(np.pi/180)) for i in range(n)]

# Plot the sin wave.
data = []
data.append({'x': x,
             'y': y})

# Create the annotations.
notes = []
notes.append({'text': ('An annotation at the top of the graph.<br><br>'
                       'And some more rambling text to take up some '
                       'more space on the graph to represent<br>'
                       'a longer annotation, just because we can.<br><br>'
                       'And one more for good measure.'),
              'x': 0.00,
              'y': 1.60,
              'xref': 'paper',
              'yref': 'paper',
              'showarrow': False,
              'align': 'left'})
notes.append({'text': 'Elevated Graph Annotation Example',
              'x': 0.00,
              'y': 1.15,
              'xref': 'paper',
              'yref': 'paper', 
              'showarrow': False,
              'font': {'size': 18}})

# Setup the layout.
layout = {}
layout['annotations'] = notes
layout['margin'] = {'t': 175}

# Plot the data. (May also use `plot` rather than `iplot`)
iplot({'data': data, 'layout': layout})

注:

您必须创建自己的图形标题(而不是依赖于layout中可用的title参数),因为您需要自己定位图形标题。否则,自动标题和注释将重叠

相关问题 更多 >