绘制点图 - 如何对多列进行分组

问题描述 投票:0回答:1

我觉得我要疯狂地试图解决这个问题。假设我有这个数据框,如何创建类似于下图的点图? (只有点,没有条)

探针组ID 骨髓 骨髓 肾脏 肾脏
AFFX-r2-Bs-dap-3_at 120 250 221 150 332 242 400 130 432

如果列具有相同的名称,请注意点如何位于同一 x 轴

Ideal graph here

我尝试了点图,但无法以某种方式将列分组在一起。

这是我的代码

import plotly.express as px
import plotly.graph_objects as go
  
# initialize list of lists 
data = [['ajfhf',120 , 250, 221 , 150 , 332, 242, 400, 130, 432]] 
  
# Create the pandas DataFrame 
df = pd.DataFrame(data, columns=['ProbeID', 'bone marrow' , 'bone marrow' ,'kidney' , 'kidney' ,'heart', 'heart' ,' heart' , 'lungs' ,'spleen']) 
cols = df.columns[1:]
print(cols)
fig = go.Figure()  
for col in cols:
    fig.add_trace(go.Scatter(
        y=df[col],
        name=col
    ))


fig.update_traces(marker_size=10)
fig.show()

这就是它的样子。请注意它们如何全部堆叠在一列上,并且缺少一些点。在 go.Scatter 部分,我不得不省略 x 轴,因为我不确定在那里使用什么。

What I have

plotly plotly-dash plotly-python
1个回答
0
投票

由于x轴上的类别变量是重复的,如果简单指定列名,则不会提取重复项。此外,x 轴未正确绘制,因为未指定它。 x 轴是重复的,因此通过索引指定,并且 x 轴的文本稍后更新。

import plotly.express as px
import plotly.graph_objects as go
import pandas as pd

# initialize list of lists 
data = [['ajfhf',120 , 250, 221 , 150 , 332, 242, 400, 130, 432]] 
  
# Create the pandas DataFrame 
df = pd.DataFrame(data, columns=['ProbeID', 'bone marrow' , 'bone marrow' ,'kidney' , 'kidney' ,'heart', 'heart' ,' heart' , 'lungs' ,'spleen']) 
cols = df.columns[1:]

fig = go.Figure() 
for i,col in enumerate(cols, start=1):
    fig.add_trace(go.Scatter(
        x=[i],
        y=df.iloc[:,i],
        name=col,
        marker=dict(size=10),
        ))
fig.update_xaxes(tickvals=[1,2,3,4,5,6,7,8,9], ticktext=cols.tolist())
fig.show()

© www.soinside.com 2019 - 2024. All rights reserved.