gpt4 book ai didi

python-3.x - 更改 hvplot.hist 的默认悬停数据

转载 作者:行者123 更新时间:2023-12-04 10:53:59 25 4
gpt4 key购买 nike

我有以下名为 df 的数据框包含 2 列:

In [4]: df.head(20)                                                                               
Out[4]:
age age_band
0 NaN NaN
1 61.0 55-64
2 NaN NaN
3 55.0 55-64
4 NaN NaN
5 67.0 65+
6 NaN NaN
7 20.0 18-24
8 53.0 45-54
9 NaN NaN
10 NaN NaN
11 23.0 18-24
12 60.0 55-64
13 NaN NaN
14 54.0 45-54
15 NaN NaN
16 67.0 65+
17 NaN NaN
18 50.0 45-54
19 70.0 65+

In [5]: df.info()                                                                                 
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 107632 entries, 0 to 107631
Data columns (total 2 columns):
age 73289 non-null float64
age_band 73289 non-null object
dtypes: float64(1), object(1)
memory usage: 1.6+ MB

In [7]: df["age_band"].value_counts()                                                             
Out[7]:
45-54 22461
55-64 17048
35-44 14582
65+ 12990
25-34 4078
18-24 2130
Name: age_band, dtype: int64

In [8]: df["age"].min()                                                                           
Out[8]: 19.0

In [9]: df["age"].max()
Out[9]: 74.0

目标:
我想为 df["age"] 绘制直方图使用 hvplot。在此图中,我想将年龄放入与我的 df["age_band"] 相对应的箱中。列值。下图执行此操作:

In [10]: df.hvplot.hist("age",bins=[18,25,35,45,55,65,74],xticks=[18,25,35,45,55,65,74],hover_cols
...: =["age_band"],line_width=4,line_color="w")

enter image description here

当您将鼠标悬停在每个垃圾箱上时,每个 age_band 的计数正确显示为 Count ,但是,而不是每个 age band值,它似乎显示平均值或中位数 age对于每个垃圾箱。

经过进一步调查,似乎设置 hover_cols="age_band"实际上对情节没有影响(如果省略,您将获得相同的情节。)

然后我尝试使用 HoverTool:

In [11]: from bokeh.models import HoverTool 
...:
...: hover = HoverTool(tooltips=df["age_band"].dropna())
...:
...: df.hvplot.hist("age",bins=[18,25,35,45,55,65,74],xticks=[18,25,35,45,55,65,74],line_width
...: =4,line_color="w").opts(tools=[hover])

但是我收到以下错误:

ValueError: expected an element of either String or List(Tuple(String, String)), got 1         55-64

然后我尝试了:

In [12]: from bokeh.models import HoverTool 
...:
...: hover = HoverTool(tooltips="age_band")
...:
...: df.hvplot.hist("age",bins=[18,25,35,45,55,65,74],xticks=[18,25,35,45,55,65,74],line_wi
...: dth=4,line_color="w").opts(tools=[hover])

这导致:

enter image description here

那么我也尝试过:

In [13]: hover = HoverTool(tooltips=[("18-24","2130"),("25-34","4078"),("35-44","14582"),("45-54",
...: "22461"),("55-64","17048"),("65+","12990")])
...:
...: df.hvplot.hist("age",bins=[18,25,35,45,55,65,74],xticks=[18,25,35,45,55,65,74],line_width
...: =4,line_color="w").opts(tools=[hover])

结果如下:

enter image description here

有没有办法生成 df["age"] 的直方图? ,使用 hvplot.hist,当您将鼠标悬停在垃圾箱上时,您会看到相应的 age_band & Countage_band ?

谢谢

最佳答案

设置 by=['age_band'] 应该可以工作,并且应该在您悬停时显示该列:

df.hvplot.hist(
y='age',
by=['age_band'],
legend=False,
color='lightblue',
bins=[18,25,35,45,55,65,74],
xticks=[18,25,35,45,55,65,74],
)

虽然在您描述的情况下,您也可以选择 在 value_counts 上创建条形图:
age_band_counts = df['age_band'].value_counts().sort_index()

age_band_counts.hvplot.bar(bar_width=1.0)

关于python-3.x - 更改 hvplot.hist 的默认悬停数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59326844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com