简体   繁体   中英

How to make the rectangle plot with variable width based on color in plotly?

I am new to the plotly & I have to plot a rectangle plot. My data looks like this:

df:
start  end color
1       5   blue
6       50  grey
51      56  red
57      60  blue
61      105 grey
106     111 red

For every row, I need to create a rectangle plot with color blocks from the start column to the end column of every row. But when the value in the color column is grey, I want to make the plot with a fixed value of 5 and want to continue the next color after the end of the grey color. After the grey color, I am not able to continue the plot from the next number after the end of the grey color. In short, I want to a make rectangle plot continuous.

This is my current code:

import plotly.graph_objects as go
fig = go.Figure()
start = min(df['start'])
end = max(df['end'])

fig.update_xaxes(range=[start-100, end+100], showgrid=False)
fig.update_yaxes(range=[0, 2])
for row in df.index:
    color_rect = df['color'][row]
    if color_rect == 'grey':
        x_start = df['start'][row]
        x_stop = df['start'][row]+50
        fig.add_shape(type="rect",x0=x_start, y0=0.5, x1=x_stop, y1=1.5,
                line=dict(color=color_rect,),fillcolor=color_rect,)
        x_cont=x_stop+1
    else:
        x_start = df['start'][row]
        x_stop = df['end'][row]
        fig.add_shape(type="rect",x0=x_start, y0=0.5, x1=x_start, y1=1.5,
            line=dict(color=color_rect,),fillcolor=color_rect,)
fig.update_shapes(dict(xref='x', yref='y'))
fig.show()

This is what my current plot looks like: 在此处输入图像描述

I want to avoid the gaps after the grey color. Kindly help with this!

There are few errors in the code.

  • You say that the width should be 5 if the color is gray, but your code has 50 instead of 5
  • x_cont needs to be set to 0 initially and then used as the start point for the rectangle. Width should be 5 in case of gray, else df.end - df.start
  • For non-gray, x0 and x1 are both mentioned as x_start
  • In both cases (gray or not), you need to increment x_cont

The updated code and plot are below. Data is as in your question. Hope this is what you are looking for...

import plotly.graph_objects as go
fig = go.Figure()
start = min(df['start'])
end = max(df['end'])
x_cont = 0  ## Initialize x_cont to zero
fig.update_xaxes(range=[start-100, end+100], showgrid=False)
fig.update_yaxes(range=[0, 2])
for row in df.index:
    color_rect = df['color'][row]
    if color_rect == 'grey':
        x_start = x_cont ## Using x_cont instead of df.start
        x_stop = x_cont+5
        fig.add_shape(type="rect",x0=x_start, y0=0.5, x1=x_stop, y1=1.5,
                line=dict(color=color_rect,),fillcolor=color_rect,)
    else:
        x_start = x_cont ## Using x_cont instead of df.start
        x_stop = x_cont + df['end'][row] - df['start'][row] ## x_end is x_cont+width
        fig.add_shape(type="rect",x0=x_start, y0=0.5, x1=x_stop, y1=1.5,
            line=dict(color=color_rect,),fillcolor=color_rect,)
    x_cont=x_stop+1  ## Note that x_cont is updated in each loop of for loop
        
fig.update_shapes(dict(xref='x', yref='y'))
fig.show()

Plot在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM