plt.plot绘图设置
绘图第一步,先导入库。
import matplotlib.pyplot as plt
先讲一些基础知识,后面会给出优秀示例,码住一起学习📚,未完待续……
画布设置
一般情况下,绘图前先设置画布大小plt.figure(figsize=(10, 5))
matplotlib.pyplot.figure(num=None, figsize=None, dpi=None, *, facecolor=None, edgecolor=None, frameon=True, FigureClass=<class 'matplotlib.figure.Figure'>, clear=False, **kwargs)
- frameon
图例是否有边框。
横纵坐标和标题设置
一般情况下,一幅好看的统计图表,都需要给它命名,我将介绍Python中的plt.xlabel,plt.ylabel,plt.title
。
plt.xlabel()
matplotlib.pyplot.xlabel(xlabel, fontdict=None, labelpad=None, *, loc=None, **kwargs)
- xlabel
字符串形式,即横坐标内容 - loc
有'left', 'center', 'right'
三种,默认居中'center'
plt.ylabel()
matplotlib.pyplot.ylabel(ylabel, fontdict=None, labelpad=None, *, loc=None, **kwargs)
- ylabel
字符串形式,即纵坐标内容 - loc
有'bottom', 'center', 'top'
三种,默认居中'center'
plt.title()
matplotlib.pyplot.title(label, fontdict=None, loc=None, pad=None, *, y=None, **kwargs)[source]#
- label
字符串形式,即标题内容 - fontdict
{‘fontsize’: 12,
‘fontweight’: rcParams[‘axes.titleweight’],
‘color’: rcParams[‘axes.titlecolor’],
‘verticalalignment’: ‘baseline’,
‘horizontalalignment’: loc}
- loc
有'center', 'left', 'right'
三种,默认居中
ax.set_xlim
Axes.set_xlim(left=None, right=None, *, emit=True, auto=False, xmin=None, xmax=None)
- left/right
浮点数,设置横坐标的起始结束位置。 - xmin/xmax
浮点数
ax.set_ylim
Axes.set_ylim(bottom=None, top=None, *, emit=True, auto=False, ymin=None, ymax=None)[source]
- bottom/top
浮点数,设置纵坐标的起始结束位置。 - ymin/ymax
浮点数
plt.suptitle()
图形正上方的标题,一般可用于多张图时。
plt.suptitle("Classification vector for...")
Markers
character | description |
---|---|
'.' | 点 |
'o' | 圈 |
'v' | 倒三角 |
'^' | 正三角 |
'<' | 左三角 |
'>' | 右三角 |
'*' | 星型 |
'+' | +号 |
'_' | 水平线 |
's' | 正方形 |
'x' | x型 |
'X' | x (filled) marker |
',' | pixel marker |
'1' | tri_down marker |
'2' | tri_up marker |
'3' | tri_left marker |
'4' | tri_right marker |
'8' | octagon marker |
'p' | pentagon marker |
'P' | plus (filled) marker |
'h' | hexagon1 marker |
'H' | hexagon2 marker |
'D' | diamond marker |
'd' | thin_diamond marker |
ls线条形状
character | description |
---|---|
'-' | 实线 |
'--' | 虚线 |
'-.' | 虚线加点 |
':' | 点线 |
lw线条宽度
浮点数,如:lw=2/3/1.5
c颜色
character | description |
---|---|
'b' | 蓝色 |
'g' | 绿色 |
'r' | 红色 |
'c' | 青色 |
'y' | 黄色 |
'k' | 黑色 |
'w' | 白色 |
'm' | 洋红色 |
一个小例子:
import numpy as np
import matplotlib.pyplot as plt
# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)
# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()
结果为:
To be continue