bokeh彩色流线图

2024-06-13 11:41:21 发布

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

我有一张由matplotlib绘制的流线图。我想把与matplotlib(Inferno)相同的颜色。我试图在matplotlib.pyplot.streamplot中查找与color相同的函数。有什么方法可以根据数据的值来表示颜色吗? 这就是我对matplotlib.pyplot.streamplot所做的 enter image description here

我尝试了bokeh.palettes并且有一个inferno()函数,它根据0~256之间的数字显示inferno颜色。Inferno 它看起来很有效,但颜色的表示方式与matplotlib显示的不同。这是我和博克的结果。enter image description here Bokeh似乎随机显示颜色,而不是基于值(这意味着值越高,值越亮)。在

from bokeh.layouts import gridplot
from bokeh.models import BasicTickFormatter, ColorBar, BasicTicker, LinearColorMapper
import numpy as np
from bokeh.palettes import Inferno256, inferno
import bokeh.plotting as blt
from streamline import streamlines #a package that I made
for comp in range(0,3):
   fig = []
   fig.append(blt.figure())
   x = np.linspace(-6.02138592, 6.02138592, nElem[0]) #Elem[0] = 24
   y = np.linspace(-5.8125, 5.8125, nElem[1]) #Elem[1] = 32

   xs, ys = streamlines(x, y, data[..., 6].transpose(), data[..., 
   7].transpose(), density=1) 
   ''' a fuction to make x velocity and y velocity. here xs and xy are 
   232 However it varies based on the range of x and y. It means it 
   could go over 256.'''

   magnitude = np.sqrt(values[..., 2*comp]**2 
                       + values[..., 2*comp+1]**2) 
   #it will make the lines have color depending on this value

   fig[comp].multi_line(xs, ys, color=inferno(len(xs)), line_width=2, 
   line_alpha=0.8) # I need to change len(xs) because sometimes it 
   exceeds 256

   mapper = LinearColorMapper(palette='Inferno256',

                              low=np.amin(magnitude.transpose()), 

                              high=np.amax(magnitude.transpose()))


   color_bar = ColorBar(color_mapper=mapper, 
                        width=7, 
                        location=(0,0),           
                        formatter=BasicTickFormatter(precision=1),
                        ticker=BasicTicker(desired_num_ticks=4), 
                                           label_standoff=10, 
                                           border_line_color=None,
                                           padding=2,
                                           bar_line_color='black')

   fig[comp].add_layout(color_bar, 'right')
   gp = gridplot(children=fig, toolbar_location='right', 
                 ncols=2, merge_tools=True)
   show(gp)

Tags: fromimportherematplotlib颜色nplinebokeh
1条回答
网友
1楼 · 发布于 2024-06-13 11:41:21

首先创建matplotlib流图:

import numpy as np
import pylab as pl

w = 3
Y, X = np.mgrid[-w:w:100j, -w:w:100j]
U = -1 - X**2 + Y
V = 1 + X - Y**2
speed = np.sqrt(U**2 + V**2)

fig, ax = pl.subplots()
strm = ax.streamplot(X, Y, U, V, color=U, linewidth=2, cmap='viridis')

然后得到线条和颜色数据:

^{pr2}$

使用数据创建multiline,使用linear_map和{}设置行的颜色:

from bokeh.io import output_notebook, show
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
from bokeh.transform import linear_cmap
from bokeh.palettes import Viridis256

lines = strm.lines
pathes = lines.get_paths()
arr = lines.get_array().data

points = np.stack([p.vertices.T for p in pathes], axis=0)
X = points[:, 0, :].tolist()
Y = points[:, 1, :].tolist()

fig = figure()
mapper = linear_cmap(field_name="color", palette=Viridis256, low=arr.min(), high=arr.max())
source = ColumnDataSource(dict(x=X, y=Y, color=arr))
fig.multi_line("x", "y", line_color=mapper, source=source, line_width=3)
show(fig)

相关问题 更多 >