有没有一个情节上等同于matplotlib pColoresh的? [英] Is there a plotly equivalent to matplotlib pcolormesh?

查看:20
本文介绍了有没有一个情节上等同于matplotlib pColoresh的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程和Python方面的新手。我正在做一些教科书上的天线方向图,有一种叫做正弦空间的东西,天线方向图被投影到x-y平面上。生成的图案应包含在单位圆内)。当我使用matplotlib.pColoresh时,我能够获得我想要的模式。但我想不出如何让它与普洛特利一起工作。

我试图在Jupyter笔记本中说明我的问题。使用matplotlib.pColoresh,您可以看到我得到了预期的图。我特意没有包括实际的天线方向图计算,因为它们太长了,不需要用来说明这个问题。

# 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)

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

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

这导致了下面的图表,这没有任何意义:

我也得到了与Go几乎相同的结果。

我真的很感谢你的帮助。谢谢!

推荐答案

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

#!/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)

此代码将产生以下图形:

此答案基于thisGitHub问题。

这篇关于有没有一个情节上等同于matplotlib pColoresh的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆