将 Matplotlib 补丁与极坐标图混合?

发布于 2024-10-10 08:37:37 字数 537 浏览 2 评论 0 原文

我正在尝试在极坐标中绘制一些数据,但我不想要使用 Matplotlib polar() 函数获得的标准刻度、标签、轴等。我想要的只是原始图,没有其他任何东西,因为我正在使用手动绘制的补丁和线条来处理所有内容。

以下是我考虑过的选项:

1)使用 polar() 绘制数据,隐藏多余的内容(使用 ax.axes.get_xaxis().set_visible(False) > 等),然后绘制我自己的轴(使用 Line2D、Circle 等)。问题是,当我调用 polar() 并随后添加 Circle 补丁时,它是在极坐标中绘制的,最终看起来像一个无穷大符号。此外,缩放似乎不适用于 polar() 函数。

2)跳过polar()函数并以某种方式使用Line2D手动制作我自己的极坐标图。问题是我不知道如何使 Line2D 在极坐标中绘制,并且还没有弄清楚如何使用变换来做到这一点。

知道我应该如何进行吗?

I'm trying to plot some data in polar coordinates, but I don't want the standard ticks, labels, axes, etc. that you get with the Matplotlib polar() function. All I want is the raw plot and nothing else, as I'm handling everything with manually drawn patches and lines.

Here are the options I've considered:

1) Drawing the data with polar(), hiding the superfluous stuff (with ax.axes.get_xaxis().set_visible(False), etc.) and then drawing my own axes (with Line2D, Circle, etc.). The problem is when I call polar() and subsequently add a Circle patch, it's drawn in polar coordinates and ends up looking like an infinity symbol. Also zooming doesn't seem to work with the polar() function.

2) Skip the polar() function and somehow make my own polar plot manually using Line2D. The problem is I don't know how to make Line2D draw in polar coordinates and haven't figured out how to use a transform to do that.

Any idea how I should proceed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

作妖 2024-10-17 08:37:37

考虑到您想要做什么,您的选项#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.

Your option #2 is probably the simplest, given what you want to do. You would thus stay in rectangular coordinates, modify your function from polar to rectangular coordinates, and plot it with plot() (which is easier than using `Line2D').

The transformation of your polar function into a rectangular one can be done with:

def polar_to_rect(theta, r):
    return (r*cos(theta), r*sin(theta))

and the plotting can be done with:

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.
不喜欢何必死缠烂打 2024-10-17 08:37:37

要删除刻度和标签,请尝试使用

`matplotlib.pyplot.tick_params(axis='both', which='both', length=0, width=0, labelbottom = False, labeltop = False, labelleft = False, labelright = False)`

From http://matplotlib。 sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.polar

To remove the ticks and the labels, try using

`matplotlib.pyplot.tick_params(axis='both', which='both', length=0, width=0, labelbottom = False, labeltop = False, labelleft = False, labelright = False)`

From http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.polar

放我走吧 2024-10-17 08:37:37

关于您关于使用 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)

Regarding your comment about using the matplotlib transforms...I used the following method to translate a polar plot into a polygon that I could draw on my cartesian/rectangular axes.

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文