matplotlib 散布図のカラーバー 質問する

matplotlib 散布図のカラーバー 質問する

私は、x、y、c の 3 つのプロット パラメーターを持つデータを扱っています。散布図のカスタム カラー値を作成するにはどうすればよいですか?

これを拡張する私がやろうとしていること:

import matplotlib
import matplotlib.pyplot as plt
cm = matplotlib.cm.get_cmap('RdYlBu')
colors=[cm(1.*i/20) for i in range(20)]
xy = range(20)
plt.subplot(111)
colorlist=[colors[x/2] for x in xy] #actually some other non-linear relationship
plt.scatter(xy, xy, c=colorlist, s=35, vmin=0, vmax=20)
plt.colorbar()
plt.show()

しかし結果はTypeError: You must first set_array for mappable

ベストアンサー1

散布図に関するmatplotlibドキュメントより1:

cmapはcがfloatの配列である場合にのみ使用されます。

したがって、colorlist は、現在使用しているタプルのリストではなく、浮動小数点のリストである必要があります。plt.colorbar() は、plt.scatter() が返す CircleCollection のようなマップ可能なオブジェクトを必要とします。vmin と vmax は、カラーバーの制限を制御できます。vmin/vmax の外側にあるものは、エンドポイントの色を取得します。

これはあなたにとってどうですか?

import matplotlib.pyplot as plt
cm = plt.cm.get_cmap('RdYlBu')
xy = range(20)
z = xy
sc = plt.scatter(xy, xy, c=z, vmin=0, vmax=20, s=35, cmap=cm)
plt.colorbar(sc)
plt.show()

画像例

おすすめ記事