Matplotlib极坐标轴和笛卡尔坐标轴位于同一图形上

jq6vz3qz  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(201)

我想绘制一个数据,它本质上是极坐标的(即,有θ和r)。但我还想为它绘制笛卡尔坐标轴,它应该是正确的(即,r cos(θ)必须是x,r sin(θ)必须是y)。
我已经尝试了一些方法,但不起作用x1c 0d1x
极坐标图的原点与笛卡尔坐标的原点不重合,半径也不匹配。
附加代码

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
    
ax4 = fig.add_subplot(1,1,1)
ax4_polar = fig.add_axes(ax4.get_position(), projection='polar', frameon=False)

ax4.set_facecolor('white')
ax4.set_xlim([-10,10])
ax4.set_ylim([-10,10])
ax4.set_xlabel("Distance along lateral axis (meters)")
ax4.set_ylabel("Distance along longitudinal axis (meters)")
ax4.set_title("X-Y scatter plot", color='black')
ax4_polar.set_thetamin(30)
ax4_polar.set_thetamax(150)
# ax4.grid(True)
ax4.xaxis.label.set_color('black')       
ax4.yaxis.label.set_color('black')          
ax4.tick_params(axis='x', colors='black') 
ax4.tick_params(axis='y', colors='black')
theta = [0, np.pi/6, np.pi/3, np.pi/2, 3*np.pi/4]
r = [0, 2, 3 ,4 ,6]  
a4, =  ax4_polar.plot(theta, r, c='red', marker=".", ls="", alpha=1, label="X-Y scatter")

plt.show()

救命啊!

55ooxyrt

55ooxyrt1#

我认为解决你的问题的理想方案将更多地取决于你在这个玩具例子之外试图完成的事情。
但请考虑以下几点:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot()
axp = fig.add_axes(ax.get_position().bounds, polar=True, frameon=False)

theta = np.array([0, np.pi/6, np.pi/3, np.pi/2, 3*np.pi/4])
r = np.array([0, 2, 3 ,4 ,6])

axp.plot(theta, r, 'r.')

# the following 4 commands align the two axes 
ax.set_aspect('equal')
ax.set_xlim(-10,10)
ax.set_ylim(-10,10)
axp.set_rlim(0,10)

# to plot on the cartesian axes, one must convert to polar
x = np.cos(theta)*r
y = np.sin(theta)*r
ax.plot(x,y, 'bs', ms=10, mfc='none') 

# display the data
plt.draw()
plt.pause(0.1)

在上图中,可以看到两个轴对齐。
但是请注意,如果您呼叫下列指令来设定theta限制,则会中断直角坐标轴和极坐标轴之间的对齐:

axp.set_thetamin(30)
axp.set_thetamax(150)
plt.draw()
plt.pause(0.1)

这是因为对set_thetaminset_thetamax的调用为极轴axp引入了新的变换规则,这可以通过研究axp.trans矩阵来看出,例如在调整到theta极限之前和之后的axp.transWedge.get_matrix()
在绘制笛卡尔数据时,实际上可以使用这些更新的变换来保持对齐:

# remove the original Cartesian axes plot with bad alignment
ax.lines.pop()

# grab the transformation introduced to the theta limits 
# note, I explored the transformations tutorial and experimented here to find the proper rules, as transWedge doesn't seem well documented
trans = axp.transProjectionAffine + axp.transWedge + axp.transAxes

# use the transform keyword argument
ax.plot(x,y, 'bs', ms=10, mfc='none', transform=trans)
plt.title("Oh no, the Cartesian axes labels broke!")

plt.draw()
plt.pause(0.1)

在上面的例子中,你可以再次看到对齐,但是现在笛卡尔坐标轴的标签不正确。你可以手动设置它们,同样理想的解决方案将取决于你想完成什么。

ax.set_title("Manually set axis labels")
mn,mx = ax.get_ylim()  # equiv to ax.get_xlim()
# see https://stackoverflow.com/a/40475221/2077270
ax_to_dat = axp.transAxes + axp.transData.inverted()
xlabs = []
ylabs = []
for t in ax.get_yticks():  
    tfrac = (t-mn)/(mx-mn)
    th,rad = ax_to_dat.transform( [0,tfrac])
    ylab = "%.1f" % (rad*np.sin(th))
    ylabs.append(ylab)
    
    th,rad = ax_to_dat.transform( [tfrac,0])
    xlab = "%.1f" % (rad*np.cos(th))
    xlabs.append(xlab)

ax.set_yticklabels(ylabs)
ax.set_xticklabels(xlabs)

# add some test lines
ax.hlines([10,0], -10, 10, color='lightgreen', ls='--', transform=trans)

plt.draw()
plt.pause(0.1)

希望这能有所帮助,也许有更多使用matplotlib transformations经验的人可以进一步帮助!

相关问题