如何使用 matplotlib 绘制点,使它们出现在脊柱顶部?

发布于 2024-09-18 15:49:06 字数 994 浏览 1 评论 0 原文

以下生成包含三个数据点(位于 (0, 0)、(0, 0.5) 和 (1, 1) 的图。只有位于绘图区域内的绘图点(小圆圈)部分可见,因此我在角落看到四分之一圆,沿左脊柱看到半圆。

我可以使用一个技巧来使所有点完全可见,这样它们就不会被裁剪在轴框架内吗?

from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure

fig = Figure()
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111)
ax.plot([0, 0, 1], [0, 0.5, 1], 'o')
fig.canvas.print_figure('test.png')

编辑:Amro 的建议(显而易见的方法)并不是首选方法,因为这些方法适用于 ROC 图(通常在两个轴上用 0 到 1 的框绘制)。如果我可以欺骗 matplotlib 生成与 http://www.google 上的许多结果类似的结果。 com/search?q=roc+plot ,它在两个轴上都有一个紧紧围绕 0..1 的框,但像许多人一样在轴线顶部绘制了点,这将是最佳的。

编辑2:我猜这可以使用“脊椎放置"(从 MPL 0.99 开始新),绘图区域按照 Amro 的建议稍微放大,但随后脊椎稍微重新定位到沿着两个 0 轴。我会对此进行试验并发布答案(如果有效),但请随意击败我。

The following generates a plot with three data points, at (0, 0), (0, 0.5), and (1, 1). Only that portion of the plotted points (small circles) which lie inside the plot area is visible, so I see quarter-circles in the corners, and a half circle along the left spine.

Is there a trick I can use to make all the points fully visible, so they are not clipped within the axes frame?

from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure

fig = Figure()
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111)
ax.plot([0, 0, 1], [0, 0.5, 1], 'o')
fig.canvas.print_figure('test.png')

Edit: Amro's suggestion -- the obvious approach -- is not the preferred approach as these are for ROC graphs (conventionally drawn with a box from 0 to 1 on both axes). If I could trick matplotlib into producing results similar to the many at http://www.google.com/search?q=roc+plot which have a box tightly around 0..1 on both axes, yet have points drawn on top of the axis lines as many of them do, that would be optimal.

Edit 2: I'm guessing this can be done using "spine placement" (new as of MPL 0.99), with the plot area enlarged slightly as Amro suggested, but then with the spines repositioned slightly to be along both 0 axes. I'll experiment with this and post an answer if it works, though feel free to beat me to it.

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

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

发布评论

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

评论(3

只为守护你 2024-09-25 15:49:06

您可以在绘图命令中或在调用“plot”返回的艺术家对象中关闭剪辑。

首先,这是图,带有额外的大符号,因此很清楚:

alt text

在绘图命令中,您可以执行

ax.plot([0, 0, 1], [0, 0.5, 1], 'o', clip_on=False, markersize=20)

或可以执行有

p = ax.plot([0, 0, 1], [0, 0.5, 1], 'o', markersize=20)
for m in p:
    m.set_clip_on(False)

You can turn the clipping off, either in the plot command, or in the artist objects returned by a call to "plot".

First, here's the figure, with extra big symbols so it's clear:

alt text

In the plot command you can do

ax.plot([0, 0, 1], [0, 0.5, 1], 'o', clip_on=False, markersize=20)

or you could have

p = ax.plot([0, 0, 1], [0, 0.5, 1], 'o', markersize=20)
for m in p:
    m.set_clip_on(False)
阳光①夏 2024-09-25 15:49:06

您可以在所有方向上稍微扩展轴限制:

ax = fig.add_subplot(111, xlim=(-0.1,1.1), ylim=(-0.1,1.1))

alt text

You can extend the axes limits a bit in all directions:

ax = fig.add_subplot(111, xlim=(-0.1,1.1), ylim=(-0.1,1.1))

alt text

心的憧憬 2024-09-25 15:49:06

我将使用新的 spin.set_position() 功能的想法与 Amro 的建议结合起来,稍微扩大了范围。以下仅适用于 matplotlib 1.0 或更高版本,因为它依赖于新的 spin.set_bounds() 调用。 (我相信 Amro 的想法也需要 1.0 或更高版本,因为 xlim/ylim kwargs 对 0.99.1 没有任何作用。)

fig = Figure()
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111, xlim=(-0.1, 1.1), ylim=(-0.1, 1.1))
ax.plot([0, 0, 1], [0, 0.5, 1], 'o')
for side in 'left bottom top right'.split():
    ax.spines[side].set_position('zero')
    ax.spines[side].set_bounds(0, 1)
canvas.print_figure('test.png')

我仍然很有兴趣听到是否有不同的方法,但经过多次谷歌搜索后我的猜测是matplotlib 在该区域有一个基本限制:所有数据都被为轴定义的区域紧密裁剪。

I combined my idea using the new spine.set_position() capability with Amro's suggestion to expand the bounds slightly. The following works with only with matplotlib 1.0 or later, as it relies on the new spine.set_bounds() call. (I believe Amro's idea needed 1.0 or later as well, since the xlim/ylim kwargs did nothing for me with 0.99.1.)

fig = Figure()
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111, xlim=(-0.1, 1.1), ylim=(-0.1, 1.1))
ax.plot([0, 0, 1], [0, 0.5, 1], 'o')
for side in 'left bottom top right'.split():
    ax.spines[side].set_position('zero')
    ax.spines[side].set_bounds(0, 1)
canvas.print_figure('test.png')

I'd still be quite interested to hear if there's a different approach, but my guess after much googling is that matplotlib has a basic restriction around this area: all data is tightly clipped by the region defined for the axis.

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