放大绘图时隐藏文本注释
Hiding text annotation when zooming in on a plotly graph
下面的第一张图是默认视图,我在其中插入了文本注释来标记线条。当我放大以查看图形的(几乎不可见的)底部区域时,文本注释仍然存在但没有线条,因此没有任何意义。有什么方法可以在用户放大时隐藏它?
import pandas as pd
import plotly.graph_objects as go
world_df = pd.read_csv('https://raw.githubusercontent.com/vyaduvanshi/helper-files/master/world_df.csv')
fig = go.Figure()
fig.add_scatter(x=world_df.date, y=world_df.population, fill='tozeroy')
fig.add_scatter(x=world_df.date, y=world_df.total_cases, fill='tozeroy', mode='lines', line_color='#0099ff')
fig.add_scatter(x=world_df.date, y=world_df.herd_immunity_threshold)
fig.update_layout(annotations=[dict(x=0.5, y=0.7, xref='paper', yref='paper', showarrow=False,
text='something something line')])
参数xref='paper'
和yref='paper'
将相对于图表而不是实际坐标放置注释。这意味着因为您有 y=0.7
,无论您缩放多少,您的注释都将保留在图表底部和顶部之间 70% 的位置。相反,您应该设置 yref='y'
,然后将坐标传递给参数 y
(我根据原始图的轨迹将其设置为 5222515 亿)。
import pandas as pd
import plotly.graph_objects as go
world_df = pd.read_csv('https://raw.githubusercontent.com/vyaduvanshi/helper-files/master/world_df.csv')
fig = go.Figure()
fig.add_scatter(x=world_df.date, y=world_df.population, fill='tozeroy')
fig.add_scatter(x=world_df.date, y=world_df.total_cases, fill='tozeroy', mode='lines', line_color='#0099ff')
fig.add_scatter(x=world_df.date, y=world_df.herd_immunity_threshold)
fig.update_layout(annotations=[dict(x=0.5, y=5.222515*10**9, xref='paper', yref='y', showarrow=False,
text='something something line')])
fig.show()
下面的第一张图是默认视图,我在其中插入了文本注释来标记线条。当我放大以查看图形的(几乎不可见的)底部区域时,文本注释仍然存在但没有线条,因此没有任何意义。有什么方法可以在用户放大时隐藏它?
import pandas as pd
import plotly.graph_objects as go
world_df = pd.read_csv('https://raw.githubusercontent.com/vyaduvanshi/helper-files/master/world_df.csv')
fig = go.Figure()
fig.add_scatter(x=world_df.date, y=world_df.population, fill='tozeroy')
fig.add_scatter(x=world_df.date, y=world_df.total_cases, fill='tozeroy', mode='lines', line_color='#0099ff')
fig.add_scatter(x=world_df.date, y=world_df.herd_immunity_threshold)
fig.update_layout(annotations=[dict(x=0.5, y=0.7, xref='paper', yref='paper', showarrow=False,
text='something something line')])
参数xref='paper'
和yref='paper'
将相对于图表而不是实际坐标放置注释。这意味着因为您有 y=0.7
,无论您缩放多少,您的注释都将保留在图表底部和顶部之间 70% 的位置。相反,您应该设置 yref='y'
,然后将坐标传递给参数 y
(我根据原始图的轨迹将其设置为 5222515 亿)。
import pandas as pd
import plotly.graph_objects as go
world_df = pd.read_csv('https://raw.githubusercontent.com/vyaduvanshi/helper-files/master/world_df.csv')
fig = go.Figure()
fig.add_scatter(x=world_df.date, y=world_df.population, fill='tozeroy')
fig.add_scatter(x=world_df.date, y=world_df.total_cases, fill='tozeroy', mode='lines', line_color='#0099ff')
fig.add_scatter(x=world_df.date, y=world_df.herd_immunity_threshold)
fig.update_layout(annotations=[dict(x=0.5, y=5.222515*10**9, xref='paper', yref='y', showarrow=False,
text='something something line')])
fig.show()