python - 用 Bokeh 中的颜色简化绘图

标签 python matplotlib bokeh

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

我尝试了 bokeh.palettes 并且有 inferno() 函数,它根据 0~256 的数字显示 hell 颜色。 Inferno 它似乎有效,但颜色的表示方式与 matplotlib 显示的不同。这是我的 Bokeh 效果。 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)

最佳答案

首先创建一个 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')

然后获取线条和颜色数据:

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

使用数据创建多线,使用linear_mappalette设置线条的颜色:

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)

关于python - 用 Bokeh 中的颜色简化绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57186853/

相关文章:

Python:如何计算py文件中的关键字?

python - Bokeh:确定哪个模型调用了 on_change 事件处理程序

python - 如何绘制时间序列的倒数

javascript - Bokeh:双击圆圈时的JS回调

bokeh - 使用 Zeppelin 的内联 Bokeh 图

python - 选择在 shockwave flash 上且在 html 代码中没有任何 id 的复选框

python - 在 pandas 中,如何从字典列表创建数据框?

python - dev_appserver.py 无法发出 SSL 请求的解决方法

python - 按散点图叠加回归线和 rsq 进行分组

python - 在 Python 中绘制不规则间隔的 RGB 图像