简体   繁体   English

如何在 plotly 中的条形图上叠加条形图?

[英]How do I superimpose strip plots over bar charts in plotly?

What I would like to do: I have a set of data organized by genotype along a categorical x axis.我想做的事:我有一组按基因型沿分类 x 轴组织的数据。 I would like to plot the population means with associated error bars set as standard errors of the mean in a pretty standard bar chart.我想 plot 将人口平均值与相关误差条设置为漂亮标准条形图中平均值的标准误差。 I would also like to have, superimposed over my bar chart, each of the mean scores of the individual animals in my population as a strip plot (or scatter plot, I'm not picky).我还想在我的条形图上叠加我种群中个体动物的每个平均分数作为条形 plot(或分散 plot,我不挑剔)。 I can get this to work if I have a single set of named categorical axes on the x axis, but what I would really like to do is have two paired traces each attached to the x axes.如果我在 x 轴上有一组命名的分类轴,我可以让它工作,但我真正想做的是有两条成对的轨迹,每条轨迹都连接到 x 轴。

The dataframe looks like this: dataframe 看起来像这样:

>>> rotDF
          ID  Day   Genotype      Dose    Direction  Rotations
0     apple1    1        Del      Sal     Left       1
1     apple2    1         WT      Sal     Left       1
2     apple3    1         WT      Sal     Left       1
3     apple4    1        Del      Sal     Left       14
4    cherry1    1        Del      Sal     Left       3

Here's an example of the two things I want to combine.这是我想要结合的两件事的一个例子。 First, the ideal format of each trace of the bar graph would look like this:首先,条形图每条轨迹的理想格式如下所示:

First, I would like the bar charts to have their associated stripcharts superimposed over each of the bar traces.首先,我希望条形图将其关联的条形图叠加在每个条形迹线上。

Second, I would like each xaxis category to have the two color-coded traces organized around each ticklabel on the bottom, as in this bar chart.其次,我希望每个 xaxis 类别都围绕底部的每个刻度标签组织两个颜色编码的轨迹,如此条形图所示。

Here's what I currently have.这是我目前拥有的。 The bars look fine, but both sets of traces from the strip charts are clustered together at the middle, instead of being imposed over each of the bars for the respective trait.条形图看起来不错,但带状图中的两组轨迹都聚集在中间,而不是针对各自的特征强加在每个条形图上。 I have considered trying to color-code each data piece by genotype, which you can see in this version, but the dots cluster at the center rather than clustering alongside their assigned bar traces.我考虑过尝试按基因型对每个数据块进行颜色编码,您可以在此版本中看到,但点聚集在中心而不是聚集在分配的条形轨迹旁边。

And here's what I want to achieve.这就是我想要实现的目标。 This figure was created in inkscape using transparent copies of the strip plots superimposed on the relevant bar plots.该图是在 inkscape 中使用叠加在相关条形图上的条形图的透明副本创建的。 It's not ideal, but it worked for last conference--but I would really love to see something better.这并不理想,但它适用于上次会议——但我真的很想看到更好的东西。

The target result can be obtained by first creating a strip plot, then adding the bar chart:目标结果可以通过先创建条带plot,然后添加条形图:

import pandas as pd
import numpy as np

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

dose_lst = ['Sal', 'Amph 0.3', 'Amph 1', 'Amph 3']  # shortened for demonstration

# generating a dummy dataset
length = 100
data = {
    'ID': np.random.choice(['apple1', 'apple2', 'apple3', 'cherry1'], length),
    'Day': np.random.randint(1, 10, length),
    'Geno': np.random.choice(['DEL', 'WT'], length),
    'Dose': np.random.choice(dose_lst , length),
    'Rotations': np.random.randint(1, 20, length),
}
df = pd.DataFrame(data)

# some data preparation
# mean and std of 'Geno','Dose'-groups
df_WT = df.loc[df['Geno']=='WT']
df_WT = df_WT.groupby(['Geno','Dose'], as_index=False).agg({'Rotations':['mean', 'std']})
df_WT.sort_values(by='Dose', key=lambda column: column.map(lambda e: dose_lst.index(e)), inplace=True)
df_DEL = df.loc[df['Geno']=='DEL']
df_DEL = df_DEL.groupby(['Geno','Dose'], as_index=False).agg({'Rotations':['mean', 'std']})
df_DEL.sort_values(by='Dose', key=lambda column: column.map(lambda e: dose_lst.index(e)), inplace=True)
# mean of 'ID' column values (?)
df_mean = df.groupby(['ID', 'Geno','Dose'], as_index=False)[['Rotations']].mean()

# plot
fig = px.strip(
    data_frame=df,
    x='Dose',
    y='Rotations',
    category_orders={'Dose':['Sal', 'Amph 0.3', 'Amph 1', 'Amph 3'], 'Geno': ['WT', 'DEL']},
    color='Geno',
    color_discrete_map={'WT':'rgba(40,68,167,0.8)' ,'DEL':'rgba(166,184,248,0.8)'},
    orientation='v', 
    stripmode='group',
)
fig.add_trace(go.Bar(
    name='WT',
    x=dose_lst, y=df_WT.Rotations.values[:, 0],
    error_y=dict(type='data', array=df_WT.Rotations.values[:, 1]),
    marker={'color': 'rgba(40,68,167,0.8)'},
    showlegend=False
))
fig.add_trace(go.Bar(
    name='DEL',
    x=dose_lst, y=df_DEL.Rotations.values[:, 0],
    error_y=dict(type='data', array=df_DEL.Rotations.values[:, 1]),
    marker={'color': 'rgba(166,184,248,0.8)'},
    showlegend=False
))
fig.update_layout(title_text='Mean distance traveled per dose', title_x=0.5, template='simple_white')
fig.show()

结果是组合条形图和条形图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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