【问题标题】:Is there a plotly equivalent to matplotlib pcolormesh?是否有与 matplotlib pcolormesh 相当的情节?
【发布时间】:2021-03-03 05:42:45
【问题描述】:

我是编程和 Python 方面的新手。我正在研究一些教科书天线图案的东西,有一种叫做“正弦空间”的东西,其中天线图案被投影到 x-y 平面。生成的图案应包含在单位圆内)。当我使用 matplotlib.pcolormesh 时,我能够得到我预期的模式。但我不知道如何让它与 Plotly 一起使用。

我试图在 Jupyter Notebook 中说明我的问题。使用 matplotlib.pcolormesh,你可以看到我得到了预期的情节。我故意没有包括实际的天线方向图计算,因为它们太长了,而且对于说明这个问题没有必要。

# Import libraries
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

# Setup Sinespace
## - define theta and phi
theta = np.linspace(0, np.pi/2, 100)
phi = np.linspace(0, 2*np.pi, 100)

## - reshape theta and phi
thetaReshape = np.reshape(theta, (100, 1))
phiReshape = np.reshape(phi, (1, 100))

## - now when you multiply with thetaReshape and phiReshape you get a 100 x 100 array
u = np.sin(thetaReshape) * np.cos(phiReshape)
v = np.sin(thetaReshape) * np.sin(phiReshape)

# Generate a random array
Z = np.random.randn(100, 100)

# Setup and plot the figure
fig, ax = plt.subplots(1, 1)
ax.pcolormesh(u, v, Z)
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.set_aspect(1)
fig.set_size_inches(4, 4)

上面的情节是我期望看到的。当我使用 plotly 时,我做了以下操作:

import plotly.graph_objects as go
fig = go.Figure(data=go.Heatmap(
                   z=Z,
                   x=u,
                   y=v
))
fig.show()

这导致下面的这个情节没有任何意义:

go.Contour 也得到了几乎相同的结果。

我非常感谢任何帮助。谢谢!

【问题讨论】:

    标签: python matplotlib plotly heatmap contour


    【解决方案1】:

    我对天线物理不是很熟悉,所以我不确定您要绘制什么,但我认为 a 设法使用 Plotly 做了一个工作示例,如下所示。我的建议是绘制极坐标而不是将坐标转换为笛卡尔空间。

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    # Import libraries
    import numpy as np
    import plotly.graph_objs as go
    from plotly.offline import plot
    import plotly.express as px
    
    # Setup Sinespace
    # define theta and phi
    theta = np.rad2deg(np.linspace(0, 2 * np.pi, 100))
    phi = np.linspace(0, 1, 100)
    
    theta, phi = np.meshgrid(theta, phi)
    
    theta = theta.ravel()
    phi = phi.ravel()
    Z = np.random.randn(*theta.shape)
    
    hovertemplate = ('my r: %{r}<br>'
                     'my theta: %{theta}<br>'
                     'my value: %{customdata[0]:.2f}<br>'
                     '<extra></extra>')
    
    fig = go.Figure(
        go.Barpolar(
            r=phi,
            theta=theta,
            customdata=np.vstack((Z)),
            hovertemplate=hovertemplate,
            marker=dict(
                colorscale=px.colors.diverging.BrBG,
                showscale=True,
                color=Z,
            )
        )
    )
    
    fig.update_layout(
        title='My Plot',
        polar=dict(
            angularaxis=dict(tickvals=np.arange(0, 360, 10),
                             direction='clockwise'),
            radialaxis_tickvals=[],
        )
    )
    
    plot(fig)
    

    此代码将产生以下图:

    此答案基于this GitHub 问题。

    【讨论】:

      猜你喜欢
      • 2021-01-10
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 2021-11-22
      • 2014-12-20
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      相关资源
      最近更新 更多