2

でいくつかのプロットを作成する必要がありますがmatplotlib、私はそれが非常に苦手です。それぞれ値listsを含む5つがあります。100それらの値は次のように異なります。

ここに画像の説明を入力

line-and-markerそれらから2つのチャートを作成できるようにしたい:

  1. 最初のプロットにはリスト 1、2、3、および 4 が含まれ、2 つの y 軸があります。リスト 1、2、および 3 は通常の y 軸に依存していますが、リスト 4 は次のように追加された y 軸に依存しています。

ここに画像の説明を入力

  1. 2 番目はリスト 4 と 5 だけをプロットする必要がありますが、通常の y 軸を使用します。

先に進む前に、 eachlistを aに変える必要がありnumpy arrayますか? とにかく、でプロットを行う方法を理解できませんでしたmatplotlib。どんな助けでも大歓迎です。ありがとう!

4

1 に答える 1

2

別の y 軸twinxにプロットする軸を作成するだけです。ここlist 4で例を見ることができます。

そして、これがあなたが望むことをするための短いスクリプトです。numpyこの場合、配列に変換する必要はありません。

import matplotlib.pyplot as plt

# Some sample lists
l1 = [0.7,0.8,0.8,0.9,0.8,0.7,0.6,0.9,1.0,0.9]
l2 = [0.2,0.3,0.1,0.0,0.2,0.1,0.3,0.1,0.2,0.1]
l3 = [0.4,0.6,0.4,0.5,0.4,0.5,0.6,0.4,0.5,0.4]

l4 = [78,87,77,65,89,98,74,94,85,73]
l5 = [16,44,14,55,34,36,76,54,43,32]

# Make a figure
fig = plt.figure()

# Make room for legend at bottom
fig.subplots_adjust(bottom=0.2)

# The axes for your lists 1-3
ax1 = fig.add_subplot(211)
# A twin axis for list 4. This shares the x axis, but has a separate y axis
ax2 = ax1.twinx()

# Plot lines 1-3
line1 = ax1.plot(l1,'bo-',label='list 1')
line2 = ax1.plot(l2,'go-',label='list 2')
line3 = ax1.plot(l3,'ro-',label='list 3')

# Plot line 4
line4 = ax2.plot(l4,'yo-',label='list 4')

# Some sensible y limits
ax1.set_ylim(0,1)
ax2.set_ylim(0,100)

# Your second subplot, for lists 4&5
ax3 = fig.add_subplot(212)

# Plot lines 4&5
ax3.plot(l4,'yo-',label='list 4')
line5 = ax3.plot(l5,'mo-',label='list 5')

# To get lines 1-5 on the same legend, we need to 
# gather all the lines together before calling legend
lines = line1+line2+line3+line4+line5
labels = [l.get_label() for l in lines]

# giving loc a tuple in axes-coords. ncol=5 for 5 columns
ax3.legend(lines, labels, loc=(0,-0.4), ncol=5)

ax3.set_xlabel('events')

# Display the figure
plt.show()

ここに画像の説明を入力

于 2015-11-13T10:58:23.253 に答える