如何使用 plotly (python) 中的按钮更新直方图 nbins?
How to update histogram nbins with buttons in plotly (python)?
我在使用按钮更新绘图时遇到了更新 bin 数量的问题。
这是一个示例性的短数据框。在完整版本中,有更多的行,因此使用初始 nbins = 100 是有必要的。
但是,我想在更新直方图时更改每列的分箱数。
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()
这是使用可用按钮选项时直方图的外观。
**当我更改为其构造直方图的列时,箱数不会改变。我该如何解决?我试着 **
这是直方图的图像!我还没有为可能的图像嵌入赢得声誉点数。
enter image description here
- 您尚未定义每列所需的 bin 数。已使用
col_bins = {c: int(dataset[c].max()-dataset[c].min()) for c in dataset.columns}
从您的数据中生成值
- 从风格的角度来看,我使用 list 和 dict 理解来构建 updatemenus 结构
- 你的代码很接近,关键部分是理解 args of update method。这是一个 list,其中第一个元素是 dict 来更新轨迹,第二个元素是 dict 来更新布局.跟踪有两个更新:x 和 nbinsx 所以它们包含在一个 dict[=28 中=]
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'
)
我在使用按钮更新绘图时遇到了更新 bin 数量的问题。 这是一个示例性的短数据框。在完整版本中,有更多的行,因此使用初始 nbins = 100 是有必要的。 但是,我想在更新直方图时更改每列的分箱数。
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()
这是使用可用按钮选项时直方图的外观。
**当我更改为其构造直方图的列时,箱数不会改变。我该如何解决?我试着 **
这是直方图的图像!我还没有为可能的图像嵌入赢得声誉点数。
enter image description here
- 您尚未定义每列所需的 bin 数。已使用
col_bins = {c: int(dataset[c].max()-dataset[c].min()) for c in dataset.columns}
从您的数据中生成值 - 从风格的角度来看,我使用 list 和 dict 理解来构建 updatemenus 结构
- 你的代码很接近,关键部分是理解 args of update method。这是一个 list,其中第一个元素是 dict 来更新轨迹,第二个元素是 dict 来更新布局.跟踪有两个更新:x 和 nbinsx 所以它们包含在一个 dict[=28 中=]
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'
)