简体   繁体   English

如何使用plotly(python)中的按钮更新直方图nbins?

[英]How to update histogram nbins with buttons in plotly (python)?

I am having trouble updating the number of bins when updating the plot with buttons.使用按钮更新绘图时,我无法更新 bin 的数量。 This is an examplary short dataframe.这是一个示例性的短数据帧。 In the full version, there are a lot more rows so there is a point in using the initial nbins = 100. However, I would like to change the number of bins for each column when updating the histogram.在完整版本中,有更多的行,因此使用初始 nbins = 100 是有意义的。但是,我想在更新直方图时更改每列的 bin 数量。

dataset = pd.DataFrame(
    {'age': [19, 18, 28, 33, 32],
    'bmi': [27.9, 33.77, 33.0, 22.705, 28.88],
    'children': [0, 1, 3, 0, 0]}
)

fig = px.histogram(x = dataset['age'], nbins = 100)
fig.update_layout(xaxis_title = 'age')

buttons = []
for column in dataset.columns:
    buttons.append(
        dict(
            args = [
                {'x': [dataset[column]]},
                {'xaxis.title': column},
                
                # __________________________________
                # I TRIED THIS since fig.layout.figure.data[0].nbinsx = 100
                # {'figure.data[0].nbinsx': 5}
                # __________________________________

            ],
            label = column,
            method = 'update',
        )
    )

fig.update_layout(
    updatemenus = [
        dict(type = 'buttons', buttons = buttons,
             direction = 'right', x=1, y=1.15)
    ],
    title_text = 'Histograms'
)
fig.show()

This is how the histogram looks with the button options available.这是带有可用按钮选项的直方图的外观。

**When I change the column that the histogram is constructed for, the number of bins do not change. **当我更改构建直方图的列时,bin 的数量不会改变。 How do I fix this?我该如何解决? I tried to **我试过了 **

THIS IS THE IMAGE OF THE HISTOGRAM!这是直方图的图像! I HAVE YET TO EARN REPUTATION POINTS FOR IMAGE EMBEDDING TO BE POSSIBLE.我还没有为图像嵌入赢得声誉点数。

enter image description here在此处输入图片说明

  • you have not defined number of bins you want per column.您尚未定义每列所需的 bin 数量。 Have used col_bins = {c: int(dataset[c].max()-dataset[c].min()) for c in dataset.columns} to generate values from your data已使用col_bins = {c: int(dataset[c].max()-dataset[c].min()) for c in dataset.columns}从您的数据生成值
  • from a style perspective I use list and dict comprehensions to build updatemenus structures从风格的角度来看,我使用列表字典推导来构建更新菜单结构
  • your code is close, key part is understanding args of update method .您的代码很接近,关键部分是了解更新方法的参数 It's a list where first element is dict to update traces and second element is a dict to update layout.这是一个列表,其中第一个元素是字典内更新迹线和第二个元素是一个字典来更新布局。 There are two updates to traces: x and nbinsx so they are including in one dict跟踪有两个更新: xnbinsx,因此它们包含在一个字典中
import pandas as pd
import plotly.express as px

dataset = pd.DataFrame(
    {
        "age": [19, 18, 28, 33, 32],
        "bmi": [27.9, 33.77, 33.0, 22.705, 28.88],
        "children": [0, 1, 3, 0, 0],
    }
)
col_bins = {c: int(dataset[c].max()-dataset[c].min()) for c in dataset.columns}

fig = px.histogram(x=dataset["age"], nbins=100)
fig.update_layout(
    updatemenus=[
        {
            "buttons": [
                {
                    "label": c,
                    "method": "update",
                    "args": [{"x": [dataset[c]], "nbinsx":bins}, {"xaxis.title":c}],
                }
                for c, bins in col_bins.items()
            ],
            "direction": "right",
            "type":"buttons",
            "x":1,
            "y":1.15
        }
    ],
    xaxis_title="age",
    title_text = 'Histograms'
)

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

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