绘制散点图:
假设通过爬虫你获取到了北京2019年3,10月份每天白天的最高气温(分别位于列表a,b),那么此时如何寻找出气温和随时间(天)变化的某种规律?
y_3 = [14,16,14,17,17,10,13,14,11,12,16,11,16,16,14,19,20,21,24,19,12,14,12,20,22,18,21,10,13,10,15]
y_10 = [30,29,29,15,19,19,24,20,22,22,18,18,13,16,17,15,14,18,20,22,19,20,21,16,14,16,18,15,16,20,22]
数据来源: http://lishi.tianqi.com/beijing/index.html
提示:plt.scatter() # 绘制散点图
代码:
from matplotlib import pyplot as plt
import matplotlib
font = {
'family' : 'WenQuanYi Micro Hei',
'weight' : 'bold',
'size' : '10'}
# 设置中文字体
matplotlib.rc("font",**font)
# y轴数据
y_3 = [14,16,14,17,17,10,13,14,11,12,16,11,16,16,14,19,20,21,24,19,12,14,12,20,22,18,21,10,13,10,15]
y_10 = [30,29,29,15,19,19,24,20,22,22,18,18,13,16,17,15,14,18,20,22,19,20,21,16,14,16,18,15,16,20,22]
x_3 = range(1,32)
x_10 = range(41,72)
# 设置图片大小
plt.figure(figsize=(20,8),dpi=80)
# 设置以散点图方式显示
plt.scatter(x_3,y_3,label="3月")
plt.scatter(x_10,y_10,label="10月")
# 设置x刻度
x_lis = list(x_3)+list(x_10)
x_label = ["3月{}日".format(i) for i in x_3]
x_label += ["10月{}日".format(i-40) for i in x_10]
# 设置步长
plt.xticks(x_lis[::3],x_label[::3],rotation=45)
# 添加描述信息
plt.xlabel("日期")
plt.ylabel("温度")
plt.title("温度随时间变化的散点图")
# 添加图例
plt.legend(loc="upper left")
# 显示图片
plt.show()
效果图:
绘制条形图:
_x = range(len(a))
_y = b
plt.bar(_x,b,width=0.2,color="orange")
->bar绘制条形图,只能接受含数字的可迭代对象
->width表示长条的宽度,默认0.8
plt.xticks(_x,a,fontproperties=my_font,rotation=90)
-> 通过设置xticks实现数字和字符串的对应
---------------------------
绘制横着的直方图:
plt.barh(_x,b,height=0.2,color="orange")
例题:
假设你获取到了2017年内地电影票房前20的电影(列表a)和电影票房数据(列表b),那么如何更加直观的展示该数据?
a = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊",]
b = [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]
单位:亿
数据来源: http://58921.com/alltime
代码:
from matplotlib import pyplot as plt
import matplotlib
font = {
'family' : 'WenQuanYi Micro Hei',
'weight' : 'bold',
'size' : '10'}
matplotlib.rc("font",**font)
a = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:\n最后的骑士","摔跤吧!爸爸","加勒比海盗5:\n死无对证","金刚:骷髅岛","极限特工:\n终极回归","生化危机6:\n终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:\n殊死一战","蜘蛛侠:\n英雄归来","悟空传","银河护卫队2","情圣","新木乃伊",]
b = [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.figure(figsize=(20,10),dpi=80)
# 绘制条形图
plt.bar(range(len(a)),b,width=0.3,color="gold")
# 设置x轴刻度
plt.xticks(range(len(a)),a,rotation=45)
# 设置网格
plt.grid(alpha=0.5,color="#aaa")
# 添加描述信息
plt.xlabel("影片名")
plt.ylabel("票房/单位:亿")
# 保存图片
plt.savefig("./images/movie.png")
# 显示图片
plt.show()
效果图:
横向显示代码:
from matplotlib import pyplot as plt
import matplotlib
font = {
'family' : 'WenQuanYi Micro Hei',
'weight' : 'bold',
'size' : '10'}
matplotlib.rc("font",**font)
a = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
b = [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.figure(figsize=(20,8),dpi=80)
# 绘制横向条形图(barh)(y,x,height)
plt.barh(range(len(a)),b,height=0.3,color="gold")
# 设置y轴刻度
plt.yticks(range(len(a)),a)
# 设置网格
plt.grid(alpha=0.5,color="#aaa")
# 添加描述信息(x,y轴互换)
plt.ylabel("影片名")
plt.xlabel("票房/单位:亿")
# 保存图片
plt.savefig("./images/movie2.png")
# 显示图片
plt.show()
效果图:
绘制多次条形图:
假设你知道了列表a中电影分别在2020年9月07-13日(b_1), 2020年9月14-20日(b_2), 2020年09月21-27日(b_3)三周的票房,为了展示列表中电影本身的票房以及同其他电影的数据对比情况,应该如何更加直观的呈现该数据?
a = ["信条","花木兰","八佰","我在时间的尽头等你"]
b_1 = [14467,15912,26249,1710]
b_2 = [6497,8767,21738,727]
b_3 = [2516,2588,13709,390]
数据来源: http://www.cbooo.cn/movieday
代码:
from matplotlib import pyplot as plt
import matplotlib
font = {
'family' : 'WenQuanYi Micro Hei',
'weight' : 'bold',
'size' : '10'}
matplotlib.rc("font",**font)
a = ["信条","花木兰","八佰","我在时间的尽头等你"]
b_1 = [14467,15912,26249,1710]
b_2 = [6497,8767,21738,727]
b_3 = [2516,2588,13709,390]
# 设置条形图宽度
WIDTH = 0.2
# 设置x轴
b1_x = range(len(b_1))
b2_x = [i+WIDTH for i in b1_x]
b3_x = [i+2*WIDTH for i in b1_x]
# 设置图片大小
plt.figure(figsize=(20,8),dpi=80)
# 设置条形图
plt.bar(b1_x,b_1,width=WIDTH,label="第一周")
plt.bar(b2_x,b_2,width=WIDTH,label="第二周")
plt.bar(b3_x,b_3,width=WIDTH,label="第三周")
# 设置x轴刻度(取中间的位置,显示字符串)
plt.xticks(b2_x,a)
# 添加网格
plt.grid(alpha=0.2)
# 添加图例
plt.legend()
# 添加描述信息
plt.ylabel("单周票房(万)")
plt.xlabel("影片名")
# 显示
plt.show()
效果图:
绘制直方图:
假设你获取了250部电影的时长(列表a中),希望统计出这些电影时长的分布状态
(比如时长为100分钟到120分钟电影的数量,出现的频率)等信息,你应该如何呈现这些数据?
a=[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]
--------------------------------
把数据分为多少组进行统计???
组数要适当,太少会有较大的统计误差,大多规律不明显
组数:将数据分组,当数据在100个以内时,按数据多少常分5-12组.
组距:指每个小组的两个端点的距离,
组数 = 极差/组距 = (max(a)-min(a))/bin_width
--------------------------------
bin_width = 3 #设置组距为3
num_bins = int((max(a)-min(a))/bin_width) #分为多少组,
plt.hist(a,num_bins)
一> 传入需要统计的数据,以及组数即可
# plt.hist(a,[min(a) + i*bin_width for i in range(num_bins)])
一> 可以传入一个列表,长度为组数,值为分组依据,当组距不均匀的时候使用
# plt.hist(a, num_bins,normed=1)
一>normed:bool 是否绘制频率分布直方图,默认为频数直方图
plt.xticks(list(range(min(a),max(a)))[::bin_ width], rotation=45)
plt.grid(True, linestyle = "-.", alpha=0.5)
#显示网格, alpha为透明度
当 num_bins = (max(a)-min(a))/bin_width 算出来为小数时,用//地板除法会出现下面这么情况:
即网格与间隔不对应.
解决方法:
# 间隔
bin_width = 5
# 求组数
todiff = (max(a)-min(a))%bin_width
# 判断是否能整除
if(todiff!=0):
# 如过整除不能则追加一个数据使其恰好能整除
oth = bin_width - todiff
a.append(max(a)+oth)
num_bins = (max(a)-min(a))//bin_width
此种解决方法可能会使最后一格的数据多一个,当此误差对整体影响不大时,可采用
代码:
from matplotlib import pyplot as plt
import matplotlib
font = {
'family' : 'WenQuanYi Micro Hei',
'weight' : 'bold',
'size' : '10'}
matplotlib.rc("font",**font)
a = [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 = 5
# 求组数
todiff = (max(a)-min(a))%bin_width
# 判断是否能整除
if(todiff!=0):
# 如过整除不能则追加一个数据使其恰好能整除
oth = bin_width - todiff
a.append(max(a)+oth)
num_bins = (max(a)-min(a))//bin_width
# 图片大小
plt.figure(figsize=(20,8),dpi=80)
# 直方图
plt.hist(a,num_bins)
# 设置x轴刻度
x_tick = range(min(a),max(a)+bin_width,bin_width)
plt.xticks(x_tick)
# 设置网格
plt.grid()
# 显示
plt.show()
效果图:
现有以下问题:
在美国2004年人口普查发现有124 million的人在离家相对较远的地方工作.根据他们从家到上班地点所需要的时间,
通过抽样统计(最后一列)出了下表的数据,这些数据能够绘制成直方图么?
interval = [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]
quantity = [836,2737,3723,3926,3596,1438,3273,642,824,613,215,47]
数据来源:https://en.wikipedia.org/wiki/Histogram
普查报告地址:https://www.census.gov/prod/2004pubs/c2kbr-33.pdf
上面的问题中给出的数据都是统计之后的数据,
所以为了达到直方图的效果,需要绘制条形图
一般来说能够使用plt.hist方法的的是那些没有统计过的数据
代码:
from matplotlib import pyplot as plt
import matplotlib
font = {
'family' : 'WenQuanYi Micro Hei',
'weight' : 'bold',
'size' : '10'}
matplotlib.rc("font",**font)
interval = [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]
quantity = [836,2737,3723,3926,3596,1438,3273,642,824,613,215,47]
# 将条形图的宽度设置为1来使其每个各自都连着(模拟直方图)
WIDTH = 1
# 图片大小
plt.figure(figsize=(20,8),dpi=80)
# 绘制条形图(x,y)
plt.bar(range(len(interval)) ,quantity,width=WIDTH,color="gold")
# 设置x的刻度
# 条形图的刻度默认在格子中央,将x轴刻度默认向左移动半个WIDTH宽度来使其对应
# 加一个刻度是因为刻度左移动后,后面会空出来,则需要加一个数据中最大的值来作为最厚一个刻度
_x = [i-0.5 for i in range(len(interval)+1)]
x_label = interval + [150]
plt.xticks(_x,x_label)
# 设置网格
plt.grid()
# 显示
plt.show()
效果图:
matplotlib总结:
1. 应该选择那种图形来呈现数据
2. matplotlib.plot(x,y)
3. matplotlib.bar(x,y)
4. matplotlib.scatter(x,y)
5. matplotlib.hist(data,bins)
6. xticks和yticks的设置
7. label和titile,grid的设置
8. 绘图的大小和保存图片
matplotlib支持的图形是非常多的,如果有其他的需求,我们
可以查看一下url地址:
http://matplotlib.org/gallery/index.html
更多的绘图工具:
plotly:可视化工具中的github,相比于matplotlib更加简单,图形更加漂亮,同时兼容matplotlib和pandas
使用用法:简单,照着文档写即可
文档地址: https://plot.ly/python/