我不知道這個東西是如何被調用的,甚至是如何描述它,所以標題可能有點誤導。
第一個附加圖是用pyplot創建的。我想繪製一條直線貫穿所有圖形,而不是我目前使用的三個紅點。在pyplot中可能嗎?第二個圖像是我正在尋找的。 在幾張圖上畫出一條線
回答
您可以通過啓用剪下的相關線路拉這一關。有可能是一個更清潔的方式來做到這一點 - 你也許可以在主車架上直接畫線 - 但我下面的工作:
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')
有了多一點的工作,你可以修改畫線到處理多個子情節窗口的一般情況,但我非常懶惰。 :^)
我會嘗試axvline(x, y1, y2)
(link),但我不認爲任何的pyplot選項將借鑑的東西,跨越幾個次要情節/圖跨越。
如果是這樣的話,我會嘗試在圖中的每個點繪製相同的垂直線,希望將相同的意圖傳達給觀衆。
道歉。我的回答沒有提供任何你沒有的新信息。我已經舉了一個例子,所以我想我會發布。 +1對你來說快得多。 – bernie 2011-05-27 00:39:53
@Adam Bernier永遠不要道歉提供示例源代碼... – 2011-05-27 02:19:48
與Jonathan一致,與源代碼和示例的答案比沒有,速度不計數! – 2011-05-27 13:14:03
相關文章:
http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axvline
編輯:既然@DSM's answer這麼比我我已經可恥納入了一些答案,試圖讓我的回答少的可憐好得多。
我試過處理一列中多個子圖的一般情況(即不是多個子圖的更一般情況,例如在網格中)。
感謝@ 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()
[更新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')
你如何確定在哪裏拿紅點? – Sword22 2011-05-26 23:40:35
@ Sword22所有圖形都有相同的x軸。紅點基本上是x軸值的列表。 – Artium 2011-05-26 23:47:33