7

私がやりたいのは、プロット文を含む関数を定義することです。このような:

import matplotlib.pyplot as plt

def myfun(args, ax):
    #...do some calculation with args
    ax.plot(...)
    ax.axis(...)

fig.plt.figure()
ax1=fig.add_subplot(121)
ax2=fig.add_subplot(122)
para=[[args1,ax1],[args2,ax2]]
map(myfun, para)

myfun が呼び出されていることがわかりました。myfun に plt.show() を追加すると、正しいサブプロットにプロットできますが、他のサブプロットにはプロットできません。そして、最後に plt.show() を追加すると、2 組の軸だけがプロットされます。問題は、フィギュアがメイン関数にうまく転送されていないことだと思います。Pythonとmatplotlibでこのようなことをすることは可能ですか? ありがとう!

4

1 に答える 1

6

マップを介して呼び出される関数は、1 つのパラメーターのみを持つ必要があります。

import matplotlib.pyplot as plt

def myfun(args):
    data, ax = args
    ax.plot(*data)

fig = plt.figure()
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
para = [
    [[[1,2,3],[1,2,3]],ax1],
    [[[1,2,3],[3,2,1]],ax2],
]
map(myfun, para)
plt.show()

関数の署名を保持したい場合は、itertools.starmapを使用してください。

import itertools
import matplotlib.pyplot as plt

def myfun(data, ax):
    ax.plot(*data)

fig = plt.figure()
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
para = [
    [[[1,2,3],[1,2,3]],ax1],
    [[[1,2,3],[3,2,1]],ax2],
]
list(itertools.starmap(myfun, para)) # list is need to iterator to be consumed.
plt.show()
于 2013-06-17T07:26:55.077 に答える