首页 > 解决方案 > 如何使用散景绘制具有不同类型虚线的单线?

问题描述

我正在尝试为一组点绘制线。目前,我以数据框的形式将点设置为列名 X、Y 和类型。每当类型为 1 时,我想将点绘制为虚线,而每当类型为 2 时,我想将点绘制为实线。目前,我正在使用 for 循环遍历所有点并使用 plt.dash 绘制每个点。但是,这会减慢我的运行时间,因为我想绘制超过 40000 个点。那么,是否有一种简单的方法来绘制具有不同线条类型的线条整体点?

标签: python-3.xbokeh

解决方案


您可以通过像这样绘制多个line段来实现它(Bokeh v1.1.0)

import pandas as pd
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, Range1d, LinearAxis

line_style = {1: 'solid', 2: 'dashed'}

data = {'name': [1, 1, 1, 2, 2, 2, 1, 1, 1, 1],
        'counter': [1, 2, 3, 3, 4, 5, 5, 6, 7, 8],
        'score': [150, 150, 150, 150, 150, 150, 150, 150, 150, 150],
        'age': [20, 21, 22, 22, 23, 24, 24, 25, 26, 27]}
df = pd.DataFrame(data)

plot = figure(y_range = (100, 200))
plot.extra_y_ranges = {"Age": Range1d(19, 28)}
plot.add_layout(LinearAxis(y_range_name = "Age"), 'right')

for i, g in df.groupby([(df.name != df.name.shift()).cumsum()]):
    source = ColumnDataSource(g)
    plot.line(x = 'counter', y = 'score', line_dash = line_style[g.name.unique()[0]], source = source)
    plot.circle(x = 'counter', y = 'age', color = "blue", size = 10, y_range_name = "Age", source = source)

show(plot)

在此处输入图像描述


推荐阅读