最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Adjust Matplotlib Polar Plot to Show Sub Degree Motion (AKA Stretch a polar plot() slice) - Stack Overflow

programmeradmin1浏览0评论

I have RA and DEC pointing data I would like to show on a polar plot (converting to rho and theta). The theta motion is very small, ~0.01 degrees. This is not easily seen on a full polar plot so I am trying to 'zoom in' to the region and show the change from data point to data point. When I adjust the thetamin/thetamax below to the limits I would prefer the wedge becomes a very thin line that losses all useful information.

I would like a wedge shape like below but where the min/max theta angle shown is at least a degree.

    import numpy as np

    import matplotlib.pyplot as plt

    import matplotlib

    import pandas as pd

    print('matplotlib version : ', matplotlib.__version__)

    fig = plt.figure()

    

    ra = np.asarray([1.67484,1.67485,1.67485,1.67486,1.67486,1.67488,1.67487,1.67488,1.67487, 1.67487]) #radians

    dec = np.asarray([-0.92147,-0.92147,-0.92147,-0.92147,-0.92147,-0.92147,-0.92147, -0.92147,-0.92147, -0.92147]) #radians

    rho = np.sqrt(ra**2 + dec**2) # get rho from ra and dec
    theta = np.arctan2(dec,ra) # get theta from ra and dec

    fig = plt.figure()
    ax = fig.add_subplot(1,1,1,polar=True)
    ax.plot(theta, rho,'*-',color='y')
    ax.set_ylim(1.9114,1.9117) # limits of rho

    ax.set_thetamin(310)
    ax.set_thetamax(340)

    plt.show()

I've been reading online and looking at the matplotlib polar plot documentation but the examples I've found don't go beyond what I've implemented so far..

I have RA and DEC pointing data I would like to show on a polar plot (converting to rho and theta). The theta motion is very small, ~0.01 degrees. This is not easily seen on a full polar plot so I am trying to 'zoom in' to the region and show the change from data point to data point. When I adjust the thetamin/thetamax below to the limits I would prefer the wedge becomes a very thin line that losses all useful information.

I would like a wedge shape like below but where the min/max theta angle shown is at least a degree.

    import numpy as np

    import matplotlib.pyplot as plt

    import matplotlib

    import pandas as pd

    print('matplotlib version : ', matplotlib.__version__)

    fig = plt.figure()

    

    ra = np.asarray([1.67484,1.67485,1.67485,1.67486,1.67486,1.67488,1.67487,1.67488,1.67487, 1.67487]) #radians

    dec = np.asarray([-0.92147,-0.92147,-0.92147,-0.92147,-0.92147,-0.92147,-0.92147, -0.92147,-0.92147, -0.92147]) #radians

    rho = np.sqrt(ra**2 + dec**2) # get rho from ra and dec
    theta = np.arctan2(dec,ra) # get theta from ra and dec

    fig = plt.figure()
    ax = fig.add_subplot(1,1,1,polar=True)
    ax.plot(theta, rho,'*-',color='y')
    ax.set_ylim(1.9114,1.9117) # limits of rho

    ax.set_thetamin(310)
    ax.set_thetamax(340)

    plt.show()

I've been reading online and looking at the matplotlib polar plot documentation but the examples I've found don't go beyond what I've implemented so far..

Share Improve this question edited 2 days ago Márton Horváth 40113 bronze badges asked Feb 5 at 20:35 tronbonstronbons 331 silver badge7 bronze badges 1
  • same question in matlab Stretch a polarplot() slice – pippo1980 Commented Feb 6 at 11:48
Add a comment  | 

2 Answers 2

Reset to default 3

First of all, you still have some potential concerning narrowing down the plotted radial range, e.g., to ax.set_ylim(1.91159, 1.91164). Please also note, that when I was searching for a solution (which I couldn't fin on the internet either), I found that using np.arctan2() is the appropriate approach for polar coordinates, that is the reason why I changed this part in your code.

Otherwise, I had no better idea than applying a scaling approach to your plot. Now the wedge in question is scaled up by an arbitrary scaling factor (i.e., theta for the upscaled plot in degrees), as in:

import numpy as np
import matplotlib.pyplot as plt


ra = np.asarray([1.67484, 1.67485, 1.67485, 1.67486, 1.67486, 
                 1.67488, 1.67487, 1.67488, 1.67487, 1.67487]) 

dec = np.asarray([-0.92147, -0.92147, -0.92147, -0.92147, -0.92147, 
                  -0.92147, -0.92147, -0.92147, -0.92147, -0.92147]) 


rho = np.sqrt(ra**2 + dec**2) 
theta = np.arctan2(dec, ra)  # instead of np.tan(dec / ra), see the other answer.


# set scaling factor
scalingfactor = 40

# scaling up by arbitrary scaling factor
theta_scaled = (theta - np.min(theta)) / (np.max(theta) - np.min(theta)) * np.radians(scalingfactor)


fig, ax = plt.subplots(figsize=(6, 6), subplot_kw={'projection': 'polar'})

ax.plot(theta_scaled, rho, '*-', color='y', label=f"Scaled 1:{1/scalingfactor}")
ax.set_ylim(1.91159, 1.91164)

#check theta limits
print(np.degrees(min(theta)), np.degrees(max(theta)))

#creating theta ticks
ticks = np.radians(np.linspace(0, scalingfactor, 5)) 
ax.set_xticks(ticks) 

# setting theta labels explicitly
scaled_labels = np.round(np.linspace(360+np.degrees(min(theta)), 360+np.degrees(max(theta)), len(ticks)), 4)
ax.set_xticklabels(scaled_labels) 

ax.set_thetamin(-3)  
ax.set_thetamax(scalingfactor+1)  

ax.grid(True, linestyle="--", alpha=0.5)
ax.legend()

plt.show()

I am sure you can still make it look prettier, otherwise, it looks to be a solid solution to me, resulting in for example this version of the plot:

Mine is uglier :

import numpy as np
import matplotlib.pyplot as plt

import matplotlib

import pandas as pd

print('matplotlib version : ', matplotlib.__version__)

fig = plt.figure()

ra = np.asarray([1.67484,1.67485,1.67485,1.67486,1.67486,1.67488,1.67487,1.67488,1.67487, 1.67487]) #radians

dec = np.asarray([-0.92147,-0.92147,-0.92147,-0.92147,-0.92147,-0.92147,-0.92147, -0.92147,-0.92147, -0.92147]) #radians

rho = np.sqrt(ra**2 + dec**2) # get rho from ra and dec

#theta_or = np.tan(dec / ra) #get theta from ra and dec to keep original

theta_or = np.arctan2(dec , ra) ## changed as per comments , theta expressed correctly

#theta = np.tan(dec / ra) as in the original question

#theta expressed correctly:
theta = np.arctan2(dec, ra)

print('\ntheta : ', theta)
print(theta*180/np.pi)
print(360+theta*180/np.pi)

# theta = 360+theta*180/np.pi


idx = None
print(enumerate(theta))
for i, value in enumerate(theta):
    #print(i,value)
    #print(theta[i] )
    theta[i] = value + i*0.1
    #print(i*0.1)
    
    idx = i

print('theta[idx] : ', theta[idx], idx)
print('rho : ', rho)
print('\ntheta : ', theta)

fig = plt.figure()
ax = fig.add_subplot(1,1,1,polar=True)
ax.plot(theta, rho,'*-',color='y')
ax.set_ylim(1.9114,1.9117)


srt = [-10+theta[0]*180/np.pi , 10+theta[idx]*180/np.pi]

print('\nsrt : ', srt)



ax.set_thetamin(srt[0])
ax.set_thetamax(srt[1])

ticks = np.linspace(theta[0], theta[-1], len(theta))
ax.set_xticks(ticks)

#ax.set_xticklabels([i*180/np.pi+360 for i in theta]) 
ax.set_xticklabels([360 + i*180/np.pi for i in theta_or]) 

plt.show()

but the pics has x_labels more similar to the original:

Guess as per other answer The conversion in the question was incorrect:

See

And What is numpy.arctan2() in Numpy?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论