python - 使用自定义绘图标题时,“plotly.subplots”中的注释会中断
问题描述
我有一个奇怪的问题 - 不确定这是一个错误还是我遗漏了一些东西。
- 在绘制子图
Code 1
并为每个子图分配自定义标题时 - 我稍后尝试添加到底部图的注释未完全显示,而仅显示最后两个 (Output Plot 1
)。
代码 1:
import numpy as np
from plotly.subplots import make_subplots
from math import exp
fig = make_subplots(2, 1, subplot_titles=['Activation', 'Prediction errors - Model, ALO and RND'])
x = np.linspace(0, 10, 100)
y = np.array(list(map(lambda x: 1 / (1 + exp(-0.1 * x + 5)), x)))
fig.add_trace(
go.Scatter(
x=x,
y=y,
name=f'\N{Greek Small Letter Sigma}(x)',
showlegend=True
),
row=1,
col=1
)
fig['layout']['xaxis'].update(title_text='x')
for i in range(4):
x = np.where(np.random.randint(0, 2, 100)==1)[0]
fig.add_trace(
go.Scatter(
x=x,
y=np.zeros_like(x) + i,
name=f'Plot {i}',
mode='markers',
marker=dict(
symbol='circle-open',
color='green',
size=5
),
showlegend=True
),
row=2,
col=1
)
fig['layout']['xaxis2'].update(title_text='active users', range=[0, 10], autorange=True)
fig['layout']['yaxis2'].update(title_text='active users', visible=False, autorange=True)
fig['layout'].update(
annotations=[
dict(x=0, y=0.125, xref='x2', yref='y2', text='True activity', font=dict(size=10, color='green')),
dict(x=0, y=1.125, xref='x2', yref='y2', text='Model', font=dict(size=10, color='blue')),
dict(x=0, y=2.125, xref='x2', yref='y2', text='ALO', font=dict(size=10, color='red')),
dict(x=0, y=3.125, xref='x2', yref='y2', text='RND', font=dict(size=10, color='black')),
]
)
fig.show()
输出图 1
另一方面,如果我删除自定义绘图标签 - 一切都按预期工作,如Code 2
和 output所示Image 2
。
代码 2:
import numpy as np
from plotly.subplots import make_subplots
from math import exp
fig = make_subplots(2, 1)#, subplot_titles=['Activation', 'Prediction errors - Model, ALO and RND']) # <= note the change here
x = np.linspace(0, 10, 100)
y = np.array(list(map(lambda x: 1 / (1 + exp(-0.1 * x + 5)), x)))
fig.add_trace(
go.Scatter(
x=x,
y=y,
name=f'\N{Greek Small Letter Sigma}(x)',
showlegend=True
),
row=1,
col=1
)
fig['layout']['xaxis'].update(title_text='x')
for i in range(4):
x = np.where(np.random.randint(0, 2, 100)==1)[0]
fig.add_trace(
go.Scatter(
x=x,
y=np.zeros_like(x) + i,
name=f'Plot {i}',
mode='markers',
marker=dict(
symbol='circle-open',
color='green',
size=5
),
showlegend=True
),
row=2,
col=1
)
fig['layout']['xaxis2'].update(title_text='active users', range=[0, 10], autorange=True)
fig['layout']['yaxis2'].update(title_text='active users', visible=False, autorange=True)
fig['layout'].update(
annotations=[
dict(x=0, y=0.125, xref='x2', yref='y2', text='True activity', font=dict(size=10, color='green')),
dict(x=0, y=1.125, xref='x2', yref='y2', text='Model', font=dict(size=10, color='blue')),
dict(x=0, y=2.125, xref='x2', yref='y2', text='ALO', font=dict(size=10, color='red')),
dict(x=0, y=3.125, xref='x2', yref='y2', text='RND', font=dict(size=10, color='black')),
]
)
fig.show()
输出图 2
提前感谢您对此行为的任何详细说明。
解决方案
我不确定这是否是由于字幕的设置,但add_annotation()
是否正确,update(annotations=[]
可能已被函数覆盖以更新已设置的注释。
import plotly.graph_objects as go
import numpy as np
from plotly.subplots import make_subplots
from math import exp
fig = make_subplots(2, 1, subplot_titles=('Activation', 'Prediction errors - Model, ALO and RND'))
x = np.linspace(0, 10, 100)
y = np.array(list(map(lambda x: 1 / (1 + exp(-0.1 * x + 5)), x)))
fig.add_trace(
go.Scatter(
x=x,
y=y,
name=f'\N{Greek Small Letter Sigma}(x)',
showlegend=True
),
row=1,
col=1
)
fig['layout']['xaxis'].update(title_text='x')
for i in range(4):
x = np.where(np.random.randint(0, 2, 100)==1)[0]
fig.add_trace(
go.Scatter(
x=x,
y=np.zeros_like(x) + i,
name=f'Plot {i}',
mode='markers',
marker=dict(
symbol='circle-open',
color='green',
size=5
),
showlegend=True
),
row=2,
col=1
)
fig['layout']['xaxis2'].update(title_text='active users', autorange=True)
fig['layout']['yaxis2'].update(title_text='active users', visible=True, range=[-1,4])
fig.add_annotation(
dict(x=0, y=3.125, xref='x2', yref='y2', text='RND', showarrow=True, font=dict(size=10, color='black'))
)
fig.add_annotation(
dict(x=0, y=2.125, xref='x2', yref='y2', text='ALO', showarrow=True, font=dict(size=10, color='red'))
)
fig.add_annotation(
dict(x=0, y=1.125, xref='x2', yref='y2', text='Model', showarrow=True, font=dict(size=10, color='Blue'))
)
fig.add_annotation(
dict(x=0, y=0.125, xref='x2', yref='y2', text='True activity', showarrow=True, font=dict(size=10, color='green'))
)
fig.show()
推荐阅读
- kubernetes - 由于同步错误而跳过部署:复制文件:运行错误:无法升级连接:找不到容器(\\“auth\\”)
- c# - 带有注入数据访问层的测试类
- selenium - TypeError:float() 参数必须是字符串或数字,而不是使用 selenium webdriver 的“超时”
- pointers - 使用 ANativeWindow 获取高度和宽度时的空指针取消引用
- java - 如何将字符串变量添加到 SQL 查询 - Java、SpringBoot
- java - 当 Object 有多个构造函数时无法使用 ObjectMapper 反序列化
- c# - c#检测特定窗口(IE浏览器、文件资源管理器等)是否关闭/用户按下X按钮?
- julia - 如何在循环中收集 NL 表达式?
- spyder - 运行脚本直到当前行
- python - 如何在 django.values() 中保留属性值?