2016-12-15 2 views
0

Ich möchte mehrere Farben in einem mit Matplotlib erstellten Marker verwenden. Doing zwei Farben war nicht so schwer, nach this example, und mit einigen zusätzlichen Informationen aus this documentation. Ich frage mich jedoch, ob es möglich ist, einen Marker mit mehr als 2 Farben zu erstellen. Ich bin in einer Situation, wo ich möchte, dass ein einzelner Marker tatsächlich 3 verschiedene Farben bekommt (ein Punkt auf einer Karte bezieht sich auf drei verschiedene Beobachtungen).Mehrere Farben in Matplotlib-Markierungen

+0

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

+0

@ Tom tut das auch mit normalen Plots (plt.plot (..)) arbeiten? –

+0

@tom oder andere Marker –

Antwort

1

Sie können dies durch das matplotlib Beispiel folgende hier gezeigt:

matplotlib.org/examples/api/scatter_piecharts.html

Im Folgenden werde ich das Beispiel etwas ax.plot statt ax.scatter zu verwenden, haben sich geändert.

Grundsätzlich bedeutet dies, alle Marker die gleiche Größe haben müssen, und statt mit dem s kwarg für scatter, verwenden Sie die ms (oder markersize) kwarg für plot.

Außerdem müssen Sie anstelle von facecolormarkerfacecolor definieren.

Abgesehen von diesen Änderungen bleibt alles andere das gleiche wie im ursprünglichen Beispiel.

""" 
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