4

個々のサブプロットの色を変更したい:
1. プロットの希望の色を手動で指定する
2. ランダムな色を使用する

基本コード ( 1から取得)

 df = DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))
 df = df.cumsum()

 df.plot(subplots=True)
 plt.legend(loc='best') 
 plt.show()

私はこれを試しました:

 colors = ['r','g','b','r']                   #first option
 colors = list(['r','g','b','r'])             #second option
 colors = plt.cm.Paired(np.linspace(0,1,4))   #third option

 df.plot(subplots=True, color=colors)

しかし、それらはすべて機能しません。2つ見つかりましたが、これを変更する方法がわかりません:

 plots=df.plot(subplots=True)
 for color in plots:
   ??????
4

2 に答える 2

5

styleパラメータに色の略語のリストを指定することで、これを簡単に行うことができます。

from pandas import Series, DataFrame, date_range
import matplotlib.pyplot as plt
import numpy as np

ts = Series(np.random.randn(1000), index=date_range('1/1/2000', periods=1000))
ts = ts.cumsum()

df = DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))
df = df.cumsum()

ax = df.plot(subplots=True, style=['r','g','b','r'], sharex=True)
plt.legend(loc='best')
plt.tight_layout()
plt.show()

ここに画像の説明を入力


「標準」色からのランダムな色

「標準」色 (青、緑、赤、シアン、マゼンタ、黄、黒、白) のみを使用する場合は、色の略語を含む配列を定義し、これらのランダムなシーケンスを引数としてstyleパラメーターに渡すことができます。 :

colors = np.array(list('bgrcmykw'))

...

ax = df.plot(subplots=True,
             style=colors[np.random.randint(0, len(colors), df.shape[1])],
             sharex=True) 
于 2013-08-22T09:03:27.423 に答える
3

私が知る限り、各軸を単独でループし、これを手動で行う必要があります。これはおそらく機能のはずです。

df = DataFrame(np.random.randn(1000, 4), columns=list('ABCD'))
df = df.cumsum()
colors = 'r', 'g', 'b', 'k'
fig, axs = subplots(df.shape[1], 1,sharex=True, sharey=True)
for ax, color, (colname, col) in zip(axs.flat,colors,df.iteritems()):
    ax.plot(col.index,col,label=colname,c=color)
    ax.legend()
    ax.axis('tight')
fig.tight_layout()

ここに画像の説明を入力

または、インデックスに日付がある場合は、次のようにします。

import pandas.util.testing as tm

df = tm.makeTimeDataFrame()
df = df.cumsum()
colors = 'r', 'g', 'b', 'k'
fig, axs = subplots(df.shape[1], 1,sharex=True, sharey=True)
for ax, color, (colname, col) in zip(axs.flat, colors, df.iteritems()):
    ax.plot(col.index, col, label=colname,c=color)
    ax.legend()
    ax.axis('tight')
fig.tight_layout()
fig.autofmt_xdate()

取得するため

ここに画像の説明を入力

于 2013-08-21T21:25:56.363 に答える