1

I have a list with x,y,z, r coordinates (cartesians). I need to plot some circles in a polar plot, but I don't know how to do it with cartesians coordinates.

I am trying to do it with this line

circle1 = plt.Circle((x[i], y[i]), r[i], transform=ax3.transData._b, color = 'r', alpha=0.5, fill=False)

but this doesn't seem to work because I obtain the circles too far away from the center of the origin.

any help?

data1 = pd.read_csv('Uchuu_lightcone_0_11.9_voids.txt', sep='\s+', header=None)
data1 = pd.DataFrame(data1)

x = data1[0]
y = data1[1]
r = data1[3]
z = data1[2]
azvoids, elvoids, rvoids = cart2sph(x,y,z)
d = ax3.scatter(azvoids, rvoids, s=3, c='red', alpha=1, marker='.')
for i in range(len(x)):
    if elvoids[i] > 35 and elvoids[i] < 45:
        circle1 = plt.Circle((x[i], y[i]), r[i], transform=ax3.transData._b, color = 'r', alpha=0.5, fill=False)
        ax3.add_artist(circle1)


#  The cart2sph function is

def cart2sph(x,y,z):
    """ x, y, z :  ndarray coordinates
        ceval: backend to use: 
              - eval :  pure Numpy
              - numexpr.evaluate:  Numexpr """
    azimuth = arctan2(y,x)*180/math.pi
    xy2 = x**2 + y**2
    elevation = arctan2(z, sqrt(xy2))*180/math.pi
    r = sqrt(xy2 + z**2)
    return azimuth, elevation, r
4
  • does this help : stackoverflow.com/questions/19827792/… Commented May 24, 2022 at 8:00
  • 1
    Can you put a minimal example? Commented May 24, 2022 at 8:02
  • @LSeu no it doesn't, I've written the same as in the example and when I plot the center of the circles and the circles I don't get the centers right in the center of the circles Commented May 24, 2022 at 8:04
  • @ymmx added to the original post! Commented May 24, 2022 at 8:06

1 Answer 1

1

You should use azvoids and rvoids to plot the center of the circle since you use those to show tham in the scatter plot

import  math
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.transforms as transforms

def cart2sph(x, y, z):
    """ x, y, z :  ndarray coordinates
        ceval: backend to use:
              - eval :  pure Numpy
              - numexpr.evaluate:  Numexpr """
    azimuth = np.arctan2(y, x) * 180 / math.pi
    xy2 = x ** 2 + y ** 2
    elevation = np.arctan2(z, np.sqrt(xy2)) * 180 / math.pi
    r = np.sqrt(xy2 + z ** 2)
    return azimuth, elevation, r

#
# data1 = pd.read_csv('Uchuu_lightcone_0_11.9_voids.txt', sep='\s+', header=None)
# data1 = pd.DataFrame(data1)

N=100
x = (np.random.rand(N)-0.5)*100
y = (np.random.rand(N)-0.5)*100
z = (np.random.rand(N)-0.5)*100
r = np.random.rand(N)*10
azvoids, elvoids, rvoids = cart2sph(x, y, z)
fig = plt.figure()
ax3 = plt.subplot(111 )
d = plt.scatter(azvoids, elvoids, s=3 , c='red', alpha=1, marker='.' )
for i in range(len(x)):
    if elvoids[i] > 35 and elvoids[i] < 45:
        # circle1 = plt.Circle((azvoids[i], elvoids[i]), rvoids[i], color='r', alpha=0.5, fill=False)

        x, y =  ax3.transData.transform((azvoids[i], elvoids[i]))
        trans = (fig.dpi_scale_trans +
                 transforms.ScaledTranslation(azvoids[i], elvoids[i], ax3.transData))
        circle1 = plt.Circle((azvoids[i], elvoids[i]), rvoids[i]  , color='r', alpha=0.5, fill=None)
        ax3.add_artist(circle1)

plt.axis('equal')
plt.show()
#  The cart2sph function is

enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

That doesn't work neither. However, I thought transform=ax3.transData._b did that work for me
I eliminated the part transform=ax3.transData._b changed x, y for azvoids[i] rvoids[i] and now something appears but not spheres
I'm not sure to understand your issue. I edited the post to make equal scale for both axis so you get real circle and not ellipses. can you explain what you expect as output?
I have two files, one with galaxies and other with voids... They are both in cartesians coordinates and I need first to convert this cartesians coordinates to spherical ones (that's the def cart2sph) and then I need to plot these galaxies as points and these voids as circles (forget about el coordinate) as well as voids centers
Why do you need to convert to spherical coordinate? the issue is that you have a point in 3D and you try to display them in 2D so you need to make a projection.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.