繁体   English   中英

更改 hvplot.hist 的默认悬停数据

[英]Change Default Hover Data for hvplot.hist

我有以下名为df数据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

目的:我想使用 hvplot 为df["age"]绘制直方图。 在此图中,我想将年龄放入与我的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")

在此处输入图片说明

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

经过进一步调查,似乎设置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]) 

这导致:

在此处输入图片说明

那么我也尝试过:

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]) 

结果如下:

在此处输入图片说明

有没有办法使用 hvplot.hist 生成df["age"]的直方图,当您将鼠标悬停在一个 bin 上时,您会看到相应的age_band & Count of age_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)

暂无
暂无

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

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