Reference address: matplot official documentation
call method
from matplotlib import pyplot as plt # 引入库
# %matplotlib inline 嵌入内部
# %matplotlib 跳出交互
plt.plot([x], y, [fmt], data=None, **kwargs)
plt.plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)
easy to use
from matplotlib import pyplot as plt
y = [1,2,3,2,1]
plt.plot(y) # 绘制y坐标,x坐标使用列表0..N-1
x = [1, 1.1, 1.2, 1.3, 1.4]
y = [1,2,3,2,1]
plt.plot(x, y, 'bo') # 使用蓝色(blue)、圆点型绘图, x为横坐标, y为纵坐标
Color Abbreviation:
Character color
'b' blue blue
'g' green green
'r' red red
'c' cyan cyan
'm' magenta magenta
'y' yellow yellow
'k' black black
'w' white white
The marker abbreviation is as follows:
Character description
'.' point marker point
'o' circle marker circle
'v' triangle_down marker lower triangle
'^' triangle_up marker upper triangle
'<' triangle_left marker left triangle
'>' triangle_right marker right triangle
'1' tri_down marker
'2 ' tri_up marker
'3' tri_left marker
'4' tri_right marker
's' square marker square
'p' pentagon marker pentagon
'*' star marker star
'h' hexagon1 marker hexagon 1
'H' hexagon2 marker hexagon 2
'+ ' plus marker plus sign'x
' x marker × type
'|' vline marker vertical line type'_
' hline marker horizontal line type
The line abbreviation is as follows:
Character description'-
' solid line style solid line'–
' dashed line style dashed line'-
.' dash-dot line style interlaced dotted line
':' dotted line style dotted line
plt.grid() # add grid
plt.legend() # display legend
One command to draw three lines
import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0., 5., 0.4)
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()
plt.scatter(x,y) #绘制散点图
plt.show()
Jupyter uses plt to draw pictures to display Chinese
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = [u'SimHei']
plt.rcParams['axes.unicode_minus'] = False
#设置字体大小
plt.rcParams.update({
"font.size":20})#此处必须添加此句代码方可改变标题字体大小
or
plt.xlabel(u"时间")
draw multiple subplots
## 一次画四张图 demo scatter(x,y)
plt.rcParams['font.sans-serif'] = [u'SimHei'] # 可显示中文
plt.rcParams['axes.unicode_minus'] = False
plt.figure(figsize=(15,10)) # 设定整个画布的尺寸
plt.subplot(2,2,1)
plt.title("1月")
plt.scatter(data_01['WTUR_WSpd_Ra_F32'], data_01['WTUR_PwrAt_Ra_F32'])
plt.subplot(2,2,2)
plt.title("2月")
plt.scatter(data_02['WTUR_WSpd_Ra_F32'], data_02['WTUR_PwrAt_Ra_F32'])
plt.subplot(2,2,3)
plt.title("3月")
plt.scatter(data_03['WTUR_WSpd_Ra_F32'], data_03['WTUR_PwrAt_Ra_F32'])
plt.subplot(2,2,4)
plt.title("4月")
plt.scatter(data_04['WTUR_WSpd_Ra_F32'], data_04['WTUR_PwrAt_Ra_F32'])
plt.show()
Set horizontal and vertical coordinate scale
import numpy as np
import matplotlib.pyplot as plt
x = range(1,13,1)
y = range(1,13,1)
plt.plot(x,y)
plt.xticks(x)
plt.show()
Set horizontal axis scale to non-numeric
import numpy as np
import matplotlib.pyplot as plt
x = range(2,10,2)
y = range(1,5,1)
x_new = ["aaa", "bbb", "ccc", "ddd"]
plt.plot(x,y)
plt.xticks(x, x_new) #设置横轴刻度
plt.gcf().autofmt_xdate() # 自动旋转横坐标
plt.show()
# 如下图, 可以发现横坐标已更改
Equally spaced plots
import numpy as np
import matplotlib.pyplot as plt
x = range(1,5,1)
y = range(1,5,1)
plt.plot(x,y)
plt.xticks(x[::2],["aa","22","33","44"][::2])
plt.show()
Adjust the margins between plots when drawing multiple subplots
plt.subplots_adjust(left=None,bottom=None,right=None,top=None,wspace=0.15,hspace=0.15)
# 主要是wspace(宽),hspace=0.15(高)
Draw a heat map of a 10*10 matrix
from matplotlib import cm
# 产生10*10维矩阵
a = np.random.uniform(0.5, 1.0, 100).reshape([10,10])
# 绘制热力图
from matplotlib import cm
plt.imshow(a, interpolation='nearest', cmap=cm.coolwarm, origin='lower')
plt.colorbar(shrink=.92)
plt.xticks(())
plt.yticks(())
plt.show()
Axes are not visible
frame = plt.gca()
# y 轴不可见
frame.axes.get_yaxis().set_visible(False)
# x 轴不可见
frame.axes.get_xaxis().set_visible(False)
matrix display
from matplotlib import cm
import matplotlib.pyplot as plt
X = [[0.4829, 0.4680, 0.4853]]
#X = [[1, 2], [3, 4], [5, 6.6]]
plt.imshow(X)
plt.colorbar(cax=None, ax=None, shrink=0.5)
plt.xticks([0,1,2], ['long','middle','short'])