[英]bokeh: multiline with label in x axis
I created a multiline chart which tracks the CPU consumption of a machine week after week:我创建了一个多线图表,每周跟踪机器的 CPU 消耗:
But I want to include year in the x axis legend, like in this image:但我想在 x 轴图例中包含年份,如下图所示:
When I try to change index values (47, 48..., 51) by string values, I have a blank graph.当我尝试通过字符串值更改索引值(47、48...、51)时,我有一个空白图。 Is it possible to show string label values in x axis for multiline chart?
是否可以在多线图的 x 轴上显示字符串 label 值?
This is my code:这是我的代码:
import pandas as pd
from bokeh.plotting import figure, show, output_file
from bokeh.models import ColumnDataSource
output_file('temp.html')
data = pd.read_csv("data.csv")
data.index = ['2021-51', '2021-52', '2022-1', '2022-2', '2022-2']
cpu_values_daily = data.values.T.tolist()
weeks = []
for i in range(0,len(data.columns)):
weeks.append(data.index)
df = {'semaine': weeks,
'jour': ['Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi', 'Dimanche'],
'color': ['red', 'orange', 'yellow', 'green', 'grey', 'pink', 'purple'],
'HCPU': cpu_values_daily}
source = ColumnDataSource(df)
p = figure(width=800, height=500)
p.multi_line(xs='semaine', ys='HCPU', legend='jour', color='color',
line_width=5, line_alpha=0.6, hover_line_alpha=1.0,
muted_color='color', muted_alpha=0.2,
source=source)
p.xaxis.visible = False
p.left[0].formatter.use_scientific = False
show(p)
And my file "data.csv":我的文件“data.csv”:
startdate_dayweek;1;2;3;4;5;6;7
47;150290;345005;343329;351631;368029;322604;615009
48;249414;381473;385862;376488;367117;342397;494052
49;236236;395367;499916;392677;372029;377518;518521
50;223065;347776;434387;372996;378691;385578;645206
51;190055;358690;354985;413861;414002;470053;525458
There are two options, how you can achive this goal:有两种选择,您可以如何实现此目标:
This is very basic.这是非常基本的。 You just define a dictionary with the position and the label.
您只需使用 position 和 label 定义一个字典。 In your example this could be:
在您的示例中,这可能是:
data = pd.read_csv("data.csv")
data.startdate_dayweek = '2021-' + data.startdate_dayweek.astype(str)
# some other code
p.xaxis.major_label_overrides = {i: val for i, val in enumerate(data.startdate_dayweek)}
show(p)
p = figure(x_axis_type='datetime')
and a DatetimeTickFormatter
p = figure(x_axis_type='datetime')
和DatetimeTickFormatter
This is cleaner, because you are working with dates and bokeh does support dates.这更干净,因为您正在使用日期,而散景确实支持日期。 First convert your index to a datetime-object, I used
%Y-%W-%w
as a workaround.首先将您的索引转换为日期时间对象,我使用
%Y-%W-%w
作为解决方法。 Why I need this is explained here .为什么我需要这个在这里解释。 Then define your wanted Formatter, in your case
%Y-%W
.然后定义您想要的格式化程序,在您的情况下
%Y-%W
。 In your example this could be:在您的示例中,这可能是:
from bokeh.models import DatetimeTickFormatter
# other imports
data = pd.read_csv("data.csv")
data.startdate_dayweek = '2021-' + data.startdate_dayweek.astype(str) + '-0'
data.index = pd.to_datetime(data.startdate_dayweek, format='%Y-%W-%w')
# some other code
p = figure(width=800, height=500, x_axis_type='datetime')
# some other code
p.xaxis.formatter.days = ['%Y-%W']
show(p)
Both times the poutput looks like this:两次 poutput 都如下所示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.