折线图
matplotlib.pyplot.
plot
(* args,scalex = True,scaley = True,data = None,** kwargs )
x:x轴上的数值
y:y轴上的数值
ls:线条风格
lw:线条宽度
color:颜色
lable:标签文本
返回值:Line2D对象
散点图
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)
x:x轴上的数值
y:y轴上的数值
s:标记大小
c:标记颜色
marker:标记样式
xmin:x轴上最小值
xmax:x轴上最大值
ymin:y轴上最小值
ymax:y轴上最大值
lable:标签文本
返回值:PathCollection对象
垂直条形图
matplotlib.pyplot.
bar
(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)
x:数据源
height:bar的高度
width:bar的宽度,默认0.8
bottom:y轴的基准,默认0
align:{‘center’, ‘edge’}, optional, default: ‘center’ x轴的位置,默认中间,edge表示将bar的左边与x对齐
color:bar颜色
tick_label:标签,默认无,使用数字标签
alpha:柱体透明度
hatch:给柱子内部加样式,如/ \\ | _等,符号数量越多,柱子内部的样式越密集
xerr:错误条
yerr:错误条
error_kw:字典,误差棒的属性
返回值:BarContainer对象
水平条形图
matplotlib.pyplot.
barh
(y,width,height = 0.8,left = None,*,align ='center',** kwargs )
y:数据源
width:bar的宽度
height:bar的高度,默认0.8
align:{‘center’, ‘edge’}, optional, default: ‘center’ x轴的位置,默认中间,edge表示将bar的左边与x对齐
color:bar颜色
tick_label:标签,默认无,使用数字标签
alpha:柱体透明度
left:x轴的基准,默认0
hatch:给柱子内部加样式,如/ \\ | _等,符号数量越多,柱子内部的样式越密集
xerr:错误条
yerr:错误条
error_kw:字典,误差棒的属性
返回值:BarContainer对象
直方图
matplotlib.pyplot.
hist
(x,bins = None,range = None,density = None,weights = None,cumulative = False,bottom = None,histtype ='bar',align ='mid',orientation ='vertical',rwidth = None,log = False,color = None,label = None,stacked = False,normed = None,*,data = None,** kwargs )
x:数据源
bins:分块数,默认10
range:画图范围,忽略超过下限和上限异常值。
normed:概率分布,是否正则化
weights:权重
cumulative:每一列累加
rwidth:bar的宽度
color:bar的颜色
lable:标签文本
histtype:柱体类型,默认bar
'''
'bar'是传统的条形直方图。如果给出多个数据,则条并排排列。
'barstacked'是一种条形直方图,其中多个数据堆叠在一起。
'step'生成一个默认未填充的线图。
'stepfilled'生成一个默认填充的线图。
'''
stacked:是否堆积,默认false,一般x为高位列表时,才进行堆积
'''如果x为高位列表,且没设置stacked,那么就是平行的'''
返回值:
n : array or list of arrays
The values of the histogram bins. See normed or density and weights for a description of the possible semantics. If input x is an array, then this is an array of length nbins. If input is a sequence of arrays [data1, data2,..], then this is a list of arrays with the values of the histograms for each of the arrays in the same order.
bins : array
The edges of the bins. Length nbins + 1 (nbins left edges and right edge of last bin). Always a single array even when multiple data sets are passed in.
patches : list or list of lists
Silent list of individual patches used to create the histogram or list of such list if multiple input datasets.
饼图:
matplotlib.pyplot.
pie
(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, rotatelabels=False, *, data=None)
x:占比大小
explode:每一块饼与圆心的距离(百分比),列表
labels:饼的标签,列表
autopct:每块饼的占比,%.2f保留一位小数,字符串或函数
colors:颜色列表,饼图将通过它循环
'''
# 随机生成一个颜色值
def randomcolor():
colorArr = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
color = ""
for i in range(6):
color += random.choice(colorArr)
return "#"+color
'''
pctdistance:每块饼占比与圆心的距离(百分比),默认0.6
shadow:是否绘制饼片的阴影
labeldistance:饼的标签与圆心的距离(百分比)
radius:直径的大小,默认为1
counterclock:逆时针或顺时针
返回值:
patches : list
A sequence of matplotlib.patches.Wedge instances
texts : list
A list of the label matplotlib.text.Text instances.
autotexts : list
A list of Text instances for the numeric labels. This will only be returned if the parameter autopct is not None.
极线图
matplotlib.pyplot.
polar
(* args,** kwargs )
theta:标记所在射线与极径的夹角
r:每个标记到原点的距离
color:颜色
linewigth:线条宽度
marker:标记样式
棉棒图
matplotlib.pyplot.
stem
(* args,linefmt = None,markerfmt = None,basefmt = None,bottom = 0,label = None,data = None )
x:棉棒的x轴基线上的位置
y:棉棒的长度
linefmt:垂直线的样式
'''
'-' 实线
'--' 虚线
'-.' 点划线
':' 虚线
'''
markerfmt:棉棒末端的样式
basefmt:基线的样式
label:文本标签
返回值:StemContainer
误差棒图
matplotlib.pyplot.
errorbar
(x, y, yerr=None, xerr=None, fmt='', ecolor=None, elinewidth=None, capsize=None, barsabove=False, lolims=False, uplims=False, xlolims=False, xuplims=False, errorevery=1, capthick=None, *, data=None, **kwargs)
x,y:数据位置
xerr:x轴方向误差计算值
yerr:y轴方向误差计算值值
fmt:是定义颜色,标记和线条样式等基本格式的便捷方式
elinewidth:误差线的线宽
marker:标记
color:颜色
ecolor:误差线颜色
lable:文本标签
elinewidth:误差棒线条粗细
ms:数据点的大小
mfc:数据点标记的颜色
capthick:误差棒边界横杠的厚度
capsize:误差棒边界横杠的大小
返回值:
容器 : ErrorbarContainer
容器包含:
plotline:Line2Dx,y的标记和/或行的实例。
caplines:Line2D错误栏上限的实例元组。
barlinecols:LineCollection具有水平和垂直误差范围的元组 。
堆积折线图
pyplot.
stackplot
(x,* args,data = None,** kwargs )
x:x轴坐标
y:数组,需要堆积的数据
labels:lanbel组成的列表
colors:color组成的列表
返回值:PolyCollection实例列表
间断条形图
pyplot.
broken_barh
(xranges,yrange,*,data = None,** kwargs )
xranges:元组序列[(xmin,xwidth),...],xmin表示x轴的起点位置,xwidth表示向右移动多少
yrange:(YMIN,YMAX),YMIN表示y轴的起点位置,YMAX表示向上移动多少
'''xranges与yrange形成一个或多个矩形'''
facecolor:颜色或颜色元组
箱线图
pyplot.
boxplot
(x, notch=None, sym=None, vert=None, whis=None, positions=None, widths=None, patch_artist=None, bootstrap=None, usermedians=None, conf_intervals=None, meanline=None, showmeans=None, showcaps=None, showbox=None, showfliers=None, boxprops=None, labels=None, flierprops=None, medianprops=None, meanprops=None, capprops=None, whiskerprops=None, manage_xticks=True, autorange=False, zorder=None, *, data=None)
x:数据,可以是高位数组
sym:表示异常点的形状
vert:是否垂直,箱线图是横向的(False)还是竖向的(True)
whis: IQR的倍数,默认1.5,用来确定箱须包含数据的范围
patch_artist:是否给箱体添加颜色
showmeans:是否显示均值,默认不显示
meanline:是否用线的形式表示均值,默认用点表示
showbox:是否显示箱线图的箱体
showcaps:是否显示边缘线,箱线图顶端和末端的两条线默认显示
showfliers:是否显示异常值
notch:中间箱体是否缺口
labels:每个数据集的刻度标签
阶梯图
pyplot.
step
(x,y,* args,where ='pre',data = None,** kwargs )
与plot的用法相同
where:{'pre','post','mid'},可选,默认'pre'
pre:按照左开右闭的方式取值
post:按照左闭右开的方式取值
子图
pyplot.subplot()
参数:几行、几列、要在哪里绘图
211:两行一列,且此时我需要绘制的是最上面的一行
223:两行两列,且此时我需要绘制的是第二行的左边的区域
nrows:行数
ncols:列数
sharex:是否共享X轴坐标
sharey:是否共享Y轴坐标
返回值:Axes(Axes调用方法,一般要在方法前加上set_)
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
plt.subplot(221)
x=[1,2,3,4,5]
y=[1,2,3,4,5]
plt.plot(x,y)
plt.subplot(222)
x1=["A","B","C"]
y1=[10,15,20]
plt.bar(x1,y1)
ax=plt.subplot(223)
x2=["A","B","C"]
y2=[10,15,20]
ax.set_xlabel("水平条形图")
plt.barh(x2,y2)
plt.show()
颜色
字符 | 颜色 |
---|---|
'b' | 蓝色 |
'g' | 绿色 |
'r' | 红色 |
'c' | 青色 |
'm' | 品红 |
'y' | 黄色 |
'k' | 黑色 |
'w' | 白色 |
线条样式
字符 | 描述 |
---|---|
'-' | 实线风格 |
'--' | 虚线样式 |
'-.' | 点划线样式 |
':' | 虚线样式 |
标记
字符 | 描述 |
---|---|
'.' | 点标记 |
',' | 像素标记 |
'o' | 圆圈标记 |
'v' | triangle_down标记 |
'^' | triangle_up标记 |
'<' | triangle_left标记 |
'>' | triangle_right标记 |
'1' | tri_down标记 |
'2' | tri_up标记 |
'3' | tri_left标记 |
'4' | tri_right标记 |
's' | 方形标记 |
'p' | 五边形标记 |
'*' | 明星标记 |
'h' | hexagon1标记 |
'H' | hexagon2标记 |
'+' | 加上标记 |
'x' | x标记 |
'D' | 钻石标记 |
'd' | thin_diamond标记 |
'|' | vline标记 |
'_' | hline标记 |