2016-12-15 64 views
0

我想在使用matplotlib制作的标记中使用多种颜色。做两种颜色并不困难,跟在this example之后,还有一些来自this documentation的其他信息。但是,我想知道是否可以制作超过2种颜色的标记。我处于这样一种情况,我希望单个标记实际得到3种不同的颜色(地图上的一个点指的是三种不同的观察结果)。Matplotlib标记中的多种颜色填充

+0

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

+0

@汤姆确实也有正常的地块(plt.plot(..))工作? –

+0

@tom或其他标记 –

回答

1

您可以按照此处显示的matplotlib例子做到这一点:

matplotlib.org/examples/api/scatter_piecharts.html

下面我已经改变了例子稍微用ax.plot,而不是ax.scatter

基本上这意味着所有的标记必须具有相同的大小和,而不是使用s kwarg为scatter,您使用ms(或markersize)kwarg为plot

此外,而不是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