![](/img/trans.png)
[英]Is there an equivalent of `pcolormesh` (or `pcolor`) from `matplotlib.pyplot` in `plotly`?
[英]Is there a plotly equivalent to matplotlib pcolormesh?
我是编程和 Python 的新手。 我正在研究一些教科书天线模式的东西,有一种叫做“正弦空间”的东西,其中天线模式被投影到 xy 平面。 生成的图案应包含在单位圆内)。 当我使用 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 得到了几乎相同的结果。
我真的很感激任何帮助。 谢谢!
我对天线物理不是很熟悉,所以我不确定你要绘制什么,但我认为使用 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)
此答案基于此GitHub 问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.