1

I'm a novice at programming and Python. I'm working on some textbook antenna pattern stuff and there's a thing called "sinespace" where the antenna pattern is projected down to the x-y plane. The resulting pattern should be contained within a unit circle). I'm able to get my expected pattern when I use matplotlib.pcolormesh. But I can't figure out how to get it to work with Plotly.

I tried to illustrate my problem in a Jupyter Notebook. Using matplotlib.pcolormesh, you can see that I get the expected plot. I purposely didn't include the actual antenna pattern calculations as they're too long and not necessary for illustrating this issue.

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

Here's what I get with matplotlib.pcolormesh

The above plot is what I expect to see. When I used plotly, I did the following:

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

Which results in this plot below that doesn't make any sense:

Here's what I get with Plotly

I get pretty much the same thing with go.Contour as well.

I truly appreciate any help. Thanks!

1 Answers1

3

I am not very familiar with antenna physics so I am not sure what you are trying to plot, but I think a managed to do a working example using Plotly as shown below. My suggestion would be to plot in polar coordinates instead of converting the coordinates to cartesian space.

#!/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 code will result in the following plot: enter image description here

This answer was based on this GitHub Issue.