我对您问题的理解是您希望获得直方图中显示的准确间隔和计数。对于较小的子集px.data.tips()
, this:
从图表中读出这些值将是:
counts = [2, 4, 3, 1]
bins = [5, 15, 25, 35, 45]
没有direct方法来做到这一点,但这并不意味着这是不可能的。至少如果你愿意使用很棒的fig.full_figure_for_development()
and a little numpy.
代码亮点(完整片段在最后)
xbins = f.data[0].xbins
plotbins = list(np.arange(start=xbins['start'], stop=xbins['end']+xbins['size'], step=xbins['size']))
counts, bins = np.histogram(list(f.data[0].x), bins=plotbins)
Output:
[2 4 3 1] [ 5 15 25 35 45]
所有细节:
我猜你会的like能够做到的是:
Run:
fig.data[0].count
And get:
[2, 4, 3, 1]
但你会得到的最接近的是:
Run:
fig.data[0].x
And get:
[15.53, 10.07, 12.6 , 32.83, 35.83, 29.03, 27.18, 22.67, 17.82,
18.78]
这些只是输入的原始值df['total_bill'].tail(10)
。所以 DerekO 是对的,其余的由 javascript 处理。但fig.full_figure_for_development() will:
[...]返回一个新的 go.Figure 对象,预填充了相同的值
您提供的,以及 Plotly.js 计算的所有默认值,
让您详细了解哪些属性控制每个细节
您的身材以及如何定制它们。
所以跑步f = fig.full_figure_for_development(warn=False)
, 进而:
f.data[0].xbins
会给你:
histogram.XBins({
'end': 45, 'size': 10, 'start': 5
})
现在你已经知道了足够的知识,可以用一点 numpy 在你的图中获得相同的值:
完整代码:
import plotly.express as px
import numpy as np
df = px.data.tips()
df = df.tail(10)
fig = px.histogram(df, x="total_bill")
f = fig.full_figure_for_development(warn=False)
xbins = f.data[0].xbins
plotbins = list(np.arange(start=xbins['start'], stop=xbins['end']+xbins['size'], step=xbins['size']))
counts, bins = np.histogram(list(f.data[0].x), bins=plotbins)
print(counts, bins)