2016-09-26 16 views
0
import matplotlib.pyplot as plt 
x = [1,2,3,4,5,-6,7,8] 
y = [5,2,4,-2,1,4,5,2] 
plt.scatter(x,y, label='test', color='k', s=25, marker="o") 
plt.xlabel('x') 
plt.ylabel('y') 
plt.title('Test') 
plt.legend() 
plt.show() 
plt.legend() 
plt.show() 

値が負のyの変更の場合iは、色=「R」 を変更しようとしていると負のXの変化iの値が "に「O」=マーカーを変更しようとしている場合バツ"。私はmatplotlibを初めて使っています。2D散布図matplotlibの

追加の質問として、-1と-.5、0.5から0、0から.5、.5から1の範囲に入るxとyの色とマーカーにどのように影響するか2色のマーカーが8種類あります。

答えて

1

numpy.whereを使用して、y値が正または負である場合の表示を取得し、対応する値をプロットすることができます。

import numpy as np 
import matplotlib.pyplot as plt 


x = np.array([1, 2, 3, 4, 5, -6, 7, 8, 2, 5, 7]) 
y = np.array([5, 2, 4, -2, 1, 4, 5, 2, -1, -5, -6]) 
ipos = np.where(y >= 0) 
ineg = np.where(y < 0) 
plt.scatter(x[ipos], y[ipos], label='Positive', color='b', s=25, marker="o") 
plt.scatter(x[ineg], y[ineg], label='Negative', color='r', s=25, marker="x") 
plt.xlabel('x') 
plt.ylabel('y') 
plt.title('Test') 
plt.legend() 
plt.show() 

編集

あなたが同じことを行い

i_opt1 = np.where((y >= 0) & (0 < x) & (x < 3)) # filters out positive y-values, with x-values between 0 and 3 
i_opt2 = np.where((y < 0) & (3 < x) & (x < 6)) # filters out negative y-values, with x between 3 and 6 
plt.scatter(x[i_opt1], y[i_opt1], label='First set', color='b', s=25, marker="o") 
plt.scatter(x[i_opt2], y[i_opt2], label='Second set', color='r', s=25, marker="x") 

として&演算子は(およびオペレータ)で区切ってnp.whereにいくつかの条件を追加することができますすべてのあなたの異なる要件。

Example of multiple conditions

Link to documentation of np.where

+0

コードのおかげで、私は少しそれが軸を追加する改正しました。しかし、私は質問が追加されている、私はこのグラフにマップされなければならない別の変数zを持っていて、それはマーカーのタイプについてのみ言う。 -1

-1

これはAltairは風になりそうです。

import pandas as pd 

x = [1,2,3,4,5,-6,7,8] 
y = [5,2,4,-2,1,4,5,2] 

df = pd.DataFrame({'x':x, 'y':y}) 
df['cat_y'] = pd.cut(df['y'], bins=[-5, -1, 1, 5]) 
df['x>0'] = df['x']>0 
Chart(df).mark_point().encode(x='x',y='y',color='cat_y', shape='x>0').configure_cell(width=200, height=200) 

enter image description here