1. 程式人生 > >原 matplotlib散點scatter學習2,引數測試(marker1)

原 matplotlib散點scatter學習2,引數測試(marker1)

續上篇
繪製散點圖的函式,x,y分別對應點的x軸座標和y軸座標
plt.scatter(x,y)
matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None,
cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None,
verts=None, edgecolors=None, , data=None, **kwargs)[source]
繼續測試marker,實際上marker涉及的內容比想象的複雜,marker 定義:
markers = {
‘.’: ‘point’,
‘,’: ‘pixel’,
‘o’: ‘circle’,
‘v’: ‘triangle_down’,
‘^’: ‘triangle_up’,
‘<’: ‘triangle_left’,
‘>’: ‘triangle_right’,
‘1’: ‘tri_down’,
‘2’: ‘tri_up’,
‘3’: ‘tri_left’,
‘4’: ‘tri_right’,
‘8’: ‘octagon’,
‘s’: ‘square’,
‘p’: ‘pentagon’,
'

’: ‘star’,
‘h’: ‘hexagon1’,
‘H’: ‘hexagon2’,
‘+’: ‘plus’,
‘x’: ‘x’,
‘D’: ‘diamond’,
‘d’: ‘thin_diamond’,
‘|’: ‘vline’,
‘_’: ‘hline’,
‘P’: ‘plus_filled’,
‘X’: ‘x_filled’,
TICKLEFT: ‘tickleft’,
TICKRIGHT: ‘tickright’,
TICKUP: ‘tickup’,
TICKDOWN: ‘tickdown’,
CARETLEFT: ‘caretleft’,
CARETRIGHT: ‘caretright’,
CARETUP: ‘caretup’,
CARETDOWN: ‘caretdown’,
CARETLEFTBASE: ‘caretleftbase’,
CARETRIGHTBASE: ‘caretrightbase’,
CARETUPBASE: ‘caretupbase’,
CARETDOWNBASE: ‘caretdownbase’,
“None”: ‘nothing’,
None: ‘nothing’,
’ ': ‘nothing’,
‘’: ‘nothing’
}
可以看出marker實際是一個dict,補充定義
0 (TICKLEFT) m25 tickleft
1 (TICKRIGHT) m26 tickright
2 (TICKUP) m27 tickup
3 (TICKDOWN) m28 tickdown
4 (CARETLEFT) m29 caretleft
5 (CARETRIGHT) m30 caretright
6 (CARETUP) m31 caretup
7 (CARETDOWN) m32 caretdown
8 (CARETLEFTBASE) m33 caretleft (centered at base)
9 (CARETRIGHTBASE) m34 caretright (centered at base)
10 (CARETUPBASE) m35 caretup (centered at base)
11 (CARETDOWNBASE) m36 caretdown (centered at base)
還是看程式碼:

a,b=plt.subplots(2,2) 
col=['c', 'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'] #定義顏色

marke1=['.', ',', 'o', 'v', '^', '<', '>', '1', '2', '3', '4', '8'] #形狀1
marke2=[ 's', 'p', 'P', '*', 'h', 'H', '+', 'x', 'X', 'D', 'd', '|', '_'] #形狀2
x=2; y=3

for i,j in enumerate(marke1):   #mark形狀1
    b[0,0].scatter(x+i, y+2*i, s=(i+1)*8, marker=j, c=col[i % len(col)]) #這個測試字元

for i,j in enumerate(marke2):   #marke不能作為list輸入
    b[0,1].scatter(x+i, y+2*i, s=(i+1)*8, marker=j, c=col[i % len(col)]) #這個測試字元
for i in range(6):
    b[1, 0].scatter(x * (i + 1), y * (i + 1), s=(i+1)*10, marker=i, c=col[i % len(col)]) #這個測試整數
    b[1, 1].scatter(x * (i + 1), y * (i + 1), s=(i + 1) * 10, marker=i+6, c=col[i % len(col)])  # 這個測試整數

plt.show()

執行結果如下在這裡插入圖片描述
執行結果如上。
下面這段程式碼可以看到所有定義的線型 引用https://matplotlib.org/gallery/lines_bars_and_markers/marker_reference.html

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D


points = np.ones(4)  # Draw 3 points for each line
text_style = dict(horizontalalignment='right', verticalalignment='center',
                  fontsize=12, fontdict={'family': 'monospace'})
#text_style ={'horizontalalignment': 'right', 'verticalalignment':
# 'center', 'fontsize': 12, 'fontdict': {'family': 'monospace'}}

marker_style = dict(linestyle=':', color='0.8', markersize=10,
                    mfc="C0", mec="C0")
#marker_style={'linestyle': ':', 'color': '0.8', 'markersize': 10, 'mfc': 'C0', 'mec': 'C0'}


def format_axes(ax):
    ax.margins(0.2)
    ax.set_axis_off()
    ax.invert_yaxis()


def nice_repr(text):
    return repr(text).lstrip('u')


def math_repr(text):
    tx = repr(text).lstrip('u').strip("'").strip("$")
    return r"'\${}\$'".format(tx)


def split_list(a_list):
    i_half = len(a_list) // 2
    return (a_list[:i_half], a_list[i_half:])

fig, axes = plt.subplots(ncols=2)

fig.suptitle('un-filled markers', fontsize=14)

# Filter out filled markers and marker settings that do nothing.

Myt=[]
MyK=[]
for m,func in  Line2D.markers.items():
    Myt.append(m)
    MyK.append(func)
#print(Line2D.filled_markers) #('o', 'v', '^', '<', '>', '8', 's', 'p', '*', 'h', 'H', 'D', 'd', 'P', 'X')
#print(Myt) #['.', ',', 'o', 'v', '^', '<', '>', '1', '2', '3', '4', '8', 's', 'p', '*', 'h', 'H', '+', 'x',
#  'D', 'd', '|', '_', 'P', 'X', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 'None', None, ' ', '']

unfilled_markers = [m for m, func in Line2D.markers.items()
                    if m in Line2D.filled_markers]   
                     #('o', 'v', '^', '<', '>', '8', 's', 'p', '*', 'h', 'H', 'D', 'd', 'P', 'X')
                # if func != 'nothing' and and m not in Line2D.filled_markers 這一               
                #行可以看到其它的線型,太多一張圖放不下    
                    # 條件:m, func in Line2D.markers.items() 這樣用
x=[m for m,y in [(1,2),("x","y")]]
#print(x) #[1, 'x']

for ax, markers in zip(axes, split_list(unfilled_markers)):
    print(zip(axes, split_list(unfilled_markers)))
    for y, marker in enumerate(markers):
        ax.text(-1, y*2, nice_repr(marker), **text_style)  #ax.text(-0.5, y, nice_repr(marker), **text_style)
        ax.plot((y*2) * points, marker=marker, **marker_style)
        format_axes(ax)
plt.show()

執行結果如下在這裡插入圖片描述
在這裡插入圖片描述
下面再看這個例子marker(i,j) j<4 三種形狀,i指齒數5角或6角

np.random.seed(19680801)
x = np.arange(0.0, 20.0, 2.0)
y = x ** 1.3 + np.random.rand(*x.shape) * 30.0
s = np.random.rand(*x.shape) * 800 + 500
plt.subplot(211)
col=['c', 'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'] #定義顏色
for i in range(1,4):
    for j in range(1,9):
        plt.scatter((10*i),(j*10), s=80, c=col[j % len(col)], marker=(j, i))

plt.subplot(212)
plt.scatter(x, y, s=(x+1)*20, c=col, alpha=0.5, marker=r'$\clubsuit$',
            label="Luck")
plt.show()

在這裡插入圖片描述