matplotlib scatter-plot mit verschiedenen Marken und Farben
Ich würde gerne einen plot mit verschiedenen Marken und verschiedenen Farben entsprechend die Werte von 2 externen Vektoren.
Hier was ich versucht habe:
>>> s = [u'+', u'+', u'o']
>>> col = ['r','r','g']
>>> x = np.array([1,2,3])
>>> y = np.array([4,5,6])
>>> pl.scatter(x,y,marker=s,c=col)
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/matplotlib/markers.py", line 233, in set_marker
Path(marker)
File "/usr/lib/python3/dist-packages/matplotlib/path.py", line 133, in __init__
vertices = np.asarray(vertices, np.float_)
File "/usr/lib/python3/dist-packages/numpy/core/numeric.py", line 460, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float: '+'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 3087, in scatter
linewidths=linewidths, verts=verts, **kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/axes.py", line 6298, in scatter
marker_obj = mmarkers.MarkerStyle(marker)
File "/usr/lib/python3/dist-packages/matplotlib/markers.py", line 162, in __init__
self.set_marker(marker)
File "/usr/lib/python3/dist-packages/matplotlib/markers.py", line 236, in set_marker
raise ValueError('Unrecognized marker style {}'.format(marker))
ValueError: Unrecognized marker style ['+', '+', 'o']
>>>
BEARBEITEN:
Die vorgeschlagene Lösung funktioniert im simulierten Beispiel, aber es funktioniert nicht auf mein eigentliches problem. (Ich weiß nicht, warum).
Hier mein vollständiger code :
import matplotlib.pyplot as pl
import numpy as np
locY = np.linspace(0, 1, 50) #in the real data locX and locY are coordinate of the points
locX = np.linspace(0,1,50)
p = 50
C = {0: [0, 13, 14, 19, 22, 30], 16: [1, 16, 17], 20: [15, 18, 20, 24], 37: [4, 8, 9, 33, 37, 40, 47], 41: [5, 7, 28, 32, 34, 36, 39, 41, 42, 44, 46], 26: [6, 26, 27, 29, 31, 35], 45: [10, 11, 12, 38, 43, 45, 48, 49], 21: [2, 3, 21, 23, 25]}
index = C.keys()
def plot_cluster(C, index):
color_map = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'w']
colors = []
m_type = []
for i in range(p):
for pos, k in enumerate(index):
if i in C[k]:
#colors.append(pos)
colors.append(color_map[pos])
if i == k:
m_type.append(u'+')
else:
m_type.append(u'o')
fig = pl.scatter(locX, locY, c=colors, s=30, cmap='spectral')
fig = pl.locator_params(nbins=3)
fig = pl.xlim(-0.2, 1.2)
fig = pl.ylim(-0.2, 1.2)
fig = pl.locator_params(nbins=5, axis='y')
return fig
pl.show(plot_cluster(C, index))
Wenn ich die Stelle des Zeichnens Teil mit den vorgeschlagenen Antwort, die ich erhalten nur 2 Punkte in der Handlung (ich weiß nicht, warum)
Wie kann ich das Problem lösen?
InformationsquelleAutor Donbeo | 2014-10-21
Du musst angemeldet sein, um einen Kommentar abzugeben.
Dies funktioniert:
Rendering wie diese:
Update
Es scheint, dass Sie haben eine Vielzahl von Farben und haben einen einzigen Aufruf der scatter-Funktion: Beispiel. Die mehrere Farbe-Funktion bestätigt wird, die API aber es nicht Lesen kann, können Sie eine iterierbar für die Marke kwarg. Dein code funktioniert, wenn Sie entfernen
marker=s
Wenn ich versuche zu laufen, dass code, bekomme ich die Fehlermeldung "AttributeError: 'module' Objekt hat kein Attribut 'show'", d.h. ein problem mit der letzten Zeile. Können Sie das nochmal überprüfen
es funktioniert für mich. Seien Sie vorsichtig mit den Einrückungen und den import matplotlib.pyplot as pl und import numpy as np am Anfang
Die pyplot import erfolgt in der Regel als
plt
, nichtpl
.InformationsquelleAutor Noel Evans