0

So I am trying to plot n circle shapes in a graph with Plotly. This number n can vary and that's why I cannot create them manually but I need a loop. Is there anyway to do that? to replicate n times this piece of code according to my list indexes?:

  'shapes': [
     # Circle to define radius_of_persistence
    {
        'type': 'circle',
        'xref': 'x',
        'yref': 'y',
        'x0': zois_list[0]-radius_of_persistence,
        'y0': zois_list[1]-radius_of_persistence,
        'x1': zois_list[0]+radius_of_persistence,
        'y1': zois_list[1]+radius_of_persistence,
        'line': {
            'color': 'rgba(255, 171, 96, 1)',
        },
    },
4
  • What have you tried so far to solve the problem? The code snippet you gave is not even valid Python on its own. Commented Sep 11, 2018 at 11:01
  • The code is not complete, but it is valid. It is a piece of plotly code scheme to plot a circle. So far, I have just added them manually, adding a for loop does not work inside this kind of plotly schemes and I didn't find any post doing the same thing. Commented Sep 11, 2018 at 11:05
  • could you provide us a piece of code that we could run on our devices to test before answering? This would help a lot the people trying to help you. Commented Sep 12, 2018 at 15:20
  • Thanks, I already have the answer. Commented Sep 14, 2018 at 7:24

2 Answers 2

1

I created the list of shapes in a loop before updating the layout of the figure, like:

shapes = []
    for ind_begin, ind_end in zip(ind_begins, ind_ends):
        shapes.append(go.layout.Shape(
                    type="rect",
                    xref="x",
                    yref="paper",
                    x0=df.iloc[:, 0][ind_begin],
                    y0=0,
                    x1=df.iloc[:, 0][ind_end],
                    y1=1,
                    fillcolor="LightSalmon",
                    opacity=0.5,
                    layer="below",
                    line_width=0,
                ))
    fig.update_layout(
            shapes=shapes
        )
Sign up to request clarification or add additional context in comments.

Comments

0

So, basically the only way that I found to create shapes in a loop is to create the json object that represents the "layout" myself. In this way I can include as many entries in the dictionary (layout) as I want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.