Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 博克多线图_Python_Python 3.x_Bokeh - Fatal编程技术网

Python 博克多线图

Python 博克多线图,python,python-3.x,bokeh,Python,Python 3.x,Bokeh,我试图在一张图表上绘制RPI、CPI和CPIH,使用HoverTool当您在图表的给定区域上平移时,显示每一个的值 我最初尝试使用line()分别添加每一行,哪种方法有效: 但是,HoverTool仅在滚动到各行时才能正常工作 我尝试过使用multi\u line()如下: combined_inflation_metrics = 'combined_inflation_metrics.csv' df_combined_inflation_metrics = pd.read_csv(combi

我试图在一张图表上绘制RPI、CPI和CPIH,使用
HoverTool
当您在图表的给定区域上平移时,显示每一个的值

我最初尝试使用
line()
分别添加每一行,哪种方法有效:

但是,
HoverTool
仅在滚动到各行时才能正常工作

我尝试过使用
multi\u 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)
然而,这有以下几点:

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, ...)
但我不太清楚这是为什么

更新:

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)
我通过添加每个数据源中的所有值找到了解决方法。它可以工作,但感觉不是最有效的,并且仍然想知道如何正确地完成这项工作

编辑-代码请求:

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)

悬停工具查找要在ColumnDataSource中显示的数据。因为您为每行创建了一个新的ColumnDataSource,并将悬停工具限制为line1,所以它只能查找数据源中的数据

一般的解决方案是只创建一个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)

当然,只有当您的所有数据帧都可以合并为一个数据帧时,这才是可能的,即测量时间点是相同的。如果它们不是除了重采样/插值之外,我不知道一个好的方法来做你想做的事情。

你已经研究过了吗?@syntonym,是的,我使用了
vline
你能显示
方法(不是多行方法)的代码吗?当然,请参阅更新后。谢谢这看起来很棒,我的数据是在同一时期,所以应该运行良好!