2016-12-15 8 views
0

matplotlibで作成したマーカーで複数の色を使用したいと思います。 this exampleに続いて、2つの色を行うことはそれほど難しくありませんでした。また、this documentationからの追加情報もあります。しかし、2色以上のマーカーを作ることが可能かどうかは疑問でした。私は実際に3つの異なる色を得るために単一のマーカーを必要とする状況にあります(マップ上のポイントは3つの異なる観察を指します)。あなたがここに示さmatplotlibの例以下でこれを行うことができmatplotlibマーカーの複数の色の塗りつぶし

+0

http://matplotlib.org/examples/api/scatter_piecharts.html – tom

+0

@tomことも、通常のプロット(plt.plot(..))で動作しますか? –

+0

@tomまたは他のマーカー –

答えて

1

:I以下

matplotlib.org/examples/api/scatter_piecharts.html

ax.plot代わりのax.scatterを使用するために少しの例を変更しました。

基本的にこれは、すべてのマーカーが同じ大きさを持っていなければならないことを意味し、代わりにscatterためs kwargを使用して、あなたはplotためms(またはmarkersize)kwargを使用しています。

また、facecolorの代わりにmarkerfacecolorを定義する必要があります。

これらの変更以外は、それ以外はすべて元の例と同じです。

""" 
This example makes custom 'pie charts' as the markers for a scatter plot 

Thanks to Manuel Metz for the example 
""" 
import math 
import numpy as np 
import matplotlib.pyplot as plt 

# first define the ratios 
r1 = 0.2  # 20% 
r2 = r1 + 0.4 # 40% 

# define some sizes of the plot marker 
markersize = 20 # I changed this line 

# calculate the points of the first pie marker 
# 
# these are just the origin (0,0) + 
# some points on a circle cos,sin 
x = [0] + np.cos(np.linspace(0, 2*math.pi*r1, 10)).tolist() 
y = [0] + np.sin(np.linspace(0, 2*math.pi*r1, 10)).tolist() 

xy1 = list(zip(x, y)) 
s1 = max(max(x), max(y)) 

# ... 
x = [0] + np.cos(np.linspace(2*math.pi*r1, 2*math.pi*r2, 10)).tolist() 
y = [0] + np.sin(np.linspace(2*math.pi*r1, 2*math.pi*r2, 10)).tolist() 
xy2 = list(zip(x, y)) 
s2 = max(max(x), max(y)) 

x = [0] + np.cos(np.linspace(2*math.pi*r2, 2*math.pi, 10)).tolist() 
y = [0] + np.sin(np.linspace(2*math.pi*r2, 2*math.pi, 10)).tolist() 
xy3 = list(zip(x, y)) 
s3 = max(max(x), max(y)) 

fig, ax = plt.subplots() 

# Here's where I made changes 
ax.plot(np.arange(3), np.arange(3), marker=(xy1, 0), 
      ms=markersize, markerfacecolor='blue') # I changed this line 
ax.plot(np.arange(3), np.arange(3), marker=(xy2, 0), 
      ms=markersize, markerfacecolor='green') # I changed this line 
ax.plot(np.arange(3), np.arange(3), marker=(xy3, 0), 
      ms=markersize, markerfacecolor='red') # I changed this line 


plt.margins(0.05) 

plt.show() 

enter image description here

関連する問題