Search code examples
pythonpandasplotly

Plot chart like heat map with values from one column and color from another column


I have the following dataset with many rows, multiple samples and 3 columns. I need to plot a graph which looks like a heatmap but it should fill the color not just for the position but also till previous position.

Name    position    category
Sample1 15500   1
Sample1 15800   2
Sample1 16200   2
Sample1 17200   3
Sample1 17400   3
Sample1 17700   3
Sample1 18300   2
Sample1 20010   2
Sample1 22120   1
Sample1 30000   3
Sample2 15880   1
Sample2 16200   1
Sample2 16900   3
Sample2 18200   3
Sample2 18500   2
Sample2 20400   1
Sample2 21300   2
Sample2 24800   3
Sample2 26000   1
Sample2 30000   3

I first converted this to a pivot table

import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
    
sample_pivot=sample.pivot_table(columns="position" , index= "Name", values="category")

Then i used plotly to chart this

fig = px.imshow(sample_pivot)
fig.update_xaxes(range=[1, 35000])
fig.show() 

I got the following chart

enter image description here

I need to modify above chart to fill the color in between the positions like below where the color filling will trace back to previous position and show up on the chart

Sample 1 : 
1-15500 --> Dark blue
15501-16200 --> orange red
16201-17700 --> yellow
17701-20010 --> orange red
20011-22120 --> Dark blue
22121-30000 --> yellow

Solution

  • You can do the following :

    • In order to fill the region from 1 to the next known position, insert the corresponding column
      sample_pivot.insert(0, 1, np.nan)
      
    • Fill missing/NaN values using pandas.DataFrame.bfill
      sample_pivot.bfill(axis=1, inplace=True)
      

    screenshot

    Note also that since the underlying trace used in this case is a plotly.graph_objects.Heatmap, you could use the parameter connectgaps, although it won't do exactly what you want, it might be interesting for you to see what it does.