1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 使用Python绘图库Matplotlib绘图时 – CSS – 前端 python 列表转为字典

使用Python绘图库Matplotlib绘图时 – CSS – 前端 python 列表转为字典

时间:2021-02-08 06:48:35

相关推荐

使用Python绘图库Matplotlib绘图时 – CSS – 前端 python 列表转为字典

坐标轴范围设置

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

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

1、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(0.0, 5.0)

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

fig, ax = plt.subplots(nrows=1,ncols=2)

bgimg = img.imread(‘picture.png’)

fig.figimage(bgimg)

fig.subplots_adjust(left=0.05,right=0.95,top=0.95,bottom=0.05,wspace=0.12)

ax[0].set_facecolor(‘None’)

ax[0].plot(x,y,’o-‘,color=’gold’)

ax[1].set_facecolor(‘None’)

ax[1].plot(x,y,’o-‘,color=’gold’)

ax[1].set_ybound(-0.3,0.5)

plt.show()

2、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(0.0, 5.0)

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

fig, ax = plt.subplots(nrows=2,ncols=1)

bgimg = img.imread(‘picture.png’)

fig.figimage(bgimg)

fig.subplots_adjust(left=0.05,right=0.95,top=0.95,bottom=0.05,hspace=0.12)

ax[0].set_facecolor(‘None’)

ax[0].plot(x,y,’o-‘,color=’gold’)

ax[1].set_facecolor(‘None’)

ax[1].plot(x,y,’o-‘,color=’gold’)

ax[1].set_xlim(1,4)

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方法互相影响,位置靠后的代码效果会覆盖位置靠前的代码效果。

对比实例:

1、左图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(0.0, 5.0)

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

fig, ax = plt.subplots(nrows=1,ncols=2)

bgimg = img.imread(‘picture.png’)

fig.figimage(bgimg)

fig.subplots_adjust(left=0.05,right=0.95,top=0.95,bottom=0.05,wspace=0.12)

ax[0].set_facecolor(‘None’)

ax[0].plot(x,y,’o-‘,color=’gold’)

ax[0].set_xscale(‘log’,basex=2)

ax[0].set_xbound(0,4)

ax[1].set_facecolor(‘None’)

ax[1].plot(x,y,’o-‘,color=’gold’)

ax[1].set_xbound(0,4)

ax[1].set_xscale(‘log’,basex=2)

plt.show()

2、左图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(0.0, 5.0)

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

fig, ax = plt.subplots(nrows=1,ncols=2)

bgimg = img.imread(‘picture.png’)

fig.figimage(bgimg)

fig.subplots_adjust(left=0.05,right=0.95,top=0.95,bottom=0.05,wspace=0.12)

ax[0].set_facecolor(‘None’)

ax[0].plot(x,y,’o-‘,color=’gold’)

ax[0].set_xscale(‘log’,basex=2)

ax[0].set_xlim(0,4)

ax[1].set_facecolor(‘None’)

ax[1].plot(x,y,’o-‘,color=’gold’)

ax[1].set_xlim(0,4)

ax[1].set_xscale(‘log’,basex=2)

plt.show()

3、左图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(0.0, 5.0)

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

fig, ax = plt.subplots(nrows=1,ncols=2)

bgimg = img.imread(‘picture.png’)

fig.figimage(bgimg)

fig.subplots_adjust(left=0.05,right=0.95,top=0.95,bottom=0.05,wspace=0.12)

ax[0].set_facecolor(‘None’)

ax[0].plot(x,y,’o-‘,color=’gold’)

ax[0].invert_xaxis()

ax[0].set_xlim(0,4)

ax[1].set_facecolor(‘None’)

ax[1].plot(x,y,’o-‘,color=’gold’)

ax[1].set_xlim(0,4)

ax[1].invert_xaxis()

plt.show()

4、左图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(0.0, 5.0)

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

fig, ax = plt.subplots(nrows=1,ncols=2)

bgimg = img.imread(‘picture.png’)

fig.figimage(bgimg)

fig.subplots_adjust(left=0.05,right=0.95,top=0.95,bottom=0.05,wspace=0.12)

ax[0].set_facecolor(‘None’)

ax[0].plot(x,y,’o-‘,color=’gold’)

ax[0].invert_xaxis()

ax[0].set_xlim(0,4)

ax[1].set_facecolor(‘None’)

ax[1].plot(x,y,’o-‘,color=’gold’)

ax[1].set_xlim(0,4)

ax[1].invert_xaxis()

plt.show()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。