散景:在 x 轴上带有标签的多行

bokeh: multiline with label in x axis

我创建了一个多线图来跟踪 CPU 机器一周又一周的消耗:

但我想在 x 轴图例中包含年份,如下图所示:

当我尝试通过字符串值更改索引值 (47, 48..., 51) 时,我得到一个空白图表。是否可以在多线图表的 x 轴上显示字符串标签值?

这是我的代码:

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)

我的文件“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

有两种选择,您可以如何实现此目标:

  1. 使用p.xaxis.major_label_overrides

这是非常基础的。您只需定义一个包含位置和标签的字典。 在您的示例中,这可能是:

from io import StringIO
import pandas as pd

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource
output_notebook()

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
"""
data = pd.read_csv(StringIO(data_csv), sep=';')
startdate_dayweek = '2021-' + data.startdate_dayweek.astype(str)
data.drop('startdate_dayweek', axis=1, inplace=True)
     
df = {'semaine': [data.index]*len(data.columns), 
      'jour': ['Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi', 'Dimanche'], 
      'color': ['red', 'orange', 'yellow', 'green', 'grey', 'pink', 'purple'],
      'HCPU': data.values.T}

source = ColumnDataSource(df)
p = figure(width=800, height=500)
p.multi_line(xs='semaine', ys='HCPU', legend_group='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.left[0].formatter.use_scientific = False
p.xaxis.major_label_overrides = {i: val for i, val in enumerate(startdate_dayweek)}
show(p)
  1. 使用p = figure(x_axis_type='datetime')和一个DatetimeTickFormatter

这更清晰,因为您使用的是日期,而散景确实支持日期。首先将您的索引转换为 datetime-object,我使用 %Y-%W-%w 作为解决方法。 解释了为什么我需要这个。然后定义你想要的格式化程序,在你的情况下 %Y-%W。 在您的示例中,这可能是:

from io import StringIO
import pandas as pd

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource, DatetimeTickFormatter
output_notebook()

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
"""
data = pd.read_csv(StringIO(data_csv), sep=';')
data.startdate_dayweek = '2021-' + data.startdate_dayweek.astype(str) + '-0'
data.index = pd.to_datetime(data.startdate_dayweek, format='%Y-%W-%w')
data.drop('startdate_dayweek', axis=1, inplace=True)
     
df = {'semaine': [data.index]*len(data.columns), 
      'jour': ['Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi', 'Dimanche'], 
      'color': ['red', 'orange', 'yellow', 'green', 'grey', 'pink', 'purple'],
      'HCPU': data.values.T}

source = ColumnDataSource(df)
p = figure(width=800, height=500, x_axis_type='datetime')
p.multi_line(xs='semaine', ys='HCPU', legend_group='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.left[0].formatter.use_scientific = False
p.xaxis.formatter.days = ['%Y-%W']
show(p)

两次输出看起来像这样: