Greetings.

I recently found myself in the position of needing to plot polar, irregularly spaced data. I’ve done similar using regularly spaced values with no problem. However, I’ve found that when the points become greatly scattered, the triangulation does not translate from rectangular to polar very well. Below, find a code example that shows this; though it is much “better” than my real-world case that uses simulation results. Essentially, in the translation from regular to polar axes, many of the triangles overlap others, many features of the plot are lost, and the plot looks mangled in certain regions, esp. across the theta=0 boundary. While subtle here (but MUCH worse in the results I’m trying to visualize), some of the triangles lie outside of the triangulated region. It isn’t merely that the polar plot is suffering from poor data coverage; the triangulation is not working properly in polar coordinates and the results are quantitatively different than the rectangular plot.

The obvious work-around for this problem illustrates the issue more clearly. If we convert rad, theta back to x, y and do a rectangular plot, the triangulation is much better (not only is there no issue around theta=0, but there are no overlapping triangles), all of the details of the non-polar version are maintained, and the plot looks great. This is not the best solution, as polar plots in Matplotlib are quite elegant.

Any help here would be appreciated. It could be that triangulations are just not suited for polar plots; it could be that the theta=0 issue throws things off, etc; I’m just not sure. It would be perfect if I could use the polar axes in the end.

Thanks.

-dw

#!/usr/bin/env python

‘’’

Demonstrate troubles with polar plots and triangulations.

‘’’

import numpy as np

import matplotlib.pyplot as plt

# Regular grid:

angle=np.linspace(0, 2*np.pi, 20)

x=np.tile(angle,20)

y=np.repeat(angle,20)

z=np.cos(x)*np.sin(y)

# Irregular grid:

x_ir=2*np.pi*np.random.random(400)

y_ir=2*np.pi*np.random.random(400)

z_ir=np.cos(x_ir)*np.sin(y_ir)

f=plt.figure()

a1=f.add_subplot(221)

a1.tricontourf(x,y,z); a1.plot(x,y, ‘k+’)

a2=f.add_subplot(222, polar=True)

a2.tricontourf(x,y,z); a2.plot(x,y, ‘k+’)

a2.triplot(x,y)

a3=f.add_subplot(223)

a3.tricontourf(x_ir,y_ir,z_ir); a3.plot(x_ir,y_ir, ‘k+’)

a4=f.add_subplot(224, polar=True)

a4.tricontourf(x_ir,y_ir,z_ir); a4.plot(x_ir,y_ir, ‘k+’)

a4.triplot(x_ir,y_ir)

# “Fix” back to rectangular.

x=y_ir*np.cos(x_ir)

y=y_ir*np.sin(x_ir)

f=plt.figure(); ax=f.add_subplot(111)

ax.tricontourf(x,y,z_ir)

ax.triplot(x,y)