使用bqp将colormap应用于直线(或线段)

2024-06-25 23:30:26 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以使用bqplot将颜色映射应用于一行?你知道吗

使用matplotlib,可以将直线分割成若干段,对它们进行整理,并使用matplotlib.collections.LineCollection(segments, cmap='RdBu').set_array(c)应用颜色映射,然后使用axis.add_collection()绘制所有的颜色映射。你知道吗

但是,我在bqplot中找不到等价的方法。我遗漏了什么吗?你知道吗


Tags: addmatplotlib颜色arraycollections整理直线collection
1条回答
网友
1楼 · 发布于 2024-06-25 23:30:26

柔性线标记可以做到这一点。参见下面的示例(取自https://github.com/bloomberg/bqplot/blob/master/examples/Marks/Object%20Model/FlexLine.ipynb

from bqplot import *

## Get Data

dates = np.arange('2005-02', '2005-03', dtype='datetime64[D]')
size = len(dates)
spx = 100 + 5 * np.cumsum(np.random.randn(size))
vix = 10 + np.cumsum(np.random.randn(size))

## Displaying extra dimension with color

lin_x = DateScale()
lin_y = LinearScale()
col_line = ColorScale(colors=['green', 'white', 'red'])

ax_x = Axis(scale=lin_x, label='Date', label_location='end')
ax_y = Axis(scale=lin_y, orientation='vertical', label='Index', label_offset='4ex')
ax_col = ColorAxis(label='Vol', scale=col_line, tick_format='0.2f')

fig_margin = dict(top=50, left=80, right=20, bottom=70)
fl = FlexLine(x=dates, y=spx, color=vix,
              scales={'x': lin_x, 'color': col_line, 'y': lin_y})

Figure(marks=[fl], axes=[ax_x, ax_y, ax_col], fig_margin=fig_margin)

enter image description here

相关问题 更多 >