将“ sharex”参数设置为“ axes1”,将“ sharey”参数设置为“ axes1”

时间:2019-06-18 16:35:21

标签: matplotlib scatter-plot

使用表达式'np.arange(0.0,5.0,0.01)'定义一个numpy数组't'。

用表达式'np.sin(2np.pit)'定义另一个numpy数组's1'

再用表达式'np.sin(4np.pit)'定义另一个numpy数组's2'。

创建一个宽度为8英寸,高度为6英寸的图形。命名为无花果。

使用plt.subplot函数创建轴。将其命名为axes1。子图必须指向由2行1列创建的第一个虚拟网格。将“ title”参数设置为“ Sin(2pix)”。

使用'axes1'上的'plot'函数绘制't'和's1'的线图。

使用plt.subplot函数创建另一个轴。将其命名为axes2。子图必须指向由2行1列创建的第二个虚拟网格。将“ title”参数设置为“ Sin(4pix)”。 将“ sharex”参数设置为“ axes1”,将“ sharey”参数设置为“ axes1”。

使用'axes2'上的'plot'函数绘制't'和's2'的线图。

分别在步骤3、4和5中提供有关第二和第三功能的说明。请保存您的代码,然后继续下一步。

import numpy as np
fig = plt.figure(figsize=(8,6))
axes1 = plt.subplot(2, 1, 1, title='Sin(2pix)')
axes2 = plt.subplot(2, 1, 2, title='Sin(4pix)')
t = np.arange(0.0, 5.0, 0.01)
s1 = np.sin(2*np.pi*t)
s2 = np.sin(4*np.pi*t)
axes1.plot(t, s1)
axes2.plot(t, s2)

如何设置:将“ sharex”参数设置为“ axes1”,将“ sharey”参数设置为“ axes1”。

1 个答案:

答案 0 :(得分:1)

您可以按照以下说明在创建axes2时指定共享哪些轴。其余代码保持不变

axes2 = plt.subplot(2, 1, 2, title='Sin(4pix)', sharex=axes1, sharey=axes1)

enter image description here