2017-04-03 7 views
0

以下は、matplotlibの散布図を使用して添付画像を生成する少量のコードです。 Output image from code.Matplotlib散布図点サイズ凡例

私はいくつかの点のサイズと対応する "z値"を示す "凡例"を得ようとしています。

これを自分で構築するのは簡単ではありませんが、このようなものはありますか?カラーバーの「サイズ」アナロジー?あなたは伝説を取得するために

import matplotlib.pyplot as plt 
import numpy as np 

fig = plt.figure(figsize=(8,6)) 
inset = fig.add_subplot(111) 

np.random.seed(0) # so the image is reproducible 
x1 = np.random.rand(30) 
y1 = np.random.rand(30) 
z1 = np.random.rand(30) 


axis = inset.scatter(x1,y1,s=z1*100,c=z1,vmin=0,vmax=1) 

inset.set_xlabel("X axis") 
inset.set_ylabel("Y axis") 

cbar = fig.colorbar(axis,ticks=[0,0.5,1]) 
cbar.ax.set_yticklabels(["Low","Medium","High"]) 

plt.savefig('scatterplot-zscale.png',bbox_inches='tight') 

答えて

1

は、あなたは、少なくとも1つのデータポイントにscatterを呼び出すとき、キーワードlabelを渡す必要があります。 1つの方法は、データから3つの代表点を選択し、ラベルを付けてプロットに再び追加することです。

import matplotlib.pyplot as plt 
import numpy as np 

np.random.seed(0) # so the image is reproducible 
x1 = np.random.rand(30) 
y1 = np.random.rand(30) 
z1 = np.random.rand(30) 

fig = plt.figure(figsize=(8,6)) 
inset = fig.add_subplot(111) 
# i prefer no outlines on the dots, so edgecolors='none' 
axis = inset.scatter(x1,y1,s=z1*100,c=z1,vmin=0,vmax=1,edgecolors='none') 

inset.set_xlabel("X axis") 
inset.set_ylabel("Y axis") 

cbar = fig.colorbar(axis,ticks=[0,0.5,1]) 
cbar.ax.set_yticklabels(["Low","Medium","High"]) 

# here we step over the sorted data into 4 or 5 strides and select the 
# last 3 steps as a representative sample, this only works if your 
# data is fairly uniformly distributed 
legend_sizes = np.sort(z1)[::len(z1)//4][-3:] 

# get the indices for each of the legend sizes 
indices = [np.where(z1==v)[0][0] for v in legend_sizes] 

# plot each point again, and its value as a label 
for i in indices: 
    inset.scatter(x1[i],y1[i],s=100*z1[i],c=z1[i], vmin=0,vmax=1,edgecolors='none', 
        label='{:.2f}'.format(z1[i])) 
# add the legend 
inset.legend(scatterpoints=1) 

enter image description here

+0

は素晴らしい作品。ありがとうございました! –