Python中使用matplotlib库绘制各种图
作者:鹿上的程序媛
这篇文章主要介绍了Python中使用matplotlib库绘制各种图方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
1.绘制折线图(pyplot.plot(x,y))
from matplotlib import pyplot x = range(2,28,2) y = [15,13,14,17,20,25,26,26,24,22,18,15,1] //设置图片大小 fig = pyplot.figure(figsize=(5,5),dpi=80) //绘图 pyplot.plot(x,y) //保存图片 pyplot.savefig("./sig_size.png") //展示图片 pyplot.show()
绘制两条折线
# import matplotlib from matplotlib import pyplot x = range(11,31) y1 = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1] y2 = [1,0,3,1,2,2,3,3,2,1,2,1,1,1,1,1,1,1,1,2] #set the pic size pyplot.figure(figsize=(10,5),dpi=80) #ploting pyplot.plot(x,y1,label="Luna",color="pink",linestyle='--',linewidth=1) pyplot.plot(x,y2,label="Jim",color="tomato",linestyle='--',linewidth=1) # set x/y-axis step _xtick_labels = ["{}".format(i) for i in x] pyplot.xticks(x,_xtick_labels) pyplot.yticks(range(0,7)) # set x-asix desciption pyplot.xlabel("Age") # set y-asix description pyplot.ylabel("Num") # add legend pyplot.legend() # plot grid pyplot.grid(alpha=0.2,linestyle=':') # show pyplot.show()
2.绘制散点图(pyplot.scatter(x,y))
from matplotlib import pyplot from matplotlib import font_manager y_3 = [11,12,12,13,14,11,12,17,20,25,22,27,21,29,23,22,24,12,11,21,10,12,11,13,34,34,23,22,24,12,11] y_10 = [34,34,33,33,34,32,31,31,30,32,21,23,23,13,22,21,24,25,23,23,11,12,12,13,34,34,23,22,24,12,11] x_3 = range(1,32) x_10 = range(41,72) pyplot.figure(figsize=(20,10),dpi=80) pyplot.scatter(x_3,y_3,color="blue",label="March") pyplot.scatter(x_10,y_10,color="cyan",label="Noverber") _x = list(x_3)+list(x_10) _xticks_label = ["March{}".format(i) for i in x_3] _xticks_label += ["Noverber{}".format(i-40) for i in x_10] pyplot.xticks(_x[::10],_xticks_label[::10]) pyplot.xlabel("date") pyplot.ylabel("temperature") pyplot.legend() pyplot.show()
3.绘制条形图(pyplot.bar())
from matplotlib import pyplot from matplotlib import font_manager my_font = font_manager.FontProperties(fname="") a = ["Friends","My Heart go on","kongfu"] b = [57.1,25,12] pyplot.figure(figsize=(4,6),dpi=80) pyplot.bar(a,b,width=0.3) pyplot.xlabel("movie") pyplot.ylabel("score") pyplot.xticks(range(len(a)),a,rotation=15) pyplot.show()
pyplot.barh()=>绘制横着的条形图
from matplotlib import pyplot from matplotlib import font_manager my_font = font_manager.FontProperties(fname="") a = ["Friends","My Heart go on","kongfu"] b = [57.1,25,12] pyplot.figure(figsize=(8,6),dpi=80) pyplot.barh(a,b,height=0.3) pyplot.ylabel("movie") pyplot.xlabel("score") pyplot.yticks(range(len(a)),a) pyplot.grid(alpha=0.2) pyplot.show()
绘制对比条形图(绘制三次)
from matplotlib import pyplot a = ["movie1","movie2","movie3","movie4","movie5"] b_1 = [3124,123,5431,3411,2344] b_2 = [3456,2123,1455,8764,2323] b_3 = [213,431,124,56,120] bar_width=0.2 x_1 = list(range(len(a))) x_2 = [i+bar_width for i in x_1] x_3 = [i+bar_width*2 for i in x_1] pyplot.figure(figsize=(10,4),dpi=80) pyplot.bar(range(len(a)),b_1,width=bar_width,color="blue",label="1") pyplot.bar(x_2,b_2,width=bar_width,color="pink",label="2") pyplot.bar(x_3,b_3,width=bar_width,label="3") pyplot.xticks(x_2,a) pyplot.legend() pyplot.show()
4.绘制直方图(pyplot.hist())
频数分布直方图pyplot.hist(a,num_bins)
from matplotlib import pyplot a = [9,34,13,73,44,34,76,34,72,17,96,46,84,52,72,26,81,64,79,45,99] d = 10 num_bins = (max(a)-min(a))//d pyplot.figure(figsize=(10,6),dpi=80) pyplot.hist(a,num_bins) pyplot.xlabel("time") pyplot.ylabel("num") pyplot.xticks(range(min(a),max(a)+d,d)) pyplot.grid() pyplot.show()
频率分布直方图pyplot.hist(a,num_bins,density=True)
from matplotlib import pyplot a = [9,34,13,73,44,34,76,34,72,17,96,46,84,52,72,26,81,64,79,45,99] d = 10 num_bins = (max(a)-min(a))//d pyplot.figure(figsize=(10,6),dpi=80) pyplot.hist(a,num_bins,density=True) pyplot.xlabel("time") pyplot.ylabel("percentage") pyplot.xticks(range(min(a),max(a)+d,d)) pyplot.grid() pyplot.show()
绘制组距变化的直方图(pyplot.bar())
这里的组距为一个数组
from matplotlib import pyplot 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] quality = [836,2737,3723,3926,3596,1438,3273,642,824,613,215,47] pyplot.bar(range(12),quality,width=1) _x = [i-0.5 for i in range(13)] _xtick_label = interval+[150] pyplot.xticks(_x,_xtick_label) pyplot.grid() pyplot.show()
总结
1.如何选择哪种图来呈现数据?
2.matplotlib.plot(x,y)
绘制的是折线图,x为代表x轴的list,y为代表y轴的值的list,这里的x和y的元素个数必须是一致的
3.matplotlib.bar(x,height,width)
绘制的是条形图,x为代表x轴的list,height为对应的x的值,width为条形图的宽度
4.matplotlib.barh(y,width,height)
绘制横着的条形图,x和y的含义相反
5.matplotlib.scatter(x,y)
绘制散点图
6.matplotlib.hist(x,bin,density)
绘制直方图,这里的x为源数据的数组,bin为分多少组显示,这里的图的y值代表在某个范围内的频率或频数,通过参数density可以绘制频数直方图或频率直方图,默认为频数直方图一般设置一个组距dbin = (max(a)-min(x))//d
7.xticks和yticks的设置
设置x轴和y轴的坐标
8.label和title,grid的设置
9.绘图的大小(figure)和保存图片(savefig)
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。