express - 摆脱覆盖的 px.bar 图上的文本标签
问题描述
我已经成功地垂直绘制了五个条形图(都具有相同的 x 轴),一个在另一个之上。每个图表分别代表不同变量(由长关键短语标识)在同一时期内的出现时间。我已将 px.bar 与 barmode = 'overlay' 和 facet_row = "Key_phrases" 一起使用。除了图例之外,该例程坚持在右侧边框上垂直书写“Key_phrases”,但短语只要它们相互重叠即可。我怎样才能关闭它并在其单独的情节中水平地写下每个短语?
解决方案
- plotly express为多面图中的每一行迹线创建注释
- 已经创建了一些生成长KeyPhrase的示例数据
- 以您描述的方式生成图形
- 演示了覆盖注释文本。另一种选择是删除注释
phrase = "The COP26 summit will bring parties together to accelerate action towards the goals of the Paris Agreement and the UN Framework Convention on Climate Change "
df = pd.DataFrame(
{
"Key_phrases": np.repeat(
[" ".join(phrase.split(" ")[5 * p : 5 * (p + 1)]) for p in range(5)], 4
),
"x": np.tile(range(5), 4),
"y": np.random.uniform(1, 4, 20),
}
)
fig = px.bar(df, x="x", y="y", facet_row="Key_phrases")
fig.show()
# override annotation text
fig.update_layout(
annotations=[
{**a, **{"text": f"phr.{i+1}"}}
for i, a in enumerate(fig.to_dict()["layout"]["annotations"])
]
)
推荐阅读
- hibernate - Hibernate Envers:加载修订版时列表中出现空值
- php - 如何从 PHP 中的字符串中获取表达式?
- python-3.x - Python 不正确的值
- html - 如何在图像悬停时创建深色淡入淡出+文字?
- javascript - 为什么我下面的 IIFE 代码只与 clickEvent 一起工作一次
- javascript - Javascript函数不起作用,出了什么问题?
- c - 如何在 C 中转置 2D 矩阵?
- ruby-on-rails - 运行测试时的 ElasticSearch Rails resource_already_exists_exception
- java - 我可以使用 Inno Setup 为 Java 应用程序创建独立的 .exe 安装程序吗?
- java - 当我想将 Log4j 添加到依赖项时,Maven-plugin-plugin:descriptor 目标失败