散景多线图 [英] Bokeh multiline plot

查看:61
本文介绍了散景多线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一张图表上绘制RPI,CPI和CPIH,并用HoverTool显示当您在图表的给定区域上平移时的值.

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.

我最初尝试使用line()分别添加每一行,哪种工作方式:

I initially tried adding each line separately using line() which kind of worked:

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

However, the HoverTool only works correctly when you scroll over the individual lines.

我曾尝试使用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?

更新:

我想出了一种变通办法,方法是在每个数据源中添加所有值.它可以工作,但感觉并不是最有效,并且仍然想知道如何正确执行此操作.

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.

编辑-代码请求:

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,所以它只能在该数据源中查找数据.

The hover tool looks up the data to show in the 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并在每一行中重复使用:

The general solution is to only create one ColumnDataSource and reuse that in each line:

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, i.e. 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.

这篇关于散景多线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆