英文:
Plot chart like heat map with values from one column and color from another column
问题
我有以下包含许多行、多个样本和3列的数据集。我需要绘制一个图表,它看起来像一个热图,但它不仅要填充颜色到当前位置,还要填充到前一个位置。
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
我首先将其转换为一个数据透视表:
import pandas as pd
sample_pivot = sample.pivot_table(columns="position", index="Name", values="category")
然后我使用plotly来绘制这个图表:
import plotly.express as px
fig = px.imshow(sample_pivot)
fig.update_xaxes(range=[1, 35000])
fig.show()
我得到了以下图表:
我需要修改上面的图表,以在位置之间填充颜色,如下所示,颜色填充将追溯到前一个位置并显示在图表上:
Sample 1 :
1-15500 --> 深蓝色
15501-16200 --> 橙红色
16201-17700 --> 黄色
17701-20010 --> 橙红色
20011-22120 --> 深蓝色
22121-30000 --> 黄色
英文:
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
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
答案1
得分: 0
一种我考虑用来生成我想要的内容的方法是以100为增量的数字,如100、200、300、400……直到30,000,然后用NaN填充空白,执行外连接以获取所有位置,包括增加的位置,并使用.bfill()方法填充并创建上面的图表。对此或任何更好的方法有什么建议吗?
英文:
One way i was thinking to generate what i wanted to get above is to have numbers in increments of 100 like 100,200,300,400 ... till 30,000 and fill blanks with Nan, perform outer join so that i have all positions including the incremented positions and use .bfill() method to fill and create above graph. Any suggestions on this or any better approach ?
答案2
得分: 0
以下是已翻译的内容:
-
为了填充从1到下一个已知位置的区域,请插入相应的列
sample_pivot.insert(0, 1, np.nan)
-
使用
pandas.DataFrame.bfill
来填充缺失/NaN值sample_pivot.bfill(axis=1, inplace=True)
还请注意,由于在此情况下使用的底层跟踪是 plotly.graph_objects.Heatmap
,您可以使用参数 connectgaps
,尽管它不会完全符合您的要求,但您可能会对它的功能感兴趣。
英文:
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)
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论