8

以下のコードを使用して、4つの関数の実行に費やされた時間をプロットしています。x軸は実行回数を表し、y軸は関数の実行に費やされた時間を表します。

私はあなたが私が次のことを達成するのを手伝ってくれるかどうか疑問に思っていました:

1)正の値のみが表示されるようにx軸の制限を設定します(xは各関数が実行された回数を表すため、常に正になります)

2)4つの機能の凡例を作成する

ありがとうございました、

マーク

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


r = mlab.csv2rec('performance.csv')

fig = Figure(figsize=(9,6))

canvas = FigureCanvas(fig)

ax = fig.add_subplot(111)

ax.set_title("Function performance",fontsize=14)

ax.set_xlabel("code executions",fontsize=12)

ax.set_ylabel("time(s)",fontsize=12)

ax.grid(True,linestyle='-',color='0.75')

ax.scatter(r.run,r.function1,s=10,color='tomato');
ax.scatter(r.run,r.function2,s=10,color='violet');
ax.scatter(r.run,r.function3,s=10,color='blue');
ax.scatter(r.run,r.function4,s=10,color='green');

canvas.print_figure('performance.png',dpi=700)
4

1 に答える 1

25

legend凡例が表示されるように呼び出す必要があります。labelkwarg は_label、問題のアーティスト オブジェクトの属性のみを設定します。便宜上、凡例のラベルをプロット コマンドに明確に関連付けることができるようにしています。を明示的に呼び出さない限り、凡例はプロットに追加されませんax.legend(...)。また、xaxis の制限を調整したくax.set_xlimありません。ax.xlimこちらもご覧くださいax.axis

次のようなものが欲しいようです:

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import numpy as np

# Generate some data
x = np.arange(0, 22, 2)
f1, f2, f3, f4 = np.cumsum(np.random.random((4, x.size)) - 0.5, axis=1)

# It's much more convenient to just use pyplot's factory functions...
fig, ax = plt.subplots()

ax.set_title("Function performance",fontsize=14)
ax.set_xlabel("code executions",fontsize=12)
ax.set_ylabel("time(s)",fontsize=12)
ax.grid(True,linestyle='-',color='0.75')

colors = ['tomato', 'violet', 'blue', 'green']
labels = ['Thing One', 'Thing Two', 'Thing Three', 'Thing Four']
for func, color, label in zip([f1, f2, f3, f4], colors, labels):
    ax.plot(x, func, 'o', color=color, markersize=10, label=label)

ax.legend(numpoints=1, loc='upper left')
ax.set_xlim([0, x.max() + 1])

fig.savefig('performance.png', dpi=100)

ここに画像の説明を入力

于 2011-09-11T17:02:54.643 に答える