如何使用散景或 matplotlib 在 python 中绘制级联视图?

How to plot cascade view in python using bokeh or matplotlib?

[基本上我该如何绘制下面的图表]

我正在尝试在每条线图之间创建一个偏移量,以在图片中生成相同的图,而线条不会相互重叠。

下面是我尝试散景闪避 () 的代码,但它没有 运行!:

from bokeh.layouts import layout
from bokeh.models import ColorPicker, RangeSlider,HoverTool
from bokeh.plotting import Figure, output_file, show
from bokeh.transform import dodge

TOOLTIPS = [
    ("(x,y)", "($x, $y)"),
]

p = Figure(
    x_range=(dfs[0]['t'].min(), dfs[0]['t'].max()),
              x_axis_label = "t (ns)",
              y_axis_label = "mV",
            tooltips=TOOLTIPS,
              plot_width=850, 
              plot_height=600

)
for i in range(0,menu):
    line = p.line(x=dfs[i].t,legend_label=file_names[i],
                 y=dodge(dfs[i].A,cascade[i],p.x_range),
                 color=random.choice(color), 
                 line_width=2)
    p.legend.location = "top_left"
    p.legend.click_policy="hide"

    output_file("Test.html", title="Test")
    
    

    range_slider = RangeSlider(
        title=" Adjust X-Axis range",
        start=0,
        end=dfs[0]['t'].max(),
        step=1e-5,
        value=(p.x_range.start, p.x_range.end),
    )
    range_slider.format = "0.000000"
    range_slider.js_link("value", p.x_range, "start", attr_selector=0)
    range_slider.js_link("value", p.x_range, "end", attr_selector=1)

    
    ##Final Chart layout
layout = layout([
#             [picker],
            [range_slider],
            [p]])

p.toolbar.autohide = True

p.title_location = "above"
p.title.text = "Ultrasonic"
p.title.text_font_size = "30px"
p.title.align = "center"
p.title.text_color = "black"

show(layout)

我收到以下错误:

ValueError: failed to validate Dodge(id='1207', ...).value: expected a value of type Real, got 0      -0.000937

下面是如何绘制线条的示例:

让我们假设您拥有所需的所有导入和这个非常简单的 DataFrame:

import pandas as pd

from bokeh.plotting import Figure, output_notebook, show
from bokeh.models import ColumnDataSource
from bokeh.transform import dodge
output_notebook()

dfs = pd.DataFrame({'A':[1,2,3], 'B':[1,2,3]})
>>>
   A  B
0  1  1
1  2  2
2  3  3

你可以为每一列计算一个 fixed_offset

offset = 5
fixed_offset = dfs.max().shift(1).fillna(0).cumsum() + [i*offset for i in range(dfs.shape[1])]

现在你有两个选择:

  1. 修改原始数据并绘制一条线
dfs = dfs.add(fixed_offset)
source = ColumnDataSource(dfs)
p = Figure(width=300, height=300, x_range=(-1,3))
for name, color in zip(dfs.columns, ['blue', 'green']):
    p.line(
        x='index',
        legend_label=name,
        y=name,
        line_width=2,
        source=source,
        color = color,
    )
show(p)

  1. 使用闪避:
source = ColumnDataSource(dfs)
p = Figure(width=300, height=300, x_range=(-1,3))
for name, value, color in zip(dfs.columns, fixed_offset, ['blue', 'green']):
    p.line(
        x='index',
        legend_label=name,
        y=dodge(name, value),
        line_width=2,
        source=source,
        color = color,
    )

两种情况下的输出都是