IT运维笔记


python图表绘制及可视化

用于数据分析及可视化(更多内容可以阅读官方文档) 1.Matplotlib 子库:pyplot,与MATLAB相似 函数式绘图 import numpy as np import matplotlib.pyplot as plt x=np.linspace(0,10,1000) y=np.sin(x) z=np.cos(x**2) plt.figure(figsize=(8,4)) plt.plot(x,y,label="$sin(x)$",color="red",linewidth=2) plt.plot(x,z,"b--",label="$cos(X^2)$") plt.xlabel("Time(s)") plt.ylabel("Volt") plt.title("PyPlot First Example") plt.ylim(-1.2,1,2) plt.legend() plt.show() 子图声明方法 import numpy as np import matplotlib.pyplot as plt plt.subplot(2,1,1) #参数依次为行,列,第几项 plt.subplot(2,2,3) plt.subplot(2,2,4) plt.show() 柱状图 import numpy as np import matplotlib.pylab as plt plt.subplot(2,1,1) n=12 X=np.arange(n) Y1=(1-X/float(n))*np.random.uniform(0.5,1.0,n) Y2=(1-X/float(n))*np.random.uniform(0.5,1.0,n) plt.bar(X,+Y1,facecolor='#9999ff',edgecolor='white') plt.bar(X,-Y2,facecolor='#9999ff',edgecolor='white') for x,y in zip(X,Y1): plt.text(x+0.4,y+0.05,'%.2f' % y ,ha='center',va='bottom') plt.ylim(-1.25,+1.25) plt.subplot(2,2,3) plt.subplot(2,2,4) plt.show() 饼状图和曲线图 import numpy as np import matplotlib.pylab as plt plt.subplot(2,2,3) n=20 Z=np.random.uniform(0,1,n) plt.pie(Z) plt.subplot(2,2,4) X=np.linspace(-np.pi,np.pi,256,endpoint=True) Y_C,Y_S=np.cos(X),np.sin(X) plt.plot(X,Y_C,color="blue",linewidth=2.5,linestyle="-") plt.plot(X,Y_S,color="red",linewidth=2.5,linestyle="-") plt.xlim(X.min()*1.1,X.max()*1.1) plt.xticks([-np.pi,-np.pi/2,0,np.pi/2,np.pi],[r'$-\pi$',r'$-\pi/2$',r'$0$',r'$+\pi/2$',r'$+\pi$']) plt.ylim(Y_C.min()*1.1,Y_C.max()*1.1) plt.yticks([-1,0,+1],[r'$-1$',r'$0$',r'$+1$']) plt.show() 更多图表类型可以去Matplotlib画廊(Gallery) 2.Bokeh 针对浏览器,d3.js 交互式绘图 简单折线图 from bokeh.plotting import figure,output_file,show x=[1,2,3,4,5] y=[6,7,2,4,5] #输出为静态文件 output_file("lines.html",title="line plot example") p=figure(title="simple line example",x_axis_label='x',y_axis_label='y') p.line(x,y,legend="Line A.",line_width=2) show(p)
作者:29DCH 来源:CSDN 原文:https://blog.csdn.net/CowBoySoBusy/article/details/80490125 版权声明:本文为博主原创文章,转载请附上博文链接!