安装
pip install matplotlib
在jupyter notebook里安装
! pip install matplotlib
图像结构
不同的类型数据需要选择合适的图显示
折线图
plt.plot()
折线样式
- color:折线的颜色
- alpha:透明度
- linestyle:样式
‘-’:实线
‘- -’:虚线
‘-.’:点虚线
’..’:点线 - linewidth:折线的宽度
- marker:折点样式
'-' '--' '-.' ':' '.' ',' 'o' 'v' '^' '>' '<' '1' '2' '3' '4' 's' 'p' '*' 'h' 'H' '+' 'x' 'd' 'D' '|' '_'
设置图片
- figsize:设置图片的长宽,单位英寸
- dpi:每英寸像素点
保存图片:plt.savefig(),支持eps, jpeg, jpg, pdf, pgf, png, ps, raw, rgba, svg, svgz, tif, tiff格式
设置x轴的刻度:plt.xticks()
设置y轴的刻度:plt.yticks()
- rotation:旋转角度
显示中文
matplotlib默认不显示中文,要自行设置
- 设置
fontproperties
plt.ylabel('温度',fontproperties='simHei')
-
import matplotlib matplotlib.rcParams['font.sans-serif']=['SimHei']
一图多线
设置图例
plt.legend(prop,loc)
prop
设置字体loc
设置位置,默认upper left
,可改为upper right
upper center
lower left
center left
etc.
散点图
plt.scatter()
设置网格
plt.grid()
linestyle
网格样式axis
轴alpha
设置网格透明度
条形图
plt.bar()
width
设置柱形的宽度color
设置颜色
直方图
'''
统计250部电影的时长的分布状态(比如时长为100分钟到120分钟电影的数量,出现的频率)
'''
import numpy as np
time = [131,98,125,131,124,139,131, 117, 128, 108, 135, 138, 131, 102, 107, 114,119,128,121,142,127,130,124, 101, 110, 116,
117, 110, 128, 128, 115,99,136,126,134,95,138,117,111,78, 132, 124, 113, 150, 110, 117,86,95, 144,105, 126, 130,126,
130, 126, 116, 123, 106, 112, 138, 123,86, 101,99, 136,123,117,119,105,137, 123, 128, 125, 104, 109, 134, 125, 127,105,
120,107,129, 116,108,132,103,136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121,112,139, 125,138,109,132,134,156,
106, 117, 127, 144, 139, 139, 119, 140,83,110,102,123,107,143,115,136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112,
114,122, 109,106,123,116,131,127, 115, 118, 112, 135,115,146,137,116,103,144,83,123,111,110,111,100,154,136, 100, 118,
119, 133,134,106,129,126,110,111,109,141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103,
130,141, 117, 106, 114, 121, 114, 133, 137,92,121,112,146,97,137, 105,98,117,112,81,97, 139, 113,134, 106, 144, 110,
137,137,111,104,117, 100, 111,101,110,105, 129, 137, 112, 120, 113, 133, 112,83,94,146,133,101,131, 116,111,84, 137,
115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150]
plt.figure(figsize=(16,6))
# 设置组距
distance = 2
# 计算组数
group_num = int((max(time) - min(time)) / distance)
h = plt.hist(time,bins=group_num)
plt.xticks(range(min(time),max(time)+2)[::2])
plt.yticks(range(np.int(min(h[0])),np.int(max(h[0]))+1))
plt.grid(linestyle='--',alpha=0.5)
plt.xlabel('电影时长',fontproperties='simHei')
plt.ylabel('电影数量',fontproperties='simHei')
plt.title('250部电影的时长的分布状态',fontproperties='simHei',size=18)
plt.show()
print(h)
(array([ 1., 1., 3., 1., 2., 0., 0., 1., 3., 2., 4., 7., 6.,
8., 11., 8., 18., 12., 13., 17., 12., 9., 13., 9., 11., 8.,
10., 10., 7., 13., 10., 4., 2., 6., 3., 1., 2., 0., 2.]), array([ 78., 80., 82., 84., 86., 88., 90., 92., 94., 96., 98.,
100., 102., 104., 106., 108., 110., 112., 114., 116., 118., 120.,
122., 124., 126., 128., 130., 132., 134., 136., 138., 140., 142.,
144., 146., 148., 150., 152., 154., 156.]), <BarContainer object of 39 artists>)
饼图
plt.pie()
size
各部分大小explode
设置各部分突出label
各部分标签labeldistance
标签文本距圆心位置,1.1表示1.1倍半径autopct
圆内文本shadow
是否有阴影startangle
起始角度,默认从0开始逆时针pctdistance
圆内文本距圆心距离返回值l_text
圆内文本,p_text
圆外文本