2

If i want to color a square grid with different color in each grid cells, then it is possible in MATLAB with a simple call to imagesc command like here.

What if i want to color different cells in a grid like this: enter image description here

Is this functionality available by default in either python or Matlab? I tried discretizing this grid with very small square cells. And then color each cell. That works. But it seems ordinary. Is there a smarter way to get his done?

Community
  • 1
  • 1
user_1_1_1
  • 903
  • 1
  • 12
  • 27

1 Answers1

1

In python, there is the builtin polar projection for the axes. This projection allows you to automatically use almost every plotting method in polar coordinates. In particular, you need to you pcolor or pcolormesh as follows

import numpy as np
from matplotlib import pyplot as plt

r = np.linspace(0,4,5)
theta = np.linspace(0,2*np.pi,10)
theta,r = np.meshgrid(theta,r)
values = np.random.rand(*(theta.shape))

ax = plt.subplot(111,polar=True)
ax.pcolor(theta,r,values)
plt.show()

Note that this will produce a plot like this

default polar pcolor behavior

which is almost what you want. The obvious problem is that the patch vertices are joined by straight lines and not lines that follow the circle arc. You can solve this by making the angles array denser. Here is a posible way to do it.

import numpy as np
from matplotlib import pyplot as plt

r = np.linspace(0,4,5)
theta = np.linspace(0,2*np.pi,10)

values = np.random.rand(r.size,theta.size)

dense_theta = np.linspace(0,2*np.pi,100)
v_indeces = np.zeros_like(dense_theta,dtype=np.int)
i = -1
for j,dt in enumerate(dense_theta):
    if dt>=theta[i+1]:
        i+=1
    v_indeces[j] = i

T,R = np.meshgrid(dense_theta,r)

dense_values = np.zeros_like(T)

for i,v in enumerate(values):
    for j,ind in enumerate(v_indeces):
        dense_values[i,j] = v[ind]

ax = plt.subplot(111,polar=True)
ax.pcolor(T,R,dense_values)
plt.show()

Which would produce

improved pcolor

I am not aware of a way to do this in matlab but I googled around and found this that says it can produce pcolor plots in polar coordinates. You should check it out.

lucianopaz
  • 1,212
  • 10
  • 17