linux matplotlib 库的运用

来源:互联网 发布:为知笔记注销不能用 编辑:程序博客网 时间:2024/06/05 18:16
import numpy as npimport matplotlib.pyplot as pltimport matplotlibimport matplotlib.gridspec as gridspecfrom matplotlib.font_manager import *myfont=FontProperties(fname='/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf')#根据自己的系统选择 在root命令行键入fc-list :lang=zh 查看本机所支持的中文字体#matplotlib文本显示与分区'''1) matplotlib.rcParams['font.family]=myfont 全局设置(不推荐)里面的参数可以是 font.family font.size font.style--->normal italic(斜体)'''#2)局部设置gs=gridspec.GridSpec(3,3)ax1=plt.subplot(gs[0,:])ax2=plt.subplot(gs[1,-1])ax3=plt.subplot(gs[1,:-1])#[1,-2]+[1,-3]ax4=plt.subplot(gs[2,:],projection='polar')ax1.set_title('一个简单的$y=cos(2\pi x)$',fontproperties=myfont)ax1.arrow(0.2,0.5,1,0.3,facecolor='red',edgecolor='green',shape='right',width=0.1)#shape-->full right left widthax1.annotate('$y=cos(2\pi x)$',xy=(0.5,0.6), xytext=(2,1),arrowprops=dict(facecolor='red',shrink=0.05,width=1))x=np.arange(-np.pi*2,np.pi*2,np.pi/100)y=np.cos(x)ax1.plot(x,y,'r--',linewidth=0.6)ax1.grid(True)sizes=[15,30,15,40]explode=[0.1,0,0,0]labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'print(type(labels))ax3.pie(x=sizes,explode=explode,labels=labels,autopct='%1.1f%%')#autopct=fmt%percent symbol(%)  fmt-->%1.1fN=20left=np.linspace(0,2*np.pi,N,endpoint=False)#扇形的起始角度radii=10*np.random.rand(N)#扇形半径width=np.pi/4*np.random.rand(N)#扇形弧长bars=ax4.bar(left=left,height=radii,width=width,bottom=0)for r,bar in zip(radii,bars):    bar.set_facecolor(plt.cm.viridis(r/10))    bar.set_alpha(0.5)plt.show()