将Matplotlib补丁与极坐标图结合?
我想在极坐标系中绘制一些数据,但我不想要Matplotlib的polar()
函数提供的那些标准的刻度、标签、坐标轴等等。我只想要一个干净的图像,其他的我会用自己手动画的图形和线条来处理。
我考虑过以下几种方案:
1) 用polar()
函数绘制数据,然后把多余的东西隐藏掉(比如用ax.axes.get_xaxis().set_visible(False)
等),接着再自己画坐标轴(用Line2D
、Circle
等)。问题是,当我调用polar()
后,再添加一个Circle
图形时,它会在极坐标系中绘制,最后看起来像个无穷大符号。而且,使用polar()
函数时,缩放似乎也不太好使。
2) 不用polar()
函数,试着用Line2D
手动制作自己的极坐标图。问题是我不知道怎么让Line2D
在极坐标系中绘制,也没搞明白怎么用变换来实现这一点。
有没有什么建议我该怎么做?
3 个回答
0
关于你提到的使用matplotlib的转换功能……我用了一种方法,把极坐标图转换成了一个多边形,这样我就可以在我的笛卡尔坐标系(也就是常见的直角坐标系)上绘制它。
import matplotlib.pyplot as plt
polarPlot = plt.subplot(111, polar = True)
# Create some dummy polar plot data
polarData = np.ones((360,2))
polarData[:,0] = np.arange(0, np.pi, np.pi/360) * polarData[:,0]
# Use the polar plot axes transformation into cartesian coordinates
cartesianData = polarPlot.transProjection.transform(polarData)
0
要去掉刻度和标签,可以试试下面的代码:
`matplotlib.pyplot.tick_params(axis='both', which='both', length=0, width=0, labelbottom = False, labeltop = False, labelleft = False, labelright = False)`
来源于 http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.polar
2
你选择的第二个选项可能是最简单的,考虑到你想要做的事情。这样的话,你可以继续使用直角坐标系,把你的函数从极坐标转换成直角坐标,然后用 plot()
来绘图(这比使用 `Line2D` 要简单)。
将你的极坐标函数转换成直角坐标函数可以通过以下方式完成:
def polar_to_rect(theta, r):
return (r*cos(theta), r*sin(theta))
而绘图可以通过以下方式进行:
def my_polar(theta, r, *args, **kwargs):
"""
theta, r -- NumPy arrays with polar coordinates.
"""
rect_coords = polar_to_rect(theta, r)
pyplot.plot(rect_coords[0], rect_coords[1], *args, **kwargs)
# You can customize the plot with additional arguments, or use `Line2D` on the points in rect_coords.