6

同じデータを 2 つの異なる形式 (対数スケールと線形スケール) でプロットしています。

基本的に、まったく同じプロットを作成したいのですが、スケールが異なり、上下に重なっています。

私が今持っているのはこれです:

import matplotlib.pyplot as plt

# These are the plot 'settings'
plt.xlabel('Size')
plt.ylabel('Time(s)');
plt.title('Matrix multiplication')

plt.xticks(xl, rotation=30, size='small')
plt.grid(True)

# Settings are ignored when using two subplots

plt.subplot(211)
plt.plot(xl, serial_full, 'r--')
plt.plot(xl, acc, 'bs')
plt.plot(xl, cublas, 'g^')

plt.subplot(212)
plt.yscale('log')
plt.plot(xl, serial_full, 'r--')
plt.plot(xl, acc, 'bs')
plt.plot(xl, cublas, 'g^')

plt.subplotの前のすべての「設定」は無視されます。

これを思い通りに動作させることができますが、各サブプロット宣言の後にすべての設定を複製する必要があります。

両方のサブプロットを一度に構成する方法はありますか?

4

2 に答える 2

14

設定は通常、plt.*matplotlib の現在のプロットに適用されます。を使用するとplt.subplot、新しいプロットが開始されるため、設定は適用されなくなります。Axesプロットに関連付けられたオブジェクトを通過することで、ラベル、目盛りなどを共有できます (こちらの例を参照)。代わりに、共通の「スタイリング」を 1 つの関数に入れ、それをプロットごとに呼び出すことを提案します。

def applyPlotStyle():
    plt.xlabel('Size')
    plt.ylabel('Time(s)');
    plt.title('Matrix multiplication')

    plt.xticks(range(100), rotation=30, size='small')
    plt.grid(True)

plt.subplot(211)
applyPlotStyle()
plt.plot(xl, serial_full, 'r--')
plt.plot(xl, acc, 'bs')
plt.plot(xl, cublas, 'g^')

plt.subplot(212)
applyPlotStyle()
plt.yscale('log')
plt.plot(xl, serial_full, 'r--')
plt.plot(xl, acc, 'bs')
plt.plot(xl, cublas, 'g^')

余談ですが、プロットコマンドをそのような関数に抽出することで、より多くの重複を根絶することができます:

def applyPlotStyle():
    plt.xlabel('Size')
    plt.ylabel('Time(s)');
    plt.title('Matrix multiplication')

    plt.xticks(range(100), rotation=30, size='small')
    plt.grid(True)

def plotSeries():
    applyPlotStyle()
    plt.plot(xl, serial_full, 'r--')
    plt.plot(xl, acc, 'bs')
    plt.plot(xl, cublas, 'g^')

plt.subplot(211)
plotSeries()

plt.subplot(212)
plt.yscale('log')
plotSeries()

別の補足として、タイトルを(各プロットの上ではなく)図の上部に配置するだけで十分な場合がありsuptitleます。xlabel同様に、が 2 番目のプロットの下にのみ表示されるだけで十分な場合があります。

def applyPlotStyle():
    plt.ylabel('Time(s)');

    plt.xticks(range(100), rotation=30, size='small')
    plt.grid(True)

def plotSeries():
    applyPlotStyle()
    plt.plot(xl, serial_full, 'r--')
    plt.plot(xl, acc, 'bs')
    plt.plot(xl, cublas, 'g^')

plt.suptitle('Matrix multiplication')
plt.subplot(211)
plotSeries()

plt.subplot(212)
plt.yscale('log')
plt.xlabel('Size')
plotSeries()

plt.show()
于 2012-10-18T11:41:03.420 に答える
5

ハンスの答えはおそらく推奨されるアプローチです。ただし、軸のプロパティを別の軸にコピーしたい場合は、次の方法を見つけました。

fig = figure()
ax1 = fig.add_subplot(2,1,1)
ax1.plot([1,2,3],[4,5,6])
title('Test')
xlabel('LabelX')
ylabel('Labely')

ax2 = fig.add_subplot(2,1,2)
ax2.plot([4,5,6],[7,8,9])


for prop in ['title','xlabel','ylabel']:
    setp(ax2,prop,getp(ax1,prop))

show()
fig.show()

ここに画像の説明を入力

これにより、設定するプロパティのホワイトリストを設定できます。現在、titlexlabelおよびylabelがありますが、 を使用getp(ax1)して、使用可能なすべてのプロパティのリストを印刷できます。

次のようなものを使用してすべてのプロパティをコピーできますが、プロパティ設定の一部が 2 番目のプロットを台無しにするため、コピーしないことをお勧めします。ブラックリストを使用して一部を除外しようとしましたが、機能させるにはそれをいじる必要があります。

insp = matplotlib.artist.ArtistInspector(ax1)
props = insp.properties()
for key, value in props.iteritems():
    if key not in ['position','yticklabels','xticklabels','subplotspec']:
        try:
            setp(ax2,key,value)
        except AttributeError:
            pass

(これexcept/passは、取得可能であるが設定可能ではないプロパティをスキップすることです)

于 2012-10-18T19:29:03.270 に答える