MikkelC
MikkelC

Reputation: 291

Plotly: Problem with plotly express add_vrect to figure

I have a plotly express facet_row plot with three subplots that works nicely. I would like to add a box to all the the rows that colours a specific region of the plot and adds a text. The add_vrect should do the job, but I struggle to get it to work.

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

Code that works below

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()

I have tried to add:

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)

I get:

"AttributeError: 'Figure' object has no attribute 'add_vrect'"

Can anybody help or explain why it does not work..?

Upvotes: 3

Views: 4876

Answers (1)

vestland
vestland

Reputation: 61154

This seems to be a version problem. add_vrect() seems to have been introduced as a new functinality along with quite a few others in version 4.12.0 as of 23.10.2020:

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.

So my initial suggestion would be to update plotly and try again.

Upvotes: 2

Related Questions