1.matplotlib
1. matplotlib基本要点
In [193]: from matplotlib import pyplot as plt
In [194]: x = range(2, 26, 2) # 数据在x轴是一个可迭代对象
In [195]: x
Out[195]: range(2, 26, 2)
In [196]: y = [15, 13, 14.5, 17,20,25,26,26,24,22,18,15]
# 数据在y轴的位置是一个可迭代对象
# x轴和y轴的数据组成了所有要绘制的坐标
In [197]: plt.plot(x, y) # 传入x和y,通过plot 绘制出折线图
Out[197]: [<matplotlib.lines.Line2D at 0x112a47f10>]
In [198]: plt.show() # 在执行程序的时候展示图形
但是目前存在以下几个问题:
1.设置图片大小(想要一个高清无码大图)
2.保存到本地
3.描述信息,比如x轴和y轴表示什么,这个图表示什么
4.调整x或者y的刻度的间距
5.线条的样式(比如颜色,透明度等)
6.标记出特殊的点(比如告诉别人最高点和最低点在哪里)
7.给图片添加一个水印(防伪,防止盗用)
1. 设置图片大小
from matplotlib import pyplot as plt
fig = plt.figure(figsize=(20,8), dpi=80) # figure图形图标,这里指我们画的图;通过实例化一个figure并传递参数,能够在后台自动使用该实例
x = range(2, 26, 2)
y = [15, 13, 14.5, 17,20,25,26,26,24,22,18,15]
plt.plot(x, y)
plt.savefig("./sig_size.png")
plt.show()
2. 调整X或者Y轴上的刻度
from matplotlib import pyplot as plt
fig = plt.figure(figsize=(10,5), dpi=80) #
x = range(2, 26, 2)
y = [15, 13, 14.5, 17,20,25,26,26,24,22,18,15]
plt.plot(x, y)
plt.xticks(x) # 设置x刻度
# plt.xticks(x[::2]) 当刻度太密集的时候使用列表的步长(间隔取值)来解决,matplotlib会自动对应
plt.savefig("./sig_size.png")
plt.show()
from matplotlib import pyplot as plt
import random
fig = plt.figure(figsize=(20,8), dpi=80)
x = range(120)
random.seed(10)
y = [random.uniform(20,35) for i in range(120)]
_x_ticks = ["10点{}分".format(i) for i in x if i < 60]
_x_ticks += ["11点{}分".format(i-60) for i in x if i >= 60]
plt.xticks(x, _x_ticks[::5], rotation=90)
# len(x) 必须等于_x_ticks rotation旋转90°
plt.plot(x, y)
plt.xticks(x) # 设置x刻度
# plt.xticks(x[::2]) 当刻度太密集的时候使用列表的步长(间隔取值)来解决,matplotlib会自动对应
plt.savefig("./sig_size.png")
plt.show()
3. 设置中文显示
为什么无法显示中文:
matplotlib默认不支持中文字符,因为默认的英文字体无法显示汉字
查看linux/mac下面支持的字体:
fc-list à查看支持的字体
fc-list :lang=zh à查看支持的中文(冒号前面有空格)
那么问题来了:如何修改matplotlib的默认字体?
通过matplotlib.rc可以修改,具体方法参见源码(windows/linux)
通过matplotlib 下的font_manager可以解决(windows/linux/mac)
from matplotlib import pyplot as plt
import random
import matplotlib
from matplotlib import font_manager
# 全局设置,只用在该处修改即可(windows 和 linux 下有效)
# font = {"family": "Microsoft Yahei", "size": "10"}
# matplotlib.rc("font", **font)
x = range(120)
my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")
plt.xticks(x[::5], rotation=90, fontproperties=my_font)
plt.show()
4. 给图像添加描述信息
from matplotlib import pyplot as plt
from matplotlib import font_manager
x = range(120)
y = x
my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")
plt.xlabel("时间", fontproperties=my_font)
plt.ylabel("温度", fontproperties=my_font)
plt.xticks(x[::5], rotation=90, fontproperties=my_font)
plt.plot(x, y)
plt.show()
5. 多数据展示
from matplotlib import font_manager
from matplotlib import pyplot as plt
import random
fig = plt.figure(figsize=(20,8), dpi=80)
random.seed(10)
x = range(60)
y = [random.uniform(20,35) for i in range(60)]
y1 = [random.uniform(25,40) for i in range(60, 120)]
_x_ticks = ["10点{}分".format(i) for i in x if i < 60]
my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")
plt.xticks(x[::5], _x_ticks[::5],rotation=90, fontproperties=my_font)
# plt.yticks(y[::5], rotation=90, fontproperties=my_font)
plt.xlabel("时间", fontproperties=my_font)
plt.ylabel("温度(C°)", fontproperties=my_font)
plt.plot(x, y, label='北京', linestyle='--', color='red', alpha=0.5)
plt.plot(x, y1, label='上海', linestyle='-', color='blue', alpha=0.5)
plt.legend(prop=my_font, loc='best') # loc:图例位置 prop:图例字体
plt.title("10点气温数据展示", fontproperties=my_font)
plt.show()
![image.png](https://img-blog.csdnimg.cn/img_convert/9c709c28a65557f177980d2bf03c55ad.png#align=left&display=inline&height=289&margin=[object Object]&name=image.png&originHeight=289&originWidth=199&size=14493&status=done&style=none&width=199)
6. 常用统计图
折线图:以折线的上升或下降来表示统计数量的增减变化的统计图
特点:能够显示数据的变化趋势,反映事物的变化情况。(变化)
直方图:由一系列高度不等的纵向条纹或线段表示数据分布的情况。
一般用横轴表示数据范围,纵轴表示分布情况。
特点:绘制连续性的数据,展示一组或者多组数据的分布状况(统计)
条形图:排列在工作表的列或行中的数据可以绘制到条形图中。
特点:绘制连离散的数据,能够一眼看出各个数据的大小,比较数据之间的差别。(统计)
散点图:用两组数据构成多个坐标点,考察坐标点的分布,判断两变量
之间是否存在某种关联或总结坐标点的分布模式。
特点:判断变量之间是否存在数量关联趋势,展示离群点(分布规律)
2. 折线图(plt.plot(x, y))
3. 散点图(plt.scatter(x,y))
from matplotlib import font_manager
from matplotlib import pyplot as plt
fig = plt.figure(figsize=(20,8), dpi=80)
my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")
x = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
y = [56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23]
plt.xticks(range(len(x)), x,rotation=90, fontproperties=my_font)
plt.xlabel("movie", fontproperties=my_font)
plt.ylabel("money(亿元)", fontproperties=my_font)
plt.scatter(x, y, label='北京', linestyle='--', color='red', linewidth=5, alpha=0.5) #alpha:透明度;linewidth:粗细;
plt.scatter(x, y[::-1], label='上海', linestyle='-', color='blue', linewidth=5, alpha=0.5) #alpha:透明度;linewidth:粗细;
plt.legend(prop=my_font, loc='best') # loc:图例位置 prop:图例字体
plt.title("北京上海票房统计", fontproperties=my_font)
plt.show()
4. 条形图(plt.bar(x, y))
from matplotlib import font_manager
from matplotlib import pyplot as plt
fig = plt.figure(figsize=(10,4), dpi=120)
my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")
x = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
y = [56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23]
bar_width = 0.2
plt.xticks([i+bar_width/2 for i in range(len(x))], x,rotation=90, fontproperties=my_font)
plt.xlabel("movie", fontproperties=my_font)
plt.ylabel("money(亿元)", fontproperties=my_font)
# 只接收含有数字的可迭代对象
plt.bar(x, y, label='北京', color='red', width=bar_width) #alpha:透明度;width:粗细;
plt.bar([i+bar_width for i in range(len(x))], y[::-1], label='上海', color='blue', width=bar_width) #alpha:透明度;linewidth:粗细;
plt.legend(prop=my_font, loc='best') # loc:图例位置 prop:图例字体
plt.title("北京上海票房统计", fontproperties=my_font)
plt.show()
5. 直方图(plt.hist(x, nums))(plt.grid(True)
组距:指每个小组两个端点的距离
组数:将数据分组,当数据在100个以内时,按数据多少常分组5-12组
组数: 组 数 = 极 差 组 距 = M A X ( a ) − M I N ( a ) w i d t h 组数=\frac{极差}{组距}=\frac{MAX(a)-MIN(a)}{width} 组数=组距极差=widthMAX(a)−MIN(a)
在美国2004年人口普查发现有124 million的人在离家相对较远的地方工作。根据他们从家到上班地点所需要的时间,通过抽样统计(最后一列)出了下表的数据
![image.png](https://img-blog.csdnimg.cn/img_convert/d0a66328d2a49c034109716cd4a24b0e.png#align=left&display=inline&height=209&margin=[object Object]&name=image.png&originHeight=209&originWidth=167&size=27943&status=done&style=none&width=167)
原始数据
from matplotlib import font_manager
from matplotlib import pyplot as plt
import random
random.seed(10)
fig = plt.figure(figsize=(10,4), dpi=120)
my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")
x =[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]
bin_width = 2
num_bins = int((max(x)-min(x))/bin_width)
# plt.hist(x, num_bins)
# plt.hist(x, [min(x)+i*bin_width for i in range(num_bins)]) # 组距不均匀可以使用
plt.hist(x, num_bins, normed=1) # 频率直方图
plt.xticks(list(range(min(x), max(x)))[::bin_width], rotation=45, fontproperties=my_font)
plt.xlabel("movie time", fontproperties=my_font)
plt.ylabel("movie num", fontproperties=my_font)
plt.grid(True, linestyle="-.", alpha=0.5)
plt.title("电影时长统计", fontproperties=my_font)
plt.show()
统计完数据
from matplotlib import font_manager
from matplotlib import pyplot as plt
fig = plt.figure(figsize=(10,4), dpi=120)
my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")
x = [0,5,10,15,20,25,30,35,40,45,60,90]
width = [5,5,5,5,5,5,5,5,5,15,30,60]
y = [836,2737,3723,3926,3596,1438,3273,642,824,613,215,47]
bar_width = 5
plt.xticks([x[i]+width[i]/2 for i in range(len(x))], x,rotation=90, fontproperties=my_font)
plt.xlabel("movie", fontproperties=my_font)
plt.ylabel("money(亿元)", fontproperties=my_font)
# 只接收含有数字的可迭代对象
plt.bar(x, y, label='北京', color='red', width=bar_width) #alpha:透明度;width:粗细;
plt.legend(prop=my_font, loc='best') # loc:图例位置 prop:图例字体
plt.title("北京上海票房统计", fontproperties=my_font)
plt.show()
6. 常见问题
1.应该选择那种图形来呈现数据
2.matplotlib.plot(x,y)
3.matplotlib.bar(x,y)
4.matplotlib.scatter(x,y)
5.matplotlib.hist(data,bins,normed)
6.xticks和yticks的设置
7.label和titile,grid的设置
8.绘图的大小和保存图片
7. 更多图样
matplotlib支持的图形是非常多的,如果有其他的需求,我们
可以查看一下url地址:
http://matplotlib.org/gallery/index.html
8. 绘图工具
plotly:可视化工具中的github,相比于matplotlib更加简单,图形更加漂亮,同时兼容matplotlib和pandas
使用用法:简单,照着文档写即可
文档地址:https://plot.ly/python/