使用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()
- 5星
- 4星
- 3星
- 2星
- 1星
- 暂无评论信息
