I want to make multiple Plotly Scatter plots (one for each column) in a df using a for loop in Python, which I get to work as expected.
Then I want the color of the plots to be conditional of the last value in the dataframe. Eg. if the last row is above or below 5, so in the example I want the plot for column B to have a differrent color than A and C.
import plotly.express as px
import pandas as pd
df = pd.DataFrame({'A': [3, 1, 2, 3],
'B': [5, 6, 7, 8],
'C': [2, 1, 6, 3]})
df
A B C
0 3 5 2
1 1 6 1
2 2 7 6
3 3 8 3
making plots with Plotly and for loop:
plots = {}
for i in df.columns:
if df.iloc[-1] >= 5:
plots[i] = px.scatter(df, color_discrete_sequence=["red"], x="A", y=i)
else:
plots[i] = px.scatter(df, color_discrete_sequence=["blue"], x="A", y=i)
Here I expect to be able to show each plot with plots['A'], plots['B'] and plots['C'], but I expect plot A and C to be blue, and plot B to be red.
