简体   繁体   English

散景是否有 matplotlib contourf 等价物?

[英]Is there a matplotlib contourf equivalent for bokeh?

I need to perform something in bokeh which I am successfully doing in matplotlib.我需要在散景中执行一些我在 matplotlib 中成功执行的操作。

I plot contour plots using matplotlib along with a colorbar on the side using contourf function.我使用 matplotlib 绘制等高线图,并使用 contourf 函数在侧面绘制一个颜色条。 I do not use the meshgrid fuction because I already have X, Y and Z data in (78, 60001) shape arrays each.我不使用 meshgrid 函数,因为我已经在 (78, 60001) 形状数组中分别拥有 X、Y 和 Z 数据。 So this is what works:所以这是有效的:

array1[0].shape
(78, 60001)
array1[1].shape
(78, 60001)
array1[2].shape
(78, 60001)
CS = plt.contourf(array1[0], array1[1], array1[2], 25, cmap = plt.cm.jet)

Which gives the following results:这给出了以下结果:

3d-fft

How do I do the same thing in bokeh?我如何在散景中做同样的事情? Most of the examples on the internet and manual are using meshgrid.互联网和手册上的大多数示例都使用meshgrid。

=== ===

Update #1: Example: https://docs.bokeh.org/en/latest/docs/gallery/image.html更新 #1:示例: https : //docs.bokeh.org/en/latest/docs/gallery/image.html

I tried the above bokeh code as follows:我尝试了上面的散景代码如下:

p.image(array1[2], array1[0], array1[1], dw=10, dh=10, palette="Spectral11")

Which gives the following error:这给出了以下错误:

 RuntimeError: Columns need to be 1D (x is not)

=== ===

Update #2:更新#2:

p.image(array1[2].ravel(), array1[0].ravel(), array1[1].ravel(), dw=10, dh=None, palette="Spectral11")

Above line of code creates the HTML file but no plot is displayed.上面的代码行创建了 HTML 文件,但没有显示绘图。

RuntimeError: Columns need to be 1D (x is not)运行时错误:列必须是一维的(x 不是)

The reason for this is that you are attempting to pass coordinates for every pixel of the image, but that is not what Bokeh expects.这样做的原因是您试图为图像的每个像素传递坐标,但这不是 Bokeh 所期望的。 For each 2D image data (either RGBA array, or scalar data to be color mapped in the client), Bokeh only expects to receive a single value for each of: x, y, dw, and dh.对于每个 2D 图像数据(RGBA 数组或要在客户端进行颜色映射的标量数据),Bokeh 只希望为 x、y、dw 和 dh 中的每一个接收一个值。 The x and y coordinates are the coordinates of the lower left of the image. x 和 y 坐标是图像左下角的坐标。 The dw and dh are the width and height of the the image in data space coordinates. dw 和 dh 是图像在数据空间坐标中的宽度和高度。

It is possible to pass multiple images to a single call to image_rgba or image .可以将多个图像传递给对image_rgbaimage的单个调用。 In this case x, y, dw, and dh would have a single value for each image.在这种情况下,x、y、dw 和 dh 对每个图像都有一个值。 Ie, x would be a 1d array of numbers (one for each image).即, x 将是一维数字数组(每个图像一个)。

I think the hvplot library would answer your question.我认为hvplot 库会回答你的问题。

See a the reproducible example on an Xarray dataset below:请参阅以下Xarray 数据集上的可重现示例:

import xarray as xr
import hvplot.xarray  # noqa

import holoviews as hv
from holoviews import opts
hv.extension('bokeh')

air_ds = xr.tutorial.open_dataset('air_temperature').load()
meanairbyyear = air_ds.air.groupby('time.year').mean()
stdairbyyear = air_ds.air.groupby('time.year').std()

meanair2d = xr.Dataset(
    {
        "y2013": (["lat", "lon"], meanairbyyear[0,:,:]),
        "y2014": (["lat", "lon"], meanairbyyear[1,:,:]),
    },
    coords={
        "lon": ("lon", meanairbyyear.lon),
        "lat": ("lat", meanairbyyear.lat),
    },
)

meanair2d

pl=meanair2d.hvplot.contourf(z='y2013',width=400)

from bokeh.models import HoverTool
MyHover = HoverTool(
    tooltips=[
        ( 'x', '$x'),
        ( 'y', '$y'),
        ( 'Year 2013', '@y2013{%3.0f} degC'),      
   ],
    formatters={
        '$x' : 'numeral',
        '$y' : 'numeral',
        '@y2013' : 'printf',      
    },
    point_policy="follow_mouse"
)

pl.opts(tools = [MyHover])

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM