使用Python绘图库Matplotlib绘图时?matplotlib如何自定义显示背景

发表时间:2018-02-12 02:54:04 作者: 来源: 浏览:

在上一篇文章中,小编为您详细介绍了关于《pcb布线都是交叉咋解决啊20?联想电脑一体机C560型号开关电路板价格大概是多少》相关知识。本篇中小编将再为您讲解标题使用Python绘图库Matplotlib绘图时?matplotlib如何自定义显示背景。

坐标轴范围设置

坐标轴范围的设置通常有两种方法,第①种为通过axes.set_xbound和axes.set_ybound对X轴和Y轴进行分别设置,第②种为通过axes.set_xlim和axes.set_ylim方法对X轴和Y轴进行分别设置。

这两种方法虽然都能改变坐标轴刻度范围,但是在使用的时候却有差别。

① · axes.set_xbound和axes.set_ybound方法

axes.set_xbound(lower, upper)

axes.set_ybound(lower, upper)

axes.set_xbound和axes.set_ybound方法有两个参数值,这两个参数值不分先后,大的值代表坐标轴的最大值,小的值为坐标轴最小值。

方法实例:

import numpy as np

import matplotlib.pyplot as plt

import matplotlib as mpl

import matplotlib.image as img

mpl.rcParams[\'ytick.color\']=\'white\'

mpl.rcParams[\'xtick.color\']=\'white\'

mpl.rcParams[\'ytick.labelsize\']=\'large\'

mpl.rcParams[\'xtick.labelsize\']=\'large\'

mpl.rcParams[\'axes.edgecolor\']=\'white\'

x = np.linspace(⓪.⓪ · ⑤.⓪)

y = np.cos(② * np.pi * x) * np.exp(-x)

fig, ax = plt.subplots(nrows=① · ncols=②)

bgimg = img.imread(\'picture.png\')

fig.figimage(bgimg)

fig.subplots_adjust(left=⓪.⓪⑤ · right=⓪.⑨⑤ · top=⓪.⑨⑤ · bottom=⓪.⓪⑤ · wspace=⓪.①②)

ax[⓪].set_facecolor(\'None\')

ax[⓪].plot(x,y,\'o-\',color=\'gold\')

ax[①].set_facecolor(\'None\')

ax[①].plot(x,y,\'o-\',color=\'gold\')

ax[①].set_ybound(-⓪.③ · ⓪.⑤)

plt.show()

② · axes.set_xlim和axes.set_ylim方法

axes.set_xlim(left, right)

axes.set_ylim(bottom, top)

axes.set_xlim和axes.set_ylim方法我们已经在坐标轴方向中讲过,他们的参数分别为X轴左端点值、右端点值和Y轴底部端点值、顶部端点值,所以只要给定参数值就设定好了坐标轴范围。

方法实例:

import numpy as np

import matplotlib.pyplot as plt

import matplotlib as mpl

import matplotlib.image as img

mpl.rcParams[\'ytick.color\']=\'white\'

mpl.rcParams[\'xtick.color\']=\'white\'

mpl.rcParams[\'ytick.labelsize\']=\'large\'

mpl.rcParams[\'xtick.labelsize\']=\'large\'

mpl.rcParams[\'axes.edgecolor\']=\'white\'

x = np.linspace(⓪.⓪ · ⑤.⓪)

y = np.cos(② * np.pi * x) * np.exp(-x)

fig, ax = plt.subplots(nrows=② · ncols=①)

bgimg = img.imread(\'picture.png\')

fig.figimage(bgimg)

fig.subplots_adjust(left=⓪.⓪⑤ · right=⓪.⑨⑤ · top=⓪.⑨⑤ · bottom=⓪.⓪⑤ · hspace=⓪.①②)

ax[⓪].set_facecolor(\'None\')

ax[⓪].plot(x,y,\'o-\',color=\'gold\')

ax[①].set_facecolor(\'None\')

ax[①].plot(x,y,\'o-\',color=\'gold\')

ax[①].set_xlim(① · ④)

plt.show()

需要注意的是

当我们需要使用axes.set_xscale方法改变X轴比例尺时,axes.set_xbound方法和axes.set_xlim方法必须在axes.set_xscale方法之后使用才能正常显示。

当我们需要使用axes.invert_xaxis方法对坐标轴方向进行改变时,使用axes.set_xbound方法并不会对axes.invert_xaxis方法产生影响;而axes.set_xlim方法与axes.invert_xaxis方法互相影响,位置靠后的代码效果会覆盖位置靠前的代码效果。

对比实例:

① · 左图axes.set_xbound方法在axes.set_xscale方法之后使用,右图axes.set_xbound方法在axes.set_xscale方法之前使用

import numpy as np

import matplotlib.pyplot as plt

import matplotlib as mpl

import matplotlib.image as img

mpl.rcParams[\'ytick.color\']=\'white\'

mpl.rcParams[\'xtick.color\']=\'white\'

mpl.rcParams[\'ytick.labelsize\']=\'large\'

mpl.rcParams[\'xtick.labelsize\']=\'large\'

mpl.rcParams[\'axes.edgecolor\']=\'white\'

x = np.linspace(⓪.⓪ · ⑤.⓪)

y = np.cos(② * np.pi * x) * np.exp(-x)

fig, ax = plt.subplots(nrows=① · ncols=②)

bgimg = img.imread(\'picture.png\')

fig.figimage(bgimg)

fig.subplots_adjust(left=⓪.⓪⑤ · right=⓪.⑨⑤ · top=⓪.⑨⑤ · bottom=⓪.⓪⑤ · wspace=⓪.①②)

ax[⓪].set_facecolor(\'None\')

ax[⓪].plot(x,y,\'o-\',color=\'gold\')

ax[⓪].set_xscale(\'log\',basex=②)

ax[⓪].set_xbound(⓪ · ④)

ax[①].set_facecolor(\'None\')

ax[①].plot(x,y,\'o-\',color=\'gold\')

ax[①].set_xbound(⓪ · ④)

ax[①].set_xscale(\'log\',basex=②)

plt.show()

② · 左图axes.set_xlim方法在axes.set_xscale方法之后使用,右图axes.set_xlim方法在axes.set_xscale方法之前使用

import numpy as np

import matplotlib.pyplot as plt

import matplotlib as mpl

import matplotlib.image as img

mpl.rcParams[\'ytick.color\']=\'white\'

mpl.rcParams[\'xtick.color\']=\'white\'

mpl.rcParams[\'ytick.labelsize\']=\'large\'

mpl.rcParams[\'xtick.labelsize\']=\'large\'

mpl.rcParams[\'axes.edgecolor\']=\'white\'

x = np.linspace(⓪.⓪ · ⑤.⓪)

y = np.cos(② * np.pi * x) * np.exp(-x)

fig, ax = plt.subplots(nrows=① · ncols=②)

bgimg = img.imread(\'picture.png\')

fig.figimage(bgimg)

fig.subplots_adjust(left=⓪.⓪⑤ · right=⓪.⑨⑤ · top=⓪.⑨⑤ · bottom=⓪.⓪⑤ · wspace=⓪.①②)

ax[⓪].set_facecolor(\'None\')

ax[⓪].plot(x,y,\'o-\',color=\'gold\')

ax[⓪].set_xscale(\'log\',basex=②)

ax[⓪].set_xlim(⓪ · ④)

ax[①].set_facecolor(\'None\')

ax[①].plot(x,y,\'o-\',color=\'gold\')

ax[①].set_xlim(⓪ · ④)

ax[①].set_xscale(\'log\',basex=②)

plt.show()

③ · 左图axes.set_xbound方法在axes.invert_xaxis方法之后使用,右图axes.set_xbound方法在axes.invert_xaxis方法之前使用

import numpy as np

import matplotlib.pyplot as plt

import matplotlib as mpl

import matplotlib.image as img

mpl.rcParams[\'ytick.color\']=\'white\'

mpl.rcParams[\'xtick.color\']=\'white\'

mpl.rcParams[\'ytick.labelsize\']=\'large\'

mpl.rcParams[\'xtick.labelsize\']=\'large\'

mpl.rcParams[\'axes.edgecolor\']=\'white\'

x = np.linspace(⓪.⓪ · ⑤.⓪)

y = np.cos(② * np.pi * x) * np.exp(-x)

fig, ax = plt.subplots(nrows=① · ncols=②)

bgimg = img.imread(\'picture.png\')

fig.figimage(bgimg)

fig.subplots_adjust(left=⓪.⓪⑤ · right=⓪.⑨⑤ · top=⓪.⑨⑤ · bottom=⓪.⓪⑤ · wspace=⓪.①②)

ax[⓪].set_facecolor(\'None\')

ax[⓪].plot(x,y,\'o-\',color=\'gold\')

ax[⓪].invert_xaxis()

ax[⓪].set_xlim(⓪ · ④)

ax[①].set_facecolor(\'None\')

ax[①].plot(x,y,\'o-\',color=\'gold\')

ax[①].set_xlim(⓪ · ④)

ax[①].invert_xaxis()

plt.show()

④ · 左图axes.set_xlim方法在axes.invert_xaxis方法之后使用,右图axes.set_xlim方法在axes.invert_xaxis方法之前使用

import numpy as np

import matplotlib.pyplot as plt

import matplotlib as mpl

import matplotlib.image as img

mpl.rcParams[\'ytick.color\']=\'white\'

mpl.rcParams[\'xtick.color\']=\'white\'

mpl.rcParams[\'ytick.labelsize\']=\'large\'

mpl.rcParams[\'xtick.labelsize\']=\'large\'

mpl.rcParams[\'axes.edgecolor\']=\'white\'

x = np.linspace(⓪.⓪ · ⑤.⓪)

y = np.cos(② * np.pi * x) * np.exp(-x)

fig, ax = plt.subplots(nrows=① · ncols=②)

bgimg = img.imread(\'picture.png\')

fig.figimage(bgimg)

fig.subplots_adjust(left=⓪.⓪⑤ · right=⓪.⑨⑤ · top=⓪.⑨⑤ · bottom=⓪.⓪⑤ · wspace=⓪.①②)

ax[⓪].set_facecolor(\'None\')

ax[⓪].plot(x,y,\'o-\',color=\'gold\')

ax[⓪].invert_xaxis()

ax[⓪].set_xlim(⓪ · ④)

ax[①].set_facecolor(\'None\')

ax[①].plot(x,y,\'o-\',color=\'gold\')

ax[①].set_xlim(⓪ · ④)

ax[①].invert_xaxis()

plt.show()

编后语:关于《使用Python绘图库Matplotlib绘图时?matplotlib如何自定义显示背景》关于知识就介绍到这里,希望本站内容能让您有所收获,如有疑问可跟帖留言,值班小编第一时间回复。 下一篇内容是有关《玩英雄联盟魔兽争霸3有时在网上看视频时屏幕出现雪花?养泰迪和比熊犬谁好》,感兴趣的同学可以点击进去看看。

资源转载网络,如有侵权联系删除。

相关资讯推荐

相关应用推荐

玩家点评

条评论

热门下载

  • 手机网游
  • 手机软件

热点资讯

  • 最新话题