绘图常用代码&算法总结

做个汇总

Posted by LCY on January 28, 2019

[TOC]

画图

plot参数

线条和点

character description
’-‘ solid line style
’–’ dashed line style
’-.’ dash-dot line style
’:’ dotted line style
’.’ point marker
’,’ pixel marker
‘o’ circle marker
‘v’ triangle_down marker
’^’ triangle_up marker
’<’ triangle_left marker
’>’ triangle_right marker
‘1’ tri_down marker
‘2’ tri_up marker
‘3’ tri_left marker
‘4’ tri_right marker
’s’ square marker
‘p’ pentagon marker
‘*’ star marker
‘h’ hexagon1 marker
‘H’ hexagon2 marker
’+’ plus marker
‘x’ x marker
‘D’ diamond marker
‘d’ thin_diamond marker
’|’ vline marker
‘_’ hline marker

颜色

character color
‘b’ blue
‘g’ green
‘r’ red
‘c’ cyan
‘m’ magenta
‘y’ yellow
‘k’ black
‘w’ white

plot kwargs

plot([1,2,3], [1,2,3], 'go-', label='line 1', linewidth=2)
plot([1,2,3], [1,4,9], 'rs',  label='line 2')
axis([0, 4, 0, 10])
legend()
Property Description
agg_filter unknown
alpha float (0.0 transparent through 1.0 opaque)
animated [True | False]
antialiased or aa [True | False]
axes an Axes instance
clip_box matplotlib.transforms.Bbox instance
clip_on [True | False]
clip_path [ (PathTransform) | Patch | None ]
color or c any matplotlib color
contains a callable function
dash_capstyle [‘butt’ | ‘round’ | ‘projecting’]
dash_joinstyle [‘miter’ | ‘round’ | ‘bevel’]
dashes sequence of on/off ink in points
data 2D array (rows are x, y) or two 1D arrays
drawstyle [ ‘default’ | ‘steps’ | ‘steps-pre’ | ‘steps-mid’ | ‘steps-post’ ]
figure matplotlib.figure.Figure instance
fillstyle [‘full’ | ‘left’ | ‘right’ | ‘bottom’ | ‘top’ | ‘none’]
gid an id string
label string or anything printable with ‘%s’ conversion.
linestyle or ls [ ’-‘ | ’–‘ | ’-.’ | ’:’ | ’None’ | ’ ’ | ’‘ ] and any drawstyle in combination with a linestyle, e.g. ’steps–’.
linewidth or lw float value in points
lod [True | False]
marker [ 7 | 4 | 5 | 6 | ’o’ | ’D’ | ’h’ | ’H’ | ’_‘ | ’‘ | ’None’ | ’ ’ | None | ’8’ | ’p’ | ’,’ | ’+’ | ’.’ | ’s’ | ’‘ | ’d’ | 3 |0 | 1 | 2 | ’1’ | ’3’ | ’4’ | ’2’ | ’v’ | ’<’ | ’>’ | ’^’ | ’|’ | ’x’ | ’$…$’ | tuple | Nx2 array* ]
markeredgecolor or mec any matplotlib color
markeredgewidth or mew float value in points
markerfacecolor or mfc any matplotlib color
markerfacecoloraltor mfcalt any matplotlib color
markersize or ms float
markevery None | integer | (startind, stride)
picker float distance in points or callable pick function fn(artist, event)
pickradius float distance in points
rasterized [True | False | None]
snap unknown
solid_capstyle [‘butt’ | ‘round’ | ‘projecting’]
solid_joinstyle [‘miter’ | ‘round’ | ‘bevel’]
transform matplotlib.transforms.Transform instance
url a url string
visible [True | False]
xdata 1D array
ydata 1D array
zorder any number

坐标轴处理

plot([1,2,3], [1,2,3], 'go-', label='line 1', linewidth=2)
plot([1,2,3], [1,4,9], 'rs',  label='line 2')
axis([0, 4, 0, 10])
legend()

坐标刻度修改

plt.xticks([])
plt.yticks([])

坐标轴取值范围

plt.xlim((-1, 1))
plt.ylim((0, 2))

关闭坐标轴

plt.axis('off')

显示坐标轴网格

plt.grid(True)

非线性坐标轴

对数坐标轴

plt.subplot(2,2,1)
plt.plot(x,y)
plt.yscale('log')
plt.title('log')
plt.grid(True)

symlog坐标轴

plt.subplot(2,2,1)
plt.plot(x,y)
plt.yscale('symlog')
plt.title('symlog')
plt.grid(True)

logit坐标轴

plt.subplot(2,2,1)
plt.plot(x,y)
plt.yscale('logit')
plt.title('logit')
plt.grid(True)

example

线条、图例和标题

箭头

plt.arrow(x,y, dx, dy,fc="k", ec="k",
                              head_width=0.05, head_length=0.2 )

字号设置

ax = plt.subplot(111)
# 设置刻度字体大小
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
# 设置坐标标签字体大小
ax.set_xlabel(..., fontsize=20)
ax.set_ylabel(..., fontsize=20)
# 设置图例字体大小
ax.legend(..., fontsize=20)

乱码问题

myfont = matplotlib.font_manager.FontProperties(fname='C:/Windows/Fonts/msyh.ttf')  #微软雅黑字体
plt.legend(loc='upper left',prop={'size':6})
plt.title(u'你',fontproperties=myfont) #指定字体  
plt.xlabel(u'我',fontproperties=myfont) 

图例

# 绘制普通图像
x = np.linspace(-1, 1, 50)
y1 = 2 * x + 1
y2 = x**2

plt.figure()
# 在绘制时设置lable, 逗号是必须的
l1, = plt.plot(x, y1, label = 'line')
l2, = plt.plot(x, y2, label = 'parabola', color = 'red', linewidth = 1.0, linestyle = '--')

# 设置坐标轴的取值范围
plt.xlim((-1, 1))
plt.ylim((0, 2))

# 设置坐标轴的lable
plt.xlabel('X axis')
plt.ylabel('Y axis')

# 设置x坐标轴刻度, 原来为0.25, 修改后为0.5
plt.xticks(np.linspace(-1, 1, 5))
# 设置y坐标轴刻度及标签, $$是设置字体
plt.yticks([0, 0.5], ['$minimum$', 'normal'])

# 设置legend
plt.legend(handles = [l1, l2,], labels = ['a', 'b'], loc = 'best')
plt.show()

线条粗细

plt.plot(x, y, linewidth=2.0)  

其他类图

热力图

import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import LogNorm
import numpy as np
x, y = np.random.rand(10), np.random.rand(10)
z = (np.random.rand(9000000)+np.linspace(0,1, 9000000)).reshape(3000, 3000)
plt.imshow(z+10, extent=(np.amin(x), np.amax(x), np.amin(y), np.amax(y)),
        cmap=cm.hot, norm=LogNorm())
plt.colorbar()
plt.show()

from pylab import *

def f(x,y): return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2)

n = 256
x = np.linspace(-3,3,n)
y = np.linspace(-3,3,n)
X,Y = np.meshgrid(x,y)

contourf(X, Y, f(X,Y), 8, alpha=.75, cmap='jet')
C = contour(X, Y, f(X,Y), 8, colors='black', linewidth=.5)
show()

矩阵图

#-*- coding:utf-8 -*-
from pylab import *

#使用中文
mpl.rcParams['font.sans-serif'] = ['SimHei']
#显示负号
matplotlib.rcParams['axes.unicode_minus'] = False

n=32
list1=[i for i in range(1,33)]
list2=[i for i in range(-32,0)]
n= np.arange(n)
xlim(-1,32)
ylim(-35,35)
xlabel(u'每个城市招聘人数')
bar(n, list1, facecolor='yellow', edgecolor='white')
bar(n, list2, facecolor='red', edgecolor='white')
for x,y in zip(n,list1):
    text(x, y, '%d' % y, ha='center', va= 'bottom' )
for x,y in zip(n,list2):
    text(x, y-3, '%d' % y, ha='center', va= 'bottom')
show()

饼图

import matplotlib.pyplot as plt

labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'  # 设置标签
sizes = [15, 30, 45, 10]  # 占比,和为100
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']  # 颜色
explode = (0, 0.1, 0, 0)  # 展开第二个扇形,即Hogs,间距为0.1

plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True,
        startangle=90)  # startangle控制饼状图的旋转方向
plt.axis('equal')  # 保证饼状图是正圆,否则会有一点角度偏斜

plt.show()

3D

Matplotlib Toolkits:三维绘图工具包matplotlib.mplot3d

误差图

误差阴影

from matplotlib import pyplot as pl
import numpy as np

x = np.linspace(0, 30, 30)
y = np.sin(x/6*np.pi)
error = np.random.normal(0.1, 0.02, size=y.shape)
y += np.random.normal(0, 0.1, size=y.shape)

pl.plot(x, y, 'k-')
pl.fill_between(x, y-error, y+error)
pl.show()

误差曲线(errorbar)

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(1,10,20)
dy=0.6
y=np.sin(x)*3

plt.errorbar(x,y,yerr=dy,fmt='o',ecolor='r',color='b',elinewidth=2,capsize=4)
plt.show()

算法

时间序列法

神经网络

SVM

  • SVM ```python from sklearn import svm

X = [[0, 0], [1, 1], [1, 0]] # training samples
y = [0, 1, 1] # training target
clf = svm.SVC() # class
clf.fit(X, y) # training the svc model

result = clf.predict([2, 2]) # predict the target of testing samples
print result # target

print clf.support_vectors_ #support vectors

print clf.support_ # indeices of support vectors

print clf.n_support_ # number of support vectors for each class

- SVR
```python
X = [[0, 0], [1, 1]]  
y = [0.5, 1.5]   
clf = svm.SVR()   
clf.fit(X, y)  
result = clf.predict([2, 2])   
print result   

决策树&随机森林