I want to plot a data, which is polar in nature (i.e. has theta and r). But I also want cartesian axes for it, which should be correct (i.e. r cos(theta) must be x and r sin(theta) must be y)
I have tried something, but it isn't working

The origin of the polar plot is not coinciding with the origin of cartesian, and the radius is also mismatching.
Attaching the code
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()
Please help!
I think the ideal solution to your problem will depend a bit more on what you are trying to accomplish beyond this toy example.
But consider the following:
In the above image, one can see the two axes are aligned.
Note however, if you call the following commands to set theta limits, the alignment between the Cartesian and the polar axes is broken:
This is because the calls to
set_thetaminandset_thetamaxintroduced new transformation rules for the polar axesaxp. One can see this by exploring theaxp.transmatrices, e.g.axp.transWedge.get_matrix()both before and after the adjustment to theta limits.One can actually use these updated transformations when plotting Cartesian data to maintain alignment:
In the above, one can see alignment once again, however, now the Cartesian axes labels are incorrect. You can set them manually, again the ideal solution will depend on what you wish to accomplish.
Hope this helps somewhat, and maybe someone with more experience using matplotlib transformations can help further!