0%

绘图的线型、颜色、标记

绘图:.plot()

plt.plot():绘制点和线[1]

1
2
from numpy.random import randn
plt.plot(randn(50).cumsum(),'k--') # 生成50个正态分布的数
  • randn函数返回一个或一组样本,具有标准正态分布;
  • cumsum函数计算各行的累加值[2]
  • in:
1
2
3
import numpy as np
a = np.array([1,2,3,4,5,6])
a.cumsum()

out:

1
array([ 1, 3, 6, 10, 15, 21])

k-- 是线性选项,指定黑色虚线;

颜色(color),标记(marker),线型(linestyle)

指定绿色虚线:

1
2
ax.plot(x,y,'g--')  # 一样的效果,更简洁
ax.plot(x,y,linestyle='--',color='g')

指定黑色虚线,标记类型:

1
2
plt.plot(randn(50).cumsum(),'ko--')
plt.plot(randn(50).cumsum(),color='k',linestyle='dashed',marker='o')

用字符串指定时,颜色一定在最前面,可规范顺序:color+marker+line style

Color[3]

Long Name Short Name RGB Triplet
blue b [0,0,1]
black k [0,0,0]
red r [1,0,0]
green g [0,1,0]
yellow y [1,1,0]
cyan c [0,1,1]
magenta m [1,0,1]
white w [1,1,1]

Marker[4]

Marker Description Resulting Marker
"o" Circle
"+" Plus sign
"*" Asterisk
"." Point
"x" Cross
"_" Horizontal line
" | " Vertical line
"square" Square
"diamond" Diamond
"^" Upward-pointing triangle
"v" Downward-pointing triangle
">" Right-pointing triangle
"<" Left-pointing triangle
"pentagram" Pentagram
"hexagram" Hexagram

Line Style[4]

Line Style Description Resulting Line
"-" Solid line
"- -" Dashed line
":" Dotted line
"-." Dash-dotted line

Draw Style:在线型图中,可按非线性插值;

1
2
plt.plot(randn(50),'ko--',drawstyle='steps-post',label='steps-post')  
plt.legend(loc="best") # 给图像加图例

目前有三种可选:steps、steps-mid、steps-post 具体差别还待探究;

该图出处:https://matplotlib.org/stable/gallery/lines_bars_and_markers/step_demo.html

参考文章
+