python绘图罕用常识

figure()、subplot()和subplots()、figure.add_subplot()、figure.add_axes()、plt.plot()、plt.xlim()和plt.ylim()、plt.legeng()、plt.show()

figure()函数

函数原型:

matplotlib.pyplot.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True, FigureClass=<class 'matplotlib.figure.Figure'>, clear=False, **kwargs)

-- Create a new figure, or activate an existing figure. 新建一个画布或者激活一个曾经存在的画布

参数:

  • num: int or str, figure的惟一编号。不指定调用figure时就会默认从1减少主动传入int。
  • figsize: 指定figure的宽和高,单位为英寸;=(weight,height)
  • dpi: 指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80 1英寸等于2.5cm,A4纸是 21*30cm的纸张
  • facecolor: 背景色彩 ,facecolor='blue'
  • edgecolor: 边框色彩
  • frameon: 是否显示边框

(1)对于只画一张图,能够不应用plt.figure(),会默认将图形画在一张画布中

import matplotlib.pyplot as pltx=[1,2,3]y1=[1,2,4]y2=[1,4,8]plt.plot(x,y1,color = "red",label = "red")plt.plot(x,y2,color = "green",label = "green")plt.legend()  #无此语句会不显示右上角labelplt.show()

(2)如果想画两张图则必须应用plt.figure()两次,能够指定num,也能够不指定num,调用两次它会默认加1

import matplotlib.pyplot as pltx=[1,2,3]y1=[1,2,4]y2=[1,4,8]plt.figure()plt.plot(x,y1,color = "red",label = "red")plt.legend()  #无此语句会不显示右下角labelplt.figure()plt.plot(x,y2,color = "green",label = "green")plt.legend()  #无此语句会不显示右上角labelplt.show()

当然你也能够指定同一个num,他会和下面的输入后果统一,在同一张画布中输入图像

这个也就提供了咱们后续向某个画布中增加图像的机会,只须要调用plt.figure(num),而后调用绘制图形的函数即可,比方plt.plot()等。

subplot()和subplots()

subplot()能够将figure 划分为n个子图,而后每次执行一次subplot()会生成一个对应地位的子图

subplot()函数原型:

subplot(nrows,ncols,sharex,sharey,subplot_kw,**fig_kw)`

参数:

  • nrows: subplot的行数
  • ncols: subplot的列数
  • sharex :所有subplot应该应用雷同的X轴刻度(调节xlim将会影响所有的subplot)
  • sharey: 所有subplot应该应用雷同的Y轴刻度(调节ylim将会影响所有的subplot)
  • subplot_kw: 用于创立各subplot的关键字字典
  • **fig_kw: 创立figure时的其余关键字
import numpy as npimport matplotlib.pyplot as pltx = np.arange(0, 100)#作图1plt.subplot(2,2,1)  #等效于plt.subplot(221)plt.plot(x, x)#作图2plt.subplot(2,2,2)plt.plot(x, -x)#作图3plt.subplot(2,2,3)plt.plot(x, x ** 2)plt.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)#作图4#plt.subplot(224)#plt.plot(x, np.log(x))plt.show()

subplots()函数与subplot()参数类似

import numpy as npimport matplotlib.pyplot as pltx = np.arange(0, 100)#划分子图fig,axes=plt.subplots(2,2)   #返回一个画布和画轴对象,能够取出对于的画轴而后调用绘图函数ax1=axes[0,0]ax2=axes[0,1]ax3=axes[1,0]ax4=axes[1,1]#作图1ax1.plot(x, x)#作图2ax2.plot(x, -x)#作图3ax3.plot(x, x ** 2)ax3.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)#作图4#ax4.plot(x, np.log(x))plt.show()

和subplot()不同的是,subplots()一次画出所有的画轴,而后再调用绘图函数

figure对象能够增加子图或绘图区域

figure.add_subplot()

import numpy as npimport matplotlib.pyplot as pltx = np.arange(0, 100)#新建figure对象fig=plt.figure()#新建子图1ax1=fig.add_subplot(2,2,1)ax1.plot(x, x)#新建子图3ax3=fig.add_subplot(2,2,3)ax3.plot(x, x ** 2)ax3.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)#新建子图4ax4=fig.add_subplot(2,2,4)ax4.plot(x, np.log(x))plt.show()

能够看出和subplot的成果没什么不同

figure.add_axes()

import numpy as npimport matplotlib.pyplot as plt#新建figurefig = plt.figure()# 定义数据x = np.array([1, 2, 3, 4, 5, 6, 7])#新建区域ax1#figure的百分比,从figure 10%的地位开始绘制, 宽高是figure的80%#图像还是要在figure外部left, bottom, width, height = 0.1, 0.1, 0.8, 0.8# 取得绘制的句柄ax1 = fig.add_axes([left, bottom, width, height])  #获取一个区域ax1.plot(x,x**2 , 'r')ax1.set_title('a1')#plt.plot(x, y, 'r')#新增区域ax2,嵌套在ax1内left, bottom, width, height = 0.2, 0.6, 0.25, 0.25# 取得绘制的句柄ax2 = fig.add_axes([left, bottom, width, height])#获取另一个区域ax2.plot(x,x**3, 'b')ax2.set_title('a2')plt.show()

这样就能够在figure的任何中央绘制图像,更加灵便!

plt.plot()

参数:

  • x, y : x是可选的,如果x没有,将默认是从0到n-1,也就是y的索引。
  • fmt : str, optional, 定义线条的色彩和款式的操作,如“ro”就是红色的圆圈, "r--"就是红色虚线,这是一个疾速设置款式的办法,更多的参数能够参考最初一个keyboard arguments。
  • kwargs : Line2D properties, optional 这是一大堆可选内容,能够来外面指定很多内容,如“label”指定线条的标签,“linewidth”指定线条的宽度,等等
import matplotlib.pyplot as plta = [1, 2, 3, 4] # y 是 a的值,x是各个元素的索引b = [5, 6, 7, 8]plt.plot(a, b, 'g--', label = 'aa')plt.xlabel('this is x')plt.ylabel('this is y')plt.title('this is a demo')plt.legend() # 将样例显示进去plt.show()

plt.xlim()和plt.ylim()

import matplotlib.pyplot as pltimport numpy as npx = np.linspace(1,10,1000)y = np.random.rand(1000)plt.scatter(x,y,label="scatter figure")plt.legend()plt.xlim(1,10) #指定x和y的刻度值plt.ylim(0,1)plt.show()

plt.legeng()

legend--------(翻译:(地图或图标)图例、阐明、解释)

1.设置图列地位

plt.legend(loc='')#  填入地位,如:upper left,则图例在左上角

2.设置图例字体大小

fontsize : int or float or {‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’}

3.设置图例边框及背景

plt.legend(loc='best',frameon=False) #去掉图例边框plt.legend(loc='best',edgecolor='red') #设置图例边框色彩plt.legend(loc='best',facecolor='blue') #设置图例背景色彩,若无边框,参数有效

4.设置图例题目

legend = plt.legend(["BJ", "SH"], title='Beijing VS Shanghai')
import matplotlib.pyplot as pltimport numpy as npx = np.arange(0,10,1)plt.plot(x,x,'r--',x,np.cos(x),'g--',marker='*')plt.xlabel('row')plt.ylabel('cow')plt.legend(["BJ","SH"],loc='upper left',title='Beijing VS Shanghai'         # ,frameon=False          ,edgecolor='green'          ,facecolor='black')plt.show()

plt.show()

plt.show()是显示图片,对于有人说为什么我没有应用此函数,一样能够显示图片??

那可能是你在应用IPython这一类的交互式编程环境,比方jupyter notebook 也属于外部就是调用了IPython,因为这一类能够及时输入后果,如果你是要IDE(集成开发环境,如pycharm),就必须应用plt.show()