Plotly:Plotly express添加到图形的问题

2024-06-16 17:20:37 发布

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

我有一个plotly express facet_row图,其中有三个子图,效果很好。我想在所有行中添加一个框,为绘图的特定区域着色,并添加一个文本。附件应该能完成这项工作,但我很难让它工作

https://plotly.com/python/horizontal-vertical-shapes/https://plotly.com/python/facet-plots/

在下面工作的代码

import pandas as pd
import plotly.express as px
import plotly.io as pio

ftir_data_facet = pd.read_csv(r"C:\Users\mch047\Box Sync\PhD project Mikkel C\OneDrive - UiT Office 365\Mikkel_Christensen\Bioinformatics\Paper 1\Figure 2 Plate screen panel\Full_FTIR_dataset\Paper1_Figure2_FTIR_tidy_faceformat_071120.csv", sep=";")
#ftir_data_facet

figure2_facet = px.scatter(ftir_data_facet, x="Wavenumbers (1/cm)", y="Absorbance", color="Strain", facet_row="Plate")

figure2_facet.update_xaxes(title_text="Wavenumbers (1/cm)", range=[4000, 1000], showgrid=False)
fig.show()

我试图补充:

figure2_facet.add_vrect(x0="1750", x1="1700", row=All, 
             annotation_text="Carbonyl region", annotation_position="top left",
             fillcolor="green", opacity=0.25, line_width=0)

我得到:

“AttributeError:'Figure'对象没有属性'add\u vrect'”

有人能帮助或解释为什么它不起作用吗


Tags: csvhttpsimportcomdataasplotlyfacet
1条回答
网友
1楼 · 发布于 2024-06-16 17:20:37

这似乎是一个版本问题^截至2020年10月23日,版本add_vrect()中的new functinality似乎与许多其他版本一起作为4.12.0引入:

Added

Shapes that reference the plot axes in one dimension and the data in another dimension can be added with the new add_hline, add_vline, add_hrect, add_vrect functions, which also support the row="all" and col="all" arguments. (#2840) The add_trace, add_shape, add_annotation, add_layout_image, add_hline, add_vline, add_hrect, add_vrect functions accept an argument exclude_empty_subplots which if True, only adds the object to subplots already containing traces or layout objects. This is useful in conjunction with the row="all" and col="all" arguments.

所以我最初的建议是有计划地更新,然后再试一次

相关问题 更多 >