简体   繁体   English

如何设置条形图悬停以显示 x 轴的标签?

[英]How to set bar-chart hover to show x axis' labels?

I have generated a simple bar chart.我生成了一个简单的条形图。 In order to make it more interactive, I have added Hovertool into the graph.为了使其更具交互性,我在图表中添加了 Hovertool。

from bokeh.io import show, output_notebook
from bokeh.plotting import figure, output_file
from bokeh.models.glyphs import HBar
from bokeh.models import ColumnDataSource, Legend, HoverTool
output_notebook()

# Set x and y
functions = ['func_1', 'func_2', 'func_3']
percentage = [233.14, 312.03, 234.00]

# Set data source (color needs to be set precisely with len(category))
source = ColumnDataSource(data=dict(functions=functions, percentage=percentage))

# Set the x_range to the list of categories above
p = figure(x_range=functions, plot_height=600, plot_width=800, title="The Overall Use of my functions",
          x_axis_label='Functions', y_axis_label='Percentage')

# Categorical values can also be used as coordinates
p.vbar(x='functions', top='percentage', width=0.9, source=source)
p.add_tools(HoverTool(tooltips=[('Percentage', "@percentage")]))

show(p)

Although it shows a value from y axis correctly, I would like to display a label from x axis instead (eg, Func1: 9.45).虽然它正确显示了 y 轴的值,但我想显示 x 轴的标签(例如,Func1:9.45)。 Just like the picture that is shown from the link (I cannot post an image yet):就像链接中显示的图片(我还不能发布图片):

https://i.ibb.co/235jR39/Untitled.png https://i.ibb.co/235jR39/Untitled.png


Update#1 I tried to come up with something, this is what I got:更新#1我试图想出一些东西,这就是我得到的:

# Set Hover
percentage = list(map(lambda i: str(i), percentage))
my_hover = list(zip(functions, percentage))
p.add_tools(HoverTool(tooltips=my_hover))

It turns out it shows every detail in every bar as shown below事实证明它显示了每个条中的每个细节,如下所示

https://i.ibb.co/72hmD8q/Untitled-2.png https://i.ibb.co/72hmD8q/Untitled-2.png

This code works for Bokeh v1.0.4.此代码适用于 Bokeh v1.0.4。

import math
import numpy as np
import pandas as pd
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.plotting import figure, show
from bokeh.palettes import Category10

df = pd.DataFrame(data = np.random.rand(10, 1), columns = ['percentage'], index = ['Func {}'.format(nmb) for nmb in range(10)])
df['color'] = Category10[10]

source = ColumnDataSource(data = dict(functions = df.index.values, percentage = df['percentage'].values, color = df['color'].values))

p = figure(x_range = df.index.values, plot_height = 600, plot_width = 800, title = "The Overall Use of my functions",
           x_axis_label = 'functions', y_axis_label = 'percentage')

p.vbar(x = 'functions', top = 'percentage', width = 0.9, color = 'color', source = source)
p.add_tools(HoverTool(tooltips = '<font color=blue>@functions:</font><font color=red> @percentage</font>'))

p.xgrid.grid_line_color = None
p.xaxis.major_label_orientation = math.pi / 4  # Rotate axis' labels

show(p)

Result:结果:

在此处输入图片说明

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

相关问题 如何用pyplot在相同的x轴(日期时间)但不同的y轴上绘制线形图和条形图? - How to plot line and bar-chart on the same x-axis (datetime) but different y-axis with pyplot? 如何在所有条形图子图上显示x轴标签? - How to display x axis labels on all bar chart subplots? 如何在 python 条形图中添加 x 轴刻度标签 - How to add x-axis tick labels in python bar chart Matplotlib 不会用条形图显示所有刻度/标签。 它跳过 y 轴的第一个和 x 轴的最后一个 - Matplotlib don't show all ticks/labels with bar chart. It skips first of y axis and last one of x axis Python Pandas堆叠条形图x轴标签 - Python Pandas Stacked Bar Chart x-axis labels Matplotlib 条形图 X 轴标签顺序 - Matplotlib bar chart X-axis Labels order 当我使用matplotlib时,如何解决我的演出编号与条形图重叠的问题? - How can I solve my show number overlap my bar-chart when I using matplotlib? 如何旋转Matplotlib条形图上的X轴刻度标签? 尝试了几种方法,都没有用 - How to rotate x-axis tick labels on matplotlib bar chart? Tried several approaches, none worked 如何更改 Matplotlib 条形图上 x 轴标签上显示的单位 - How do I change the units shown on the x-axis labels on a Matplotlib bar chart 如何在 matplotllib 条形图的 x 轴上添加第二行标签 - How can I add a second row of labels to the x axis of a matplotllib bar chart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM