matplotlib画图基本设置

匿名 (未验证) 提交于 2019-12-03 00:19:01

  • 解说

    plt.figure(figsize=(9.6,4.1),facecolor='gray',edgecolor='black')

    设置图片大小9.6x4.1英寸(1in8.13cm),facecolor背景颜色,edgecolor边框线颜色。

    plt.subplot(1,2,1) plt.title('(a)') plt.xlim((-11,11)) plt.ylim((-200000,200000)) plt.xlabel(r'$\alpha$') plt.ylabel(r'$\beta$') plt.yscale('linear') plt.minorticks_on() plt.tick_params(which='minor',direction='in',width=2,length=4) plt.tick_params(which='major',direction='out',width=4,length=6) plt.tick_params(top='off',bottom='on',left='on',right='off') plt.tick_params(labeltop='off',labelbottom='on',labelleft='on',labelright='off')  plt.subplot(1,2,2) plt.title('(b)') plt.xlim((-15,15)) plt.ylim((-300000,300000)) plt.xlabel('this is x',fontsize=12) plt.ylabel('this is y')

    见下图,xlimylim指定坐标轴的取值范围,title指定图的名称,xlabel,ylabel设置坐标轴名字及其字体大小,支持Latex语法。yscale对坐标轴刻度值的函数,有log等选项。 minorticks_on打开最小刻度线,tick_params(which='minor',direction='in',width=2,length=4)控制主次刻度线的长度,宽度和朝向,(top='off',bottom='on',left='on',right='off')设置主刻度线在上下左右四条边上是否显示,tick_params(labeltop='off',labelbottom='on',labelleft='on',labelright='off')设置刻度值(ticklabel)在上下左右四条边上是否显示。

    plt.xticks([]) plt.yticks([-200000,-100000,0,100000,200000],        [r'$big$', r'$zero$',r'small',r'$really small$',r'fine'])

    如下图,xtick([])关闭x轴坐标刻度,yticks([...1],[...2])设置刻度值名称,支持Latex语法。

    plt.axis('off')

    如下图axis('off')关闭坐标轴显示。

       plt.grid(True,color='gray',linestyle='--',linewidth=0.8,alpha=0.9)    ax=plt.gca()    ax.xaxis.grid(True,color='r',linestyle='--',linewidth=1.2,alpha=0.3)#alpha is transparencys

    如下图grid打开网格线,ax.xaxis.grid()打开x轴的网格线,并设置格式。alpha是透明度,值越大透明度越,越不透明。

     ax.xaxis.set_ticks_position('top')  ax.spines['left'].set_linewidth(1.6)  ax.spines['right'].set_visible(False)  ax.spines['bottom'].set_visible(False)  ax.spines['top'].set_linestyle('--')  ax.spines['top'].set_color('blue')  ax.spines['left'].set_position(('data',0))  ax.spines['top'].set_position(('data',0))   xMajorLocator=MultipleLocator(4)  xMinorLocator=MultipleLocator(1.0)  yMajorLocator=MultipleLocator(100000)  yMinorLocator=MultipleLocator(10000)  ax.xaxis.set_major_locator(xMajorLocator)  ax.xaxis.set_minor_locator(xMinorLocator)  ax.yaxis.set_major_locator(yMajorLocator)  ax.yaxis.set_minor_locator(yMinorLocator)
    for tick in ax.xaxis.get_major_ticks():     tick.label1.set_fontsize(15)

    如下图,ax.xaxis.set_ticks_position('top')设置x轴的刻度是位于哪一条边上,一般选top或者bottom,ax.spines设置上下左右四条边的颜色、线型、位置等,ax.xaxis.set_major_locator等,设置坐标轴主次刻度值的分辨率。tick.label1.set_fontsize(15)设置刻度值字体大小。

     ax.spines['left'].set_linewidth(2.6)   ax.axes.get_yaxis().set_visible(False)

    如下图,ax.axes.get_yaxis().set_visible(False)设置坐标轴是上的刻度不可见,但轴仍在。


    plt.plot(x,y1,color='r',linewidth=0.6,label='oddpower') plt.plot(x,y3,color='g',linewidth=1.3,linestyle='--',label='evenpower') plt.legend(loc='down left')

    如下图,画图并指定图示说明的位置。


    plt.subplots_adjust(top=0.92, bottom=0.12, left=0.12, right=0.95, hspace=0.25,wspace=0.35) plt.savefig('tutorial0.png',dpi=300)

    调整子图之间的相对位置,并保存图片,指定dpidpi越大,图片越清晰。

  • 程序

     #!/usr/bin/env python2   # -*- coding: utf-8 -*-  """ Created on Tue May 22 16:49:46 2018 @author: rd """ from __future__ import division import numpy as np import matplotlib.pyplot as plts from matplotlib.ticker import MultipleLocator  x=np.linspace(-11,11,120) y1=x+x**3+x**5 y2=x**2+x**4+x**6 y3=x**2+x**6  plt.figure(figsize=(9.6,4.1),facecolor='gray',edgecolor='black') plt.subplot(1,2,1) plt.xlim((-14,14)) plt.ylim((-200000,200000)) plt.xlabel('this is x',fontsize=12) plt.ylabel('this is y') plt.minorticks_on() plt.tick_params(which='minor',direction='in',width=2,length=4) plt.tick_params(which='major',direction='out',width=4,length=6) plt.tick_params(top='off',bottom='on',left='on',right='off') plt.tick_params(labeltop='off',labelbottom='on',labelleft='on',labelright='off')  #plt.xticks([])   #plt.yticks([])   #plt.xticks([-10,-5,0,5,10],[])   #plt.xticks([-9,-6,-3,0,3,6,9],[])  plt.grid(True,color='gray',linestyle='--',linewidth=0.8,alpha=0.8)#open the grid plt.yscale('linear') plt.plot(x,y1,color='r') plt.plot(x,y3,color='g',linewidth=1.3,linestyle='--')  #plt.axis('off')  ax=plt.gca() ax.xaxis.grid(color='r',linestyle='--',linewidth=1.2,alpha=0.3)#alpha is transparencys ax.xaxis.set_ticks_position('bottom')  #ax.patch.set_facecolor('gray')  for tick in ax.xaxis.get_major_ticks():     tick.label1.set_fontsize(10) ax.spines['left'].set_linewidth(1.6)#line of the axes ax.spines['right'].set_visible(False) ax.spines['top'].set_linestyle('--') ax.spines['top'].set_color('blue') ax.spines['bottom'].set_linewidth(1.6)  #ax.spines['left'].set_position(('data',0))#the position of the   #ax.spines['bottom'].set_position(('data',0))# axis  xMajorLocator=MultipleLocator(4) xMinorLocator=MultipleLocator(1.0) yMajorLocator=MultipleLocator(100000) yMinorLocator=MultipleLocator(10000) ax.xaxis.set_major_locator(xMajorLocator) ax.xaxis.set_minor_locator(xMinorLocator) ax.yaxis.set_major_locator(yMajorLocator) ax.yaxis.set_minor_locator(yMinorLocator) plt.yticks([-200000,-100000,0,100000,200000],            [r'$big$', r'$zero$',r'small',r'$really small$',r'fine'])  #ax.axes.get_yaxis().set_visible(False) #make the yticks is unvisible   #ax.axes.get_yaxis().set_visible(False)  plt.legend('upper right')  plt.subplot(1,2,2) plt.minorticks_off()  #plt.xticks([])  plt.tick_params(which='both',direction='out') plt.plot(x,y2,color='b') plt.show() plt.subplots_adjust(top=0.92, bottom=0.12, left=0.12, right=0.95, hspace=0.25,                     wspace=0.35) plt.savefig('tutorial0.png',dpi=600)

refer

[1] https://blog.csdn.net/Fortware/article/details/51934814

[2] http://python.jobbole.com/85106/

[3] https://matplotlib.org/tutorials/introductory/usage.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!