2013-01-14 33 views
20

Vì vậy, hướng dẫn này: http://matplotlib.org/examples/pylab_examples/scatter_symbol.html enter image description hereMatplotlib tùy chỉnh marker/biểu tượng

# 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() 

Nhưng nếu bạn đang như tôi và không muốn sử dụng một dấu clubsuit ...

thế nào bạn có tạo điểm đánh dấu riêng cho mình không?

CẬP NHẬT

Những gì tôi thích về loại marker đặc biệt này là nó dễ dàng để điều chỉnh với cú pháp matplotlib đơn giản:

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

đã bạn đã có một cái nhìn tại [này] (http://stackoverflow.com/q/2318288/1025391)? – moooeeeep

+0

Có, tôi thực sự có. Nhưng nó không hiệu quả với tôi. Những gì tôi thích về mã ví dụ matplotlib là 'c = "g"' mà tôi giải thích như điều chỉnh màu cho cốt truyện (không có một vỏ python trong thời điểm viết để kiểm tra nó). – Norfeldt

Trả lời

37

Vì vậy, phát hiện ra rằng đó là chỉ sử dụng ký hiệu mathtext và không đề cập đến bất kỳ điểm đánh dấu vector đặc biệt nào được lưu trữ trong mô-đun 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

Tôi đã cố gắng tìm một 'trái tim đầy' vì \ heartsuit chỉ là một phác thảo. Sử dụng một điểm đánh dấu unicode làm việc cho tôi: 'marker = ur '$ \ u2665 $' ' – patricksurry

+0

Đối với những người quan tâm đến việc loại bỏ chữ nghiêng trên các chữ cái/từ thông thường, sử dụng lệnh' \ mathrm' hoặc '\ rm' mathtext hoạt động. Ví dụ. 'marker = r '$ \ mathrm {M} $'' cho một M. – Andy

Các vấn đề liên quan