简体   繁体   English

散景多线图

[英]Bokeh multiline plot

I am trying to plot RPI, CPI and CPIH on one chart with a HoverTool showing the value of each when you pan over a given area of the chart. 我正在尝试在一张图表上绘制RPI,CPI和CPIH,并使用HoverTool显示当您在图表的给定区域上平移时的值。

I initially tried adding each line separately using line() which kind of worked: 最初,我尝试使用line()分别添加每行,这种方式有效:

在此处输入图片说明

However, the HoverTool only works correctly when you scroll over the individual lines. 但是,仅当您在各行上滚动时, HoverTool才能正常工作。

I have tried using multi_line() like: 我试过像这样使用multi_line()

combined_inflation_metrics = 'combined_inflation_metrics.csv'
df_combined_inflation_metrics = pd.read_csv(combined_inflation_metrics)
combined_source = ColumnDataSource(df_combined_inflation_metrics)


l.multi_line(xs=['Date','Date','Date'],ys=['RPI', 'CPI', 'CPIH'], source=combined_source)
#l.multi_line(xs=[['Date'],['Date'],['Date']],ys=[['RPI'], ['CPI'], ['CPIH']], source=combined_source)

show(l)

However, this is throwing the following: 但是,这将引发以下情况:

RuntimeError: 
Supplying a user-defined data source AND iterable values to glyph methods is
not possibe. Either:

Pass all data directly as literals:

    p.circe(x=a_list, y=an_array, ...)

Or, put all data in a ColumnDataSource and pass column names:

    source = ColumnDataSource(data=dict(x=a_list, y=an_array))
    p.circe(x='x', y='y', source=source, ...)

But I am not too sure why this is? 但是我不太确定为什么会这样吗?

Update: 更新:

I figured out a workaround by adding all of the values in each of the data sources. 通过在每个数据源中添加所有值,我找到了解决方法。 It works, but doesn't feel most efficient and would still like to know how to do this properly. 它可以工作,但是感觉不是最有效,并且仍然想知道如何正确执行此操作。

Edit - Code request: 编辑-代码请求:

from bokeh.plotting import figure, output_file, show
from bokeh.models import NumeralTickFormatter, DatetimeTickFormatter, ColumnDataSource, HoverTool, CrosshairTool, SaveTool, PanTool
import pandas as pd
import os
os.chdir(r'path')

#output_file('Inflation.html', title='Inflation')

RPI = 'RPI.csv'
CPI = 'CPI.csv'
CPIH = 'CPIH.csv'

df_RPI = pd.read_csv(RPI)
df_CPI = pd.read_csv(CPI)
df_CPIH = pd.read_csv(CPIH)

def to_date_time(data_frame, data_series):
    data_frame[data_series] = data_frame[data_series].astype('datetime64[ns]')

to_date_time(df_RPI, 'Date')
to_date_time(df_CPI, 'Date')
to_date_time(df_CPIH, 'Date')

RPI_source = ColumnDataSource(df_RPI)
CPI_source = ColumnDataSource(df_CPI)
CPIH_source = ColumnDataSource(df_CPIH)

l = figure(title="Historic Inflaiton Metrics", logo=None)
l.plot_width = 1200


l.xaxis[0].formatter=DatetimeTickFormatter(
        days=["%d %B %Y"],
        months=["%d %B %Y"],
        years=["%d %B %Y"],
    )


glyph_1 = l.line('Date','RPI',source=RPI_source, legend='TYPE', color='red')
glyph_2 = l.line('Date','CPI',source=CPI_source, legend='TYPE', color='blue')
glyph_3 = l.line('Date','CPIH',source=CPIH_source, legend='TYPE', color='gold')


hover = HoverTool(renderers=[glyph_1],
                 tooltips=[     ("Date","@Date{%F}"),
                                ("RPI","@RPI"),
                                ("CPI","@CPI"),
                                ("CPIH","@CPIH")],
                          formatters={"Date": "datetime"},
                      mode='vline'
                 )
l.tools = [SaveTool(), PanTool(), hover, CrosshairTool()]

show(l)

The hover tool looks up the data to show in the ColumnDataSource. 悬停工具将查找数据以显示在ColumnDataSource中。 Because you created a new ColumnDataSource for each line and restricted the hover tool to line1 it can only lookup data in the data source there. 因为您为每行创建了一个新的ColumnDataSource并将鼠标悬停工具限制为line1,所以它只能在该数据源中查找数据。

The general solution is to only create one ColumnDataSource and reuse that in each line: 通用解决方案是仅创建一个ColumnDataSource并在每一行中重复使用:

df_RPI = pd.read_csv(RPI)
df_CPI = pd.read_csv(CPI)
df_CPIH = pd.read_csv(CPIH)

df = df_RPI.merge(dfd_CPI, on="date")
df = df.merge(df_CPIH, on="date")

source = ColumnDataSource(df)

l = figure(title="Historic Inflation Metrics", logo=None)

glyph_1 = l.line('Date','RPI',source=source, legend='RPI', color='red')
l.line('Date','CPI',source=source, legend='CPI', color='blue')
l.line('Date','CPIH',source=source, legend='CPIH', color='gold')

hover = HoverTool(renderers=[glyph_1],
                 tooltips=[     ("Date","@Date{%F}"),
                                ("RPI","@RPI"),
                                ("CPI","@CPI"),
                                ("CPIH","@CPIH")],
                          formatters={"Date": "datetime"},
                      mode='vline'
                 )

show(l)

This is of course only possible if you all your dataframes can be merged into one, ie the measurement timepoints are the same. 当然,只有将所有数据帧都可以合并为一个,即测量时间点相同,才有可能。 If they are not besides resampling/interpolating I do not know a good method to do what you want. 如果除了重采样/插值以外,他们不知道做您想要的事的好方法。

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

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