在多个图表上绘制一条线

发布于 2024-11-10 03:45:16 字数 238 浏览 0 评论 0原文

我不知道这个东西怎么称呼,甚至不知道如何描述它,所以标题可能有点误导。

第一个附图是用 pyplot 创建的。我想画一条穿过所有图表的直线,而不是我目前使用的三个红点。在 pyplot 中可以吗?第二张图片是我正在寻找的。 当前 我在寻找什么

I don't know how this thing is called, or even how to describe it, so the title may be a little bit misleading.

The first attached graph was created with pyplot. I would like to draw a straight line that goes through all graphs instead of the three red dot I currently use. Is it possible in pyplot? Second image is what I am looking for.
Currently
What I am looking for

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

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

发布评论

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

评论(4

落花浅忆 2024-11-17 03:45:16

您可以通过关闭相关线条的剪辑来实现此目的。可能有一种更干净的方法来做到这一点 - 您可能可以直接在主框架上绘制线条 - 但以下方法对我有用:

from matplotlib import pyplot as plt
from numpy import arange, sin, cos

xx = arange(100)
cut = (xx > 0) & (xx % 17 == 0)
y1 = sin(xx)
y2 = (xx**2) % 2.0+cos(xx+0.5)

fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.plot(xx, y1, c="blue",zorder=1)
ax1.scatter(xx[cut], y1[cut], c="red",zorder=2)
ax2 = fig.add_subplot(212)
ax2.plot(xx, y2, c="green",zorder=1)
ax2.scatter(xx[cut], y2[cut], c="red",zorder=2)

for x in xx[cut]:
    ax1.axvline(x=x,ymin=-1.2,ymax=1,c="red",linewidth=2,zorder=0, clip_on=False)
    ax2.axvline(x=x,ymin=0,ymax=1.2,c="red",linewidth=2, zorder=0,clip_on=False)

plt.draw()
fig.savefig('pic.png')

通过更多的工作,您可以修改线条绘制以处理多个子图的一般情况windows,但我非常懒。 :^)

跨子图垂直线示例

You can pull this off by turning clipping off for the relevant lines. There's probably a cleaner way to do this -- you might be able to draw lines on the main frame directly -- but the following worked for me:

from matplotlib import pyplot as plt
from numpy import arange, sin, cos

xx = arange(100)
cut = (xx > 0) & (xx % 17 == 0)
y1 = sin(xx)
y2 = (xx**2) % 2.0+cos(xx+0.5)

fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.plot(xx, y1, c="blue",zorder=1)
ax1.scatter(xx[cut], y1[cut], c="red",zorder=2)
ax2 = fig.add_subplot(212)
ax2.plot(xx, y2, c="green",zorder=1)
ax2.scatter(xx[cut], y2[cut], c="red",zorder=2)

for x in xx[cut]:
    ax1.axvline(x=x,ymin=-1.2,ymax=1,c="red",linewidth=2,zorder=0, clip_on=False)
    ax2.axvline(x=x,ymin=0,ymax=1.2,c="red",linewidth=2, zorder=0,clip_on=False)

plt.draw()
fig.savefig('pic.png')

With a bit more work you could modify the line drawing to handle the general case of multiple subplot windows, but I'm profoundly lazy. :^)

example of cross-subplot vertical lines

物价感观 2024-11-17 03:45:16

相关文档:
http://matplotlib.sourceforge.net/api/pyplot_api.html# matplotlib.pyplot.axvline

编辑:自 @DSM的答案比我的好得多,我羞愧地结合了一些这个答案是为了让我的答案不那么糟糕。

我试图处理一列中多个子图的一般情况(即不是多个子图的更一般情况,例如在网格中)。

感谢@DSM 的回答和@Artium 的提问。

import matplotlib.pyplot as plt
import numpy as np

def main():
    fig = plt.figure() 

    x = np.arange(20)
    y1 = np.cos(x)
    y2 = (x**2)
    y3 = (x**3)
    yn = (y1,y2,y3)
    cut = (x > 0) & (x % 2 == 0)
    COLORS = ('b','g','k')

    for i,y in enumerate(yn):
        ax = fig.add_subplot(len(yn),1,i+1)

        ax.plot(x, y,ls='solid', color=COLORS[i], zorder=1) 
        ax.scatter(x[cut], y[cut], c='r', zorder=2)

        if i != len(yn) - 1:
            ax.set_xticklabels( () )

        for j in x[cut]:
            if i != len(yn) - 1:
                ax.axvline(x=j, ymin=-1.2, ymax=1,
                           c='r', lw=2, zorder=0, clip_on=False)
            else:
                ax.axvline(x=j, ymin=0, ymax=1,
                           c='r', lw=2, zorder=0, clip_on=False)

    fig.suptitle('Matplotlib Vertical Line Example')
    plt.show()

if __name__ == '__main__':
    main()

在此处输入图像描述

Relevant documentation:
http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axvline

Edit: since @DSM's answer was so much better than mine I have shamefully incorporated some of that answer in an attempt to make my answer less poor.

I've tried to handle the somewhat-general case of multiple subplots in a column (i.e. not the even-more-general case of multiple subplots, e.g. in a grid).

Thanks, @DSM, for your answer and @Artium for the question.

import matplotlib.pyplot as plt
import numpy as np

def main():
    fig = plt.figure() 

    x = np.arange(20)
    y1 = np.cos(x)
    y2 = (x**2)
    y3 = (x**3)
    yn = (y1,y2,y3)
    cut = (x > 0) & (x % 2 == 0)
    COLORS = ('b','g','k')

    for i,y in enumerate(yn):
        ax = fig.add_subplot(len(yn),1,i+1)

        ax.plot(x, y,ls='solid', color=COLORS[i], zorder=1) 
        ax.scatter(x[cut], y[cut], c='r', zorder=2)

        if i != len(yn) - 1:
            ax.set_xticklabels( () )

        for j in x[cut]:
            if i != len(yn) - 1:
                ax.axvline(x=j, ymin=-1.2, ymax=1,
                           c='r', lw=2, zorder=0, clip_on=False)
            else:
                ax.axvline(x=j, ymin=0, ymax=1,
                           c='r', lw=2, zorder=0, clip_on=False)

    fig.suptitle('Matplotlib Vertical Line Example')
    plt.show()

if __name__ == '__main__':
    main()

enter image description here

極樂鬼 2024-11-17 03:45:16

[更新 03/2013] 在 matplotlib 的较新版本中,有 ConnectionPatch 极大地简化了此任务。当需要覆盖两个以上的子图时,它特别有用。

from matplotlib import pyplot as plt
from matplotlib.patches import ConnectionPatch
from numpy import arange, sin, cos

xx = arange(100)
cut = (xx > 0) & (xx % 17 == 0)
y1 = sin(xx)
y2 = (xx**2) % 2.0+cos(xx+0.5)

fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.plot(xx, y1, c="blue")
ax1.scatter(xx[cut], y1[cut], c="red")
ax2 = fig.add_subplot(212)
ax2.plot(xx, y2, c="green")
ax2.scatter(xx[cut], y2[cut], c="red")

for x in xx[cut]:
    con = ConnectionPatch(xyA=(x, -1.5), xyB=(x, 1.5),
        coordsA="data", coordsB="data", axesA=ax2, axesB=ax1,
        arrowstyle="-", linewidth=2, color="red")
    ax2.add_artist(con)

plt.draw()
fig.savefig('pic.png')

[Update 03/2013] In newer revisions of matplotlib, there's ConnectionPatch that greatly simplifies this task. It's particularly useful whenever there are more than two subplots that need to be covered.

from matplotlib import pyplot as plt
from matplotlib.patches import ConnectionPatch
from numpy import arange, sin, cos

xx = arange(100)
cut = (xx > 0) & (xx % 17 == 0)
y1 = sin(xx)
y2 = (xx**2) % 2.0+cos(xx+0.5)

fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.plot(xx, y1, c="blue")
ax1.scatter(xx[cut], y1[cut], c="red")
ax2 = fig.add_subplot(212)
ax2.plot(xx, y2, c="green")
ax2.scatter(xx[cut], y2[cut], c="red")

for x in xx[cut]:
    con = ConnectionPatch(xyA=(x, -1.5), xyB=(x, 1.5),
        coordsA="data", coordsB="data", axesA=ax2, axesB=ax1,
        arrowstyle="-", linewidth=2, color="red")
    ax2.add_artist(con)

plt.draw()
fig.savefig('pic.png')
青巷忧颜 2024-11-17 03:45:16

我会尝试 axvline(x, y1, y2) (链接),但我认为 pyplot 中的任何选项都不会绘制跨越多个子图/图表的内容。

如果是这样的话,我会尝试在图中的每个点绘制相同的垂直线,希望向观看者传达相同的意图。

I would try axvline(x, y1, y2) (link), but I don't think any of the options in pyplot will draw something that spans across several subplots/graphs.

If that's the case, I would just try drawing the same vertical line at each point in the graph, hoping that the same intent is conveyed to the viewer.

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