2013-01-14 66 views
20

所以有這個指南: http://matplotlib.org/examples/pylab_examples/scatter_symbol.html ​​Matplotlib定製標記/符號

# http://matplotlib.org/examples/pylab_examples/scatter_symbol.html 
from matplotlib import pyplot as plt 
import numpy as np 
import matplotlib 

x = np.arange(0.0, 50.0, 2.0) 
y = x ** 1.3 + np.random.rand(*x.shape) * 30.0 
s = np.random.rand(*x.shape) * 800 + 500 

plt.scatter(x, y, s, c="g", alpha=0.5, marker=r'$\clubsuit$', 
      label="Luck") 
plt.xlabel("Leprechauns") 
plt.ylabel("Gold") 
plt.legend(loc=2) 
plt.show() 

但如果你像我一樣,不想使用clubsuit標誌...

如何你做自己的記號嗎?

UPDATE

我喜歡這個特殊的標記類型是很容易用簡單的matplotlib語法調整:

from matplotlib import pyplot as plt 
import numpy as np 
import matplotlib 

x = np.arange(0.0, 50.0, 2.0) 
y = x ** 1.3 + np.random.rand(*x.shape) * 30.0 
s = np.random.rand(*x.shape) * 800 + 500 

plt.plot(x, y, "ro", alpha=0.5, marker=r'$\clubsuit$', markersize=22) 
plt.xlabel("Leprechauns") 
plt.ylabel("Gold") 
plt.show() 

enter image description here

+2

,你會不得不看看[這裏](http://stackoverflow.com/q/2318288/1025391)? – moooeeeep

+0

是的,我其實有。但它沒有爲我工作。我喜歡matplotlib示例代碼是'c =「g」',我將其解釋爲該圖的顏色調整(在寫作時沒有測試它的python shell)。 – Norfeldt

回答

37

於是發現,這是隻是使用mathtext符號,而不是指存儲在matplotlib模塊中的任何特殊的基於矢量的標記...

from matplotlib import pyplot as plt 
import numpy as np 
from numpy.random import randint 
import matplotlib 

x = np.arange(0.0, 100.0, 2.0) 
y = x ** 1.3 + np.random.rand(*x.shape) * 30.0 
s = np.random.rand(*x.shape) * 800 + 500 

markers = ['\\alpha', '\\beta', '\gamma', '\sigma','\infty', \ 
      '\spadesuit', '\heartsuit', '\diamondsuit', '\clubsuit', \ 
      '\\bigodot', '\\bigotimes', '\\bigoplus', '\imath', '\\bowtie', \ 
      '\\bigtriangleup', '\\bigtriangledown', '\oslash' \ 
      '\ast', '\\times', '\circ', '\\bullet', '\star', '+', \ 
      '\Theta', '\Xi', '\Phi', \ 
      '\$', '\#', '\%', '\S'] 

def getRandomMarker(): 
    return "$"+markers[randint(0,len(markers),1)]+"$" 

def getMarker(i): 
    # Use modulus in order not to have the index exceeding the lenght of the list (markers) 
    return "$"+markers[i % len(markers)]+"$" 

for i, mi in enumerate(markers): 
    plt.plot(x[i], y[i], "b", alpha=0.5, marker=getRandomMarker(), markersize=randint(16,26,1)) 
    plt.plot(x[i], y[i]+50, "m", alpha=0.5, marker=getMarker(i), markersize=randint(16,26,1)) 
    # Let's see if their "center" is located where we expect them to be... 
    plt.plot(x[i], y[i]+100, "y", alpha=0.5, marker=getMarker(i), markersize=24) 
    plt.plot(x[i], y[i]+100, "k+", markersize=12, markeredgewidth=2) 

plt.xlabel("x-axis") 
plt.ylabel("y-axis") 
plt.xlim(-5, plt.xlim()[1]+5) 
plt.ylim(0, plt.ylim()[1]*1.1) 
plt.gcf().set_size_inches(12,6) 
plt.show() 

Check Image

+7

我試圖找到一個'心滿意足',因爲\心形套裝只是一個輪廓。使用unicode標記爲我工作:'marker = ur'$ \ u2665 $'' – patricksurry

+0

對於那些有興趣刪除正常字母/單詞的斜體的人,可以使用'\ mathrm'或'\ rm' mathtext命令。例如。 'marker = r'$ \ mathrm {M} $''爲M. – Andy