添加组件_python高级绘图-添加图形组件(标题,坐标,表格)

添加图例和标题

import matplotlib as mplimport matplotlib.pyplot as pltimport numpy as np
x = np.linspace(-2*np.pi,2*np.pi,200)y = np.sin(x)y1 = np.cos(x)plt.plot(x,y,label=r'$sin(x)$')  #TeX表达式plt.plot(x,y1,label=r'$cos(x)$')plt.legend(loc='lower left')plt.title('Cos & Sin plot')plt.show()
0d0a82f87cf7f67ce54bd857de0e896f.png

图例样式

x = np.arange(0,10,.1)y = np.power(x,0.5)y1 = np.power(x,1/3)y2 = np.power(x,1/4)plt.plot(x,y,ls='-',lw=2,label='$x^{1/2}$')plt.plot(x,y1,ls='-',lw=2,c='r',label='$x^{1/3}$')plt.plot(x,y2,ls='-',lw=2,c='y',label=r'$x^{1/4}$')plt.legend(loc='upper left',bbox_to_anchor=(.05,.95),ncol=3,,shadow=True,fancybox=True)# upper rigth 1# lower right 4# lower center 8# upper left 2# center left 6# upper center 9# lower left 3# center right 7# center 10# bbox_to_anchor(x轴位置百分比,y轴位置百分比,x轴框长度,y轴框高度)plt.show()
68d0dc6dccb2da0a21ec60d3e361b6ff.png

标题样式

x = np.linspace(-2,2,1000)y = np.exp(x)plt.plot(x,y,ls='-',lw=2,color='g')plt.title('center')plt.title('Left title',loc='left',         fontdict={'size':'xx-large',                  'color':'r',                  'family':'Times New Roman'})plt.title('right title',loc='right',         size=20,         style='oblique',         color='c')plt.show()
bb427bc0021e7c27e7d7262d49b5957d.png

饼图图例

labels = [chr(i) for i in range(65,70)]weight = np.random.randint(10,50,5)wedges,texts,autotexts = plt.pie(weight,autopct='%3.2f%%',                                textprops=dict(color='w'))plt.legend(wedges,  # 饼图实例列表          labels,  # 文本标签列表          fontsize=12,          ,          loc='center left',          bbox_to_anchor=(0.9,0,.2,1))plt.setp(autotexts,size=15,weight='bold')plt.setp(texts,size=12)plt.title('content of samples')plt.show()
9b7e9b40ffb9f95dedbd106937df02a5.png

刻度范围和刻度标签

x = np.linspace(-2*np.pi,2*np.pi,200)y = np.sin(x)plt.subplot(211)  # 2行1列的图形1plt.plot(x,y)plt.subplot(212)  #2行1列的图形2plt.xlim(-2*np.pi,2*np.pi)plt.xticks([-2*np.pi,-3*np.pi/2,-1*np.pi,-1*np.pi/2,0,np.pi/2,np.pi,3*np.pi/2,2*np.pi],          [r'$-2pi$',r'$-3pi/2$',r'$-pi$',r'$-pi/2$',r'$0$',r'$pi/2$',r'$pi$',r'$3pi/2$',r'$2pi$'])# xticks()修改x轴刻度标签plt.plot(x,y)plt.show()
ba65134f3a9f88d0179ed03898ca7135.png

坐标轴刻度了逆序

xvalues = np.arange(1,11,0.2)yvalues = np.power(xvalues,1/3)+0.9plt.plot(xvalues,yvalues,        linestyle='-',        linewidth=2,        color='r')plt.xlim(10,1)plt.xlabel('axis X')plt.ylabel('axis Y')plt.grid(ls=':',lw=1,color='gray',alpha=.5)plt.show()
8bdd8e5089076c87cc9f39fc5691f31b.png

给图片添加表格

labels = [chr(i) for i in range(65,70)]values = [abs(round(i)) for i in np.random.randint(0,25,4)]values.append(100-sum(values))plt.pie(values,        explode=np.repeat(0.1,5),       labels=labels,       autopct='%1.1f%%',       startangle=45,       shadow=True)plt.title('Pie Chart')tblabels = labelsrlabels=['Chart Table']tbvalues = [list(np.random.randint(0,500,5))]plt.table(cellText=tbvalues,         cellLoc='center',         colWidths=[0.1]*5,         colLabels=tblabels,         rowLabels=rlabels,         rowLoc='center',         loc='bottom')# cellText 表格数值# colWidths 表格列宽# colLables 表格的列名# colColours 表格列名颜色# rowLabels 表格行名# rowLoc 表格每行行名对齐位置,左,中,右# loc 表格在画布中的位置plt.show()
0b57fba9e13edc498f22eeec40f55207.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值