简体   繁体   English

Plotly:如何创建包含下拉菜单的多面条形图?

[英]Plotly: How to create faceted bar plots including a dropdown menu?

I have the following dataframe我有以下数据框

import pandas as pd
df = pd.DataFrame({'var1':[0.2, 0.3, 0.4, 0.5, 0.6], 'var2': [1.2, 1.3, 1.4, 1.5, 1.6],
                   'facet_var': ['sd_1', 'sd_1', 'ctrl_1', 'ctrl_1', 'ctrl_1'],
                   'value_facet': ['a', 'b', 'a','b','c']})

I would like to create an html with barplots of var1 and var2 (selected from a dropdown menu) faceted by facet_var .我想创建一个带有var1var2 (从下拉菜单中选择)的facet_varhtml其中的barplotsfacet_var分面。 The x_values of the barplots would be the value_facet and the height of the barplots would be the var1 or var2 depending on the selection from the dropdown menux_values的barplots的将是value_facetheight的的barplots将是var1var2取决于选择从下拉菜单

Any ideas how to do something like that using plotly ?任何想法如何使用plotly做类似的事情?

What you're asking is most likely fully doable, but there are more than a few confusing elements in your question.您所问的问题很可能是完全可行的,但是您的问题中存在许多令人困惑的元素。 The question title says Histogram , but you're talking about barplots in the question itself.问题标题说的是Histogram ,但您在问题本身中谈论的是barplots So no binning then?那么没有分档呢? Further, the term facets lends iself best to plotly.express (px) .此外,术语facets最适合plotly.express (px) And px works best with data of a long format. px最适用于长格式的数据。 And you've provided a dataset that's more of a wide format since you've got columns with values of a single categeoy for var1 and var2 :并且您提供了一个更宽格式的数据集,因为您有包含var1var2单个类别值的列:

    var1    var2    facet_var   value_facet
0   0.2     1.2     sd_1        a
1   0.3     1.3     sd_1        b
2   0.4     1.4     ctrl_1      a
3   0.5     1.5     ctrl_1      b
4   0.6     1.6     ctrl_1      c

And that's a format that's normally better suited for plotly.graph_objects (go) .这是一种通常更适合plotly.graph_objects (go)的格式。 And in that case we'd be more likely talking about subplots and not facets .在这种情况下,我们更有可能谈论subplots而不是facets And one more thing.还有一件事情。 The number of dimensions in your dataset doesn't really justify a faceted bar plot.数据集中的维数并不能真正证明多面条形图的合理性。 Meaning that all dimensions of your dataset would be fully visualized using a grouped and faceted bar plot like this px.bar(df, x='value_facet', y='value', color='variable', facet_col='facet_var') :这意味着您的数据集的所有维度都将使用像这样的分组和分面条形图完全可视化px.bar(df, x='value_facet', y='value', color='variable', facet_col='facet_var')

Plot 1: All data dimensions, but no drop-down图 1:所有数据维度,但没有下拉列表

在此处输入图片说明

But to answer your question, assuming that you'd not like to use a histogram (with binned values) but a barplot with a dropdown menu , the code snippet below will produce the following plots for options var1 and var2 in the dropdown menu:但是为了回答您的问题,假设您不想使用直方图(带有分箱值)而是使用带有下拉菜单条形图,下面的代码片段将为下拉菜单中的选项var1var2生成以下图:

Plot 2: var1图 2: var1

在此处输入图片说明

Plot 3: var2图 3: var2

在此处输入图片说明

One particulary important line in the complete code snippet below is:下面完整代码片段中特别重要的一行是:

df=pd.melt(df, id_vars=['facet_var', 'value_facet'], value_vars=['var1', 'var2'])

This turns the dataset from the semi-wide format you've provided into data of a long format that's better suited for px :这会将数据集从您提供的半宽格式转换为更适合px的长格式数据:

    facet_var   value_facet variable    value
0   sd_1        a           var1    0.2
1   sd_1        b           var1    0.3
2   ctrl_1      a           var1    0.4
3   ctrl_1      b           var1    0.5
4   ctrl_1      c           var1    0.6
5   sd_1        a           var2    1.2
6   sd_1        b           var2    1.3
7   ctrl_1      a           var2    1.4
8   ctrl_1      b           var2    1.5
9   ctrl_1      c           var2    1.6

Complete code:完整代码:

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

df = pd.DataFrame({'var1':[0.2, 0.3, 0.4, 0.5, 0.6], 'var2': [1.2, 1.3, 1.4, 1.5, 1.6],
                   'facet_var': ['sd_1', 'sd_1', 'ctrl_1', 'ctrl_1', 'ctrl_1'],
                   'value_facet': ['a', 'b', 'a','b','c']})

df=pd.melt(df, id_vars=['facet_var', 'value_facet'], value_vars=['var1', 'var2'])

df1=df[df['variable']=='var1']
df2=df[df['variable']=='var2']

### all dimensions, no drop-downs  ###################################################
# PLot 1
#fig = px.bar(df, x='value_facet', y='value', color='variable', facet_col='facet_var')
#fig.show()
######################################################################################

# Figure with dropdown menu to produce plot 2 and plot 3
fig = px.bar(df1, x='value_facet', y='value', color='variable', facet_col='facet_var')


updatemenus = [
    {
        'buttons': [
            {
                'method': 'update',
                'label': 'var 1',
                'args': [
                    {'y': [df1[(df1['facet_var'] == 'sd_1')]['value'],
                           df1[(df1['facet_var'] == 'ctrl_1')]['value']]},
                ]
            },
            {
                'method': 'update',
                'label': 'var 2',
                'args': [
                    {'y': [df2[(df2['facet_var'] == 'sd_1')]['value'],
                           df2[(df2['facet_var'] == 'ctrl_1')]['value']]},
                ]
            }
        ],
        'direction': 'down',
        'showactive': True,
    }
]

fig.update_layout(updatemenus=updatemenus)
fig.show()

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

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