python-3.x - 如何使用散景绘制具有不同类型虚线的单线?
问题描述
我正在尝试为一组点绘制线。目前,我以数据框的形式将点设置为列名 X、Y 和类型。每当类型为 1 时,我想将点绘制为虚线,而每当类型为 2 时,我想将点绘制为实线。目前,我正在使用 for 循环遍历所有点并使用 plt.dash 绘制每个点。但是,这会减慢我的运行时间,因为我想绘制超过 40000 个点。那么,是否有一种简单的方法来绘制具有不同线条类型的线条整体点?
解决方案
您可以通过像这样绘制多个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)
推荐阅读
- javascript - 如何在默认引导切换器下设置全屏菜单样式?
- c# - 在 Linq 选择中返回非聚合以及聚合
- r - R中的交互式Chloropeth选举学院地图
- sql-server - 如何更新不同表上的 AFTER INSERT 触发器内的表?
- javascript - 由于引导程序,Jquery Datepicker UI Rails 未在响应式浏览器上显示
- c# - 将 WPF 控件转换为 PDF
- react-native - 从 github 打开 react-native 项目时构建失败
- php - PHP Mysqli 数据库连接池以避免最大用户数
- tmux - 从 tmux 命令行创建新窗格
- security - 如何通过 IP 地址限制对我的 subversion 服务器(即 svnserve)的访问,所以只有我的 IP 可以签出、提交等?